Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Desssha's avatar

ErrorException: Trying to get property 'id' of non-object in file C:\xampp\htdocs\hotelbooking\app\Http\Controllers\ApiHotelController.php on line 85

still not solved can any one help please

this ApiHotelController

$booking = new Booking();

        $booking->room_id = $request->room_id;
        $booking->user_id = $request->user()->id;
        $booking->date_start = $request->date_start;
        $booking->date_end = $request->date_end;
        $booking->booking_number = mt_rand(1000000, 9999999);
        $booking->save();


        return['booking_number'=>$booking->booking_number];

}

this BookingResource

class BookingResource extends JsonResource { /** * Transform the resource into an array. * * param \Illuminate\Http\Request $request * return array */ public function toArray($request) { return [ 'room_id' => $this->room_id, 'booking_number' =>$this->booking_number, 'date_start' => $this->date_start, 'date_end' => $this->date_end, ]; } }

model class Booking extends Model { use HasFactory;

protected $fillable = [
    'room_id',
    'user_id',
    'booking_number',
    'date_start',
    'date_end'
];
    public function room(){

        return   $this->belongsTo(Room::class,'room_id');
    }

    public function user(){

        return  $this->belongsTo (User::class,'user_id');
    }

model users class User extends Authenticatable { use HasFactory, Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name',
    'email',
    'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password',
    'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

public function bookings(){
  
    return  $this->hasMany(Booking::class);
}

public function hotels(){
    return $this->belongsToMany(Hotel::class);
}

}

0 likes
15 replies
Nakov's avatar

This is the line that throws the error:

$booking->user_id = $request->user()->id;

and the reason is probably because your route is not auth protected, so that's why there is no user() available.

Before that line try this:

dd($request->user());

is the result null? If it is make sure you add the auth middleware to the route like this: https://laravel.com/docs/master/authentication#protecting-routes

Desssha's avatar

i use postman and send post request to make new reserving in data Route::post('bookings/create',[ApiHotelController::class,'store']); this route

Desssha's avatar

it rout

Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); }); Route::get('rooms',[ApiHotelController::class, 'index']); Route::get('rooms/show/{id}',[ApiHotelController::class,'show']); Route::post('bookings/create',[ApiHotelController::class,'store']);

Nakov's avatar

and if you try instead of getting the user from the request, to use this one:

auth('api')->id();

//or

auth('api')->user()->id;
Desssha's avatar

how can fixed it was change before like it give me another error $booking->user_id = $request->id; give me this error Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into bookings (room_id, user_id, date_start, date_end, booking_number, updated_at, created_at) values (1, ?, 2021-1-18, 2021-1-20, 3986592, 2021-01-17 19:46:24, 2021-01-17 19:46:24)) in file

Nakov's avatar

in order for this $request->id; to work, you will need to send id => SOME_USER_ID with your request when you are posting using Postman.

Desssha's avatar

i do but give me error i send user_id , room_id , date_start , date_end and give me the error

Desssha's avatar

like you see in error Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into bookings (room_id, user_id, date_start, date_end, booking_number, updated_at, created_at) values (1, ?, 2021-1-18, 2021-1-20, 8475498, 2021-01-17 20:04:41, 2021-01-17 20:04:41)) in file

insert everything only user_id ? in vlues

Nakov's avatar
Nakov
Best Answer
Level 73

The error is because the user_id field is not nullable. So if you are sending a user_id then use this instead:

$request->user_id

and not the method.

1 like
Desssha's avatar

when i used second give me this error ErrorException: Trying to get property 'id' of non-object in file C:\xampp\htdocs\

Nakov's avatar

You probably still are using ->id on something else, and it throws an error. Sorry @desssha but I don't have whole night, nor do I have your code. But the error is pretty obvious, I think I gave you a ton of ideas on where to look for the error.

Desssha's avatar

thankssssssssssss man you are the best omg one day in this problem. it work man yeaaahh thanks alote. sorry i am new in laravel .

MarianoMoreyra's avatar

Hi @desssha

From the error and your code, the error seems to be happening at this line:

$booking->user_id = $request->user()->id;

This makes me think that you are not logged in and so it can't get the user() in order to fetch it's id

MarianoMoreyra's avatar

Well, it seems @nakov pushed the "post" button right before me! :)

Anyway, his answer is clearly the one!!

Desssha's avatar

how can fixed it was change before like it give me another error $booking->user_id = $request->id; Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null (SQL: insert into bookings (room_id, user_id, date_start, date_end, booking_number, updated_at, created_at) values (1, ?, 2021-1-18, 2021-1-20, 3986592, 2021-01-17 19:46:24, 2021-01-17 19:46:24)) in file

Please or to participate in this conversation.