0
votes

I can't get through in my code. I have error indicating that 'reservation_id' doesn't have a default value. This reservation_id of table reservation_participate_details is a foreign key from the parent table reservation's id column. I wonder if there is something to do with the hasMany or belongsTo thing. I am not yet familiar deeply on laravel.

The reservation table should/has many rows in the reservation_participate_details. The process is working before but when I changed a column (reservation_participate_id) of reservation_participate_details into reservation_id and point it as a foreign key of reservation's column id, it is now not working.

I have tried to put

    public function participateDetail()
    {
        return $this->hasMany('App\ReservationParticipateDetail');
    }

into the reservation(parent) table and

    public function reservation()
    {
        return $this->belongsTo('\App\Reservation');
    }

into the reservation_participate_detail table

I just need to save participate details into reservation_participate_details. Again, reservation table has many reservation_participate_details.

1
The error said that when you insert a row in reservation_participate_details table, you didn't set the value for reservation_id column.catcon
@catcon yes, I understand that idea. I just want to know what am I missing here. I am new to laravelMDB
@MDB can you please check in your table ? reservation_id is the primary key. so you have set AUTO_INCREMENT while creating the table ?Ankit vadariya
@MDB try adding reservation_id in corresponding model file if not present.coderLogs

1 Answers

1
votes

Since you have changed the names of foreign key columns (not followed laravel default convention), you need to define those column names in relationship functions.

reservation model

public function participateDetail()
{
    return $this->hasMany('App\ReservationParticipateDetail', 'reservation_id', 'id');
}

reservation participate details model

public function reservation()
{
    return $this->belongsTo('\App\Reservation', 'reservation_id', 'id');
}