如何从 Laravel 的控制器中获取条件数据
Posted
技术标签:
【中文标题】如何从 Laravel 的控制器中获取条件数据【英文标题】:How to fetch conditional data from controller in Laravel 【发布时间】:2021-10-10 06:51:57 【问题描述】:我有两个表“用户”和“预测”,用户表有两列“Org1”和“Org2”,预测表“CurrOg1”和“CurrOrg2”中有相同的列。当用户注册时,他必须从下拉列表中选择“Org1”和“Org2”,并将选定的值分配给用户。现在我想检查用户表中的“Org1”和“Org2”值以及登录到应用程序的当前用户的预测表。例如,如果用户“Org1”在注册时存储为“abc”,他必须只能查看“CurrOrg”为“abc”的数据,对于“Org2”也是如此。 当我尝试获取结果时出现数据表错误。
这是我的注册控制器和查看代码:
class RegisterController extends Controller
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
$this->middleware('guest');
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'Org1' => [ 'string'],
'Org2' => ['string'],
]);
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'Org1' => $data['Org1'],
'Org2' => $data['Org2'],
]);
Mail::to($data['email'])->send(new WelcomeMail($user));
return $user;
public function showRegistrationForm()
$Org1_list = DB::table('predictions')
->select('CurrOrg1')
->groupBy('CurrOrg1')
->orderBy('CurrOrg1', 'ASC')
->get();
$Org2_list = DB::table('predictions')
->select('CurrOrg2')
->groupBy('CurrOrg2')
->orderBy('CurrOrg2', 'ASC')
->get();
return view('auth.register', compact('Org1_list','Org2_list'));
注册查看:
<div class="form-group row">
<label for="Org1" class="col-md-4 col-form-label text-md-right">Org1</label>
<div class="col-md-6">
<select class="form-control" name="Org1" id="Org1">
<option value="">Select Org1</option>
@foreach($Org1_list as $use)
@if(!empty($use))
<option value="$use->CurrOrg1">$use->CurrOrg1</option>
@endif
@endforeach
</select>
@error('Org1')
<span class="invalid-feedback" role="alert">
<strong> $message </strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="Org2" class="col-md-4 col-form-label text-md-right">Org2</label>
<div class="col-md-6">
<select class="form-control" name="Org2" id="Org2">
<option value="">Select Org2</option>
@foreach($Org2_list as $use)
@if(!empty($use))
<option value="$use->CurrOrg2">$use->CurrOrg2</option>
@endif
@endforeach
</select>
@error('Org2')
<span class="invalid-feedback" role="alert">
<strong> $message </strong>
</span>
@enderror
</div>
</div>
这是我的预测控制器:
$user=DB:: table('users')
->select('id','Org1','Org2','email')
->get();
if($CurrOrg1 == $user->Org1 && Auth::user()->email == $user->email)
$data = DB:: table('predictions')
->select('id','EmployeeNumber','Role','Region','CurrOrg1','CurrOrg2','RiskZone','Probablity','Rating','Feedback','Gender','CurrOrg3','CurrOrg4','CurrOrg5','ProfessionalClassification','Local_Expat','EmployeeRoleSeniority','FeedRisklevel','Feedinfluencers','Action','Fname','Lname','Avgweekhr')
->get();
return datatables()->of($data)
->addColumn('Feedback', function($data)
if($data->Action == 'No')
return "<a href='#' style='background-color:#CA0088;color:#fff' class='btn btn-sm Feedback' id='".$data->id."'>Feedback</a>";
else
return "<a href='#' style='background-color:#00A300;color:#fff' class='btn btn-sm Feedback' id='".$data->id."'>Feedback</a>";
)
->escapeColumns([])
->make(true);
这是我加载数据的方式:
$(document).ready(function()
fill_datatable();
function fill_datatable(BusinessUnit = '', CurrOrg1 = '', CurrOrg2 = '', Role = '', Region = '' , Gender = '', CurrOrg3 = '' , CurrOrg4 = '', CurrOrg5 = '', ProfessionalClassification = '')
var dataTable = $('#members_data').DataTable(
dom: 'Bfrtip',
buttons: ['excel'],
processing: true,
serverSide: false,
ajax:
url: " route('Predictions.index') ",
data:BusinessUnit:BusinessUnit,CurrOrg1:CurrOrg1,CurrOrg2:CurrOrg2,Role:Role,Region:Region,Gender:Gender,ProfessionalClassification:ProfessionalClassification,CurrOrg3:CurrOrg3,CurrOrg4:CurrOrg4,CurrOrg5:CurrOrg5
,
columns: [
data: 'EmployeeNumber',
name: 'EmployeeNumber',
render:function(data, type, row)
//console.log(row)
//return "a"
return "<a href='/detail/"+ row.id +"'>" + row.EmployeeNumber+"</a>"
,
data:'Fname',
name:'Fname'
,
data:'Lname',
name:'Lname'
,
data:'Role',
name:'Role'
,
data:'Region',
name:'Region'
,
data:'CurrOrg1',
name:'CurrOrg1'
,
data:'CurrOrg2',
name:'CurrOrg2'
,
);
有人可以建议我在这里做错了什么,或者如何解决上述问题。提前致谢!
【问题讨论】:
能否请您显示数据表中的错误? @Tayyabmehar 这是错误:DataTables 警告:table id=members_data - Ajax 错误。有关此错误的更多信息,请参阅datatables.net/tn/7 您是否从控制器调试过数据?您收到什么格式的数据? 【参考方案1】:将 get() 函数替换为 first() 以匹配条件。
$user=DB:: table('users')
->select('id','Org1','Org2','email')
->first();
【讨论】:
这似乎不起作用..同样的数据包错误 您是否检查过您从控制器获取的数据格式是否正常?什么是可取的? debug($data) 在返回之前。其次在 ajax 调用中设置“serverSide: true”。 是的,正如你提到的那样进行了更改......我正在以所需格式获取数据 我只想向用户显示那些不应该显示 Org1=CurrOrg1 其余数据的记录。如果 Org1 值为“abc”、“xyx”、“mnc”,则检查记录预测表并仅显示 CurrOrg1 分别为“abc”、“xyx”、“mnc”的记录以上是关于如何从 Laravel 的控制器中获取条件数据的主要内容,如果未能解决你的问题,请参考以下文章
当我们压缩到视图时,如何对从控制器获取的 JSON 数组中的数据求和(Laravel)
如何正确地将数据从 vue 组件发布到 laravel 控制器
如何获取用户选择的按钮值并在 laravel 5.4 的控制器上使用它