Laravel 5.5 只加载当前登录用户的数据
Laravel 5.5 load only the data for the currently logged in user
嗨,我是 laravel 的新手,但我想加载当前登录用户的所有预订。
我试过这样做
//check if user is logged in
if ($user = Auth::user()) {
//get only the bookings for the currently logged in user
$allProducts =Booking::where('client', Auth::user()->name)->where('name', $name)->first();
//store the bookings in a products variable
$products = json_decode(json_encode($allProducts));
//Loop through the products:
foreach ($products as $key => $val) {
//get the name of the service by matching it's id in the service model to the service column in the products
$service_name = Service::where(['id' => $val->service])->first();
//get the charge amount of the service by matching it's id in the Charge model to the charge column in the products
$service_fee = Charge::where(['id' => $val->charge])->first();
//get the status of the service by matching it's id in the status model to the status column in the products
$service_status = Status::where(['id' => $val->status])->first();
$products[$key]->service_name = $service_name->name;
$products[$key]->service_fee = $service_fee->total;
$products[$key]->service_status = $service_status->name;
}
return view('client.booking.view_bookings')->with(compact('products'));
}
return view('/login');
}
但这给我一个错误:未定义的变量:行上的名称
$allProducts =Booking::where('client', Auth::user()->name)->where('name', $name)->first();
我做错了什么?以及如何解决它以仅显示所需的数据
我试图理解你在做什么,但没有成功,但从你在评论中的解释,我想我知道你想做什么。
既然你说这段代码很适合你,除了它会为你提供数据库中所有数据的结果,而不考虑登录用户
$allProducts = Booking::get();
这是因为创建了一个查询,该查询选择了数据库中的所有数据。
您需要在语句中添加一个 where 子句。要做到这一点,只需将其添加到上面的代码行
where('client', Auth::user()->name)
它将 return 仅包含与当前登录用户名称相同的客户端列的数据。
因此整行代码变为;
$allProducts = Booking::get()->where('client', Auth::user()->name);
或者您可以使用过滤器
嗨,我是 laravel 的新手,但我想加载当前登录用户的所有预订。
我试过这样做
//check if user is logged in
if ($user = Auth::user()) {
//get only the bookings for the currently logged in user
$allProducts =Booking::where('client', Auth::user()->name)->where('name', $name)->first();
//store the bookings in a products variable
$products = json_decode(json_encode($allProducts));
//Loop through the products:
foreach ($products as $key => $val) {
//get the name of the service by matching it's id in the service model to the service column in the products
$service_name = Service::where(['id' => $val->service])->first();
//get the charge amount of the service by matching it's id in the Charge model to the charge column in the products
$service_fee = Charge::where(['id' => $val->charge])->first();
//get the status of the service by matching it's id in the status model to the status column in the products
$service_status = Status::where(['id' => $val->status])->first();
$products[$key]->service_name = $service_name->name;
$products[$key]->service_fee = $service_fee->total;
$products[$key]->service_status = $service_status->name;
}
return view('client.booking.view_bookings')->with(compact('products'));
}
return view('/login');
}
但这给我一个错误:未定义的变量:行上的名称
$allProducts =Booking::where('client', Auth::user()->name)->where('name', $name)->first();
我做错了什么?以及如何解决它以仅显示所需的数据
我试图理解你在做什么,但没有成功,但从你在评论中的解释,我想我知道你想做什么。
既然你说这段代码很适合你,除了它会为你提供数据库中所有数据的结果,而不考虑登录用户
$allProducts = Booking::get();
这是因为创建了一个查询,该查询选择了数据库中的所有数据。
您需要在语句中添加一个 where 子句。要做到这一点,只需将其添加到上面的代码行
where('client', Auth::user()->name)
它将 return 仅包含与当前登录用户名称相同的客户端列的数据。
因此整行代码变为;
$allProducts = Booking::get()->where('client', Auth::user()->name);
或者您可以使用过滤器