ProCodeAL
ProCodeAL

Reputation: 23

Laravel How to get AI id in controller before saving

I have tried to get the auto increment ID from the booking table before saving the new data's, but I have errors all the time.

This is the code in the controller:

public function addAppointment(Request $request) {
    $user = auth()->user();

    $booking = new Booking();
    $booking->vac_center_id = $request->get('vaccination_center');
    $booking->vac_id = $request->get('vaccination_id');
    $booking->date_of_shot = $request->get('date_of_shot');
    $booking->time = $request->get('time');
    $booking->shot_number = $request->get('shot_number');
    $booking->isDone = 0;
    $booking->isCancelled = 0;
    $booking->user_id = $user->id;

    $booking->save();

    $booking = new BookingHasVaccinationCenters();
    //here below I want to get the auto increment id
    $booking->booking_id->id;
    $booking->vac_center_id = $request->get('vaccination_center');
    $booking->save();

    return redirect('/home');
}

This is the error that I had last time when I try to do this:

Attempt to read property "id" on null

Upvotes: 0

Views: 272

Answers (3)

Fernando Balta Jhong
Fernando Balta Jhong

Reputation: 327

Your error is that you declare with the same name the variable $booking , when you save the $booking you should declare a instance of object with other name for example

$bookingvaccine->booking_id = $booking->id;

Upvotes: 1

matiaslauriti
matiaslauriti

Reputation: 8112

I know your questions has already been answered, but let me share another way of doing what you are doing, so you prevent this errors and your code is better.

If you have relations between this tables/models (relations functions created) then you can use the relation to create new models between them, without the need of sharing or passing the parent model's ID.

Assuming your User's relation name with Booking is bookings and for Booking -> BookingHasVaccinationCenters relation (strange name) is bookingHasVaccinationCenters, you should be able to do this:

public function addAppointment(Request $request)
{
    $booking = $request->user()
        ->booking()
        ->create([
            'vac_center_id' => $request->input('vaccination_center'),
            'vac_id' => $request->input('vaccination_id'),
            'date_of_shot' => $request->input('date_of_shot'),
            'time' => $request->input('time'),
            'shot_number' => $request->input('shot_number'),
            'isDone' => false,
            'isCancelled' => false,
        ]);

    $booking->bookingHasVaccinationCenters()->create([
        'vac_center_id' => $request->input('vaccination_center'),
    ]);
    
    return redirect('/home');
}

Another super small tip, remember to cast isDone and isCancelled to boolean, so you can use those fields as boolean so you can do true or false instead of 1 or 0.

And last tip, try to always stick to the Laravel's conventions: snake_case column names, isDone and isCancelled should be is_done and is_cancelled.

Upvotes: 0

Pradeep
Pradeep

Reputation: 9707

instead of this

    $booking = new BookingHasVaccinationCenters();
    //here below I want to get the auto increment id
    $booking->booking_id->id;
    $booking->vac_center_id = $request->get('vaccination_center');
    $booking->save();

use below code

   $bookingHasVaccination = new BookingHasVaccinationCenters();
    //change here 
    $bookingHasVaccination->booking_id = $booking->id;
    $bookingHasVaccination->vac_center_id = $request->get('vaccination_center');
    $bookingHasVaccination->save();

Note : always try to define variable with the name same as model class while crud operations

Upvotes: 1

Related Questions