Home > Software design >  How to get related records for a single record in Laravel 5.8?
How to get related records for a single record in Laravel 5.8?

Time:06-17

I have a registration table where the situation is that I have a table for months and a table for years. The relationship between months and registration is one-to-many and same is the case with years and registration like below:

//Here is the registration migration

public function up()
{
    Schema::create('registrations', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('father_name');
        $table->string('contact1');
        $table->string('contact2')->nullable();
        $table->string('address')->nullable();
        $table->integer('amount');
        $table->integer('day');
        $table->unsignedInteger('month_id');
        $table->unsignedBigInteger('year_id');
        $table->timestamps();
    });
}

Below is the Registration model:

class Registration extends Model
{
protected $fillable =
    ['name', 'father_name', 'contact1', 'contact2', 'address', 'amount',
        'day', 'month_id', 'year_id'
    ];

public function month()
{
    return $this->belongsTo(Month::class);
}

public function year()
{
    return $this->belongsTo(Year::class);
}
}

This is Month model:

class Month extends Model
{
public function registration()
{
    return $this->hasMany(Registration::class);
}
}

This is Year model:

class Year extends Model
{
public function registration()
{
    return $this->hasMany(Registration::class);
}
}

Now, when I want to show one registration record with its relationships like below, I get all the registration records instead of one.

public function show(Registration $reg)
{
    $registration = $reg::with('month', 'year')->get();
    return ['registration' => $registration];
}

When I used with function with modal name then I get all records correctly but when I use it even with the single instance of the modal, I still get all the related records which I don't want. I want to select the Month and Year related to the single instance of the registration.

Any help is appreciated in advance.

CodePudding user response:

This is due to ->get();

in $reg you have one instance but then you do but then you make a new request with ->get(); and get displays all records

Do it like this

public function show($regId)
{
    $registration = Registration::with('month', 'year')->findOrFail($regId);
    return ['registration' => $registration];
}

CodePudding user response:

You can do it like this:

public function show(Registration $reg)
{
    $reg->load(['month', 'year']);
    return ['registration' => $reg];
}

You can also remove model binding and use with() for eager loading.

public function show($id)
{
    $registration = Registration::with(['year', 'month'])
        ->firstWhere('id', $id);

    return ['registration' => $registration];
}

CodePudding user response:

It think you don't have specified the foreign_key in relation. Or you have to define the foreign_key by

class Registration extends Model
{
   protected $fillable = [
       'name', 'father_name', 'contact1', 'contact2', 'address', 
        'amount','day', 'month_id', 'year_id'
   ];

   public function month()
   {
       return $this->belongsTo(Month::class,'month_id');
   }

   public function year()
   {
       return $this->belongsTo(Year::class,'year_id');
   }
}

May be it will solve your problem.

CodePudding user response:

Actually the get() method will return an array of records that you can loop over and that's why you get them all. Have you tried using the first() method that will return exactly one record.

$registration = $reg::with('month', 'year')->first();

  • Related