Reputation: 1425
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
Here auth()->user()->id is not working but its working find if i use it the same code but route code in routes/web.php
Upvotes: 2
Views: 4249
Reputation: 13669
use this way in your controller :
use Illuminate\Support\Facades\Auth
$booking->user_id = Auth::user()->id;
Upvotes: 1
Reputation: 85
use auth:api middleware in your route.
Route::middleware(['auth:api'])->put('/booking','BookingsController@store');
Upvotes: 1
Reputation: 15115
pass guard parameter in auth used like that ..
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
Upvotes: 5