public function store(Request $request)
{
$booking = ($request->isMethod('put')) ? Booking::findOrFail($request->booking_id) : new Booking;
$booking->checkIn = $request->checkIn;
$booking->checkOut = $request->checkOut;
$booking->room_id = $request->room_id;
$booking->user_id = auth()->user()->id;//not working
if($booking->save()){
return new BookingResource($booking);
}
}
Route::put('/booking','BookingsController@store');//api.php
这里auth()-> user()-> id不起作用,但是如果我使用相同的代码但在routes / web.php中使用了路由代码,它的工作查找
答案 0 :(得分:1)
通过auth时使用的防护参数..
1. auth('api')->user(); //if u are using api guard ...(web guard)
2. $request->user('api'); //by reqeust class
3. Auth::guard('api')->user() //using Auth facade
答案 1 :(得分:0)
在路由中使用 auth:api 中间件。
Route::middleware(['auth:api'])->put('/booking','BookingsController@store');
答案 2 :(得分:0)
在控制器中使用这种方式:
use Illuminate\Support\Facades\Auth
$booking->user_id = Auth::user()->id;