[Vuejs]-How to correctly use relationships with() in Laravel?

0👍

Your relationships parameters are wrong. It’s

hasMany(class, foreignKey, relatedPrimaryKey)
# Employee
public function designations()
{
    return $this->hasMany(Designations::class, 'employee_id', 'id');
}

public function positions()
{
    return $this->hasMany(Positions::class, 'employee_id', 'id');
}

If you’re eager loading more than 1 relationship, use array notation.

Also, $employees will be an instance of a Collection, so you can’t use it as an argument to array_reverse.

You can either use collection methods to achieve the same result, or use $employees->all() to get the underlying array.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Employees;

class EmployeesController extends Controller
{
    public function index()
    { 
        $employees = Employees::with(['designations', 'positions'])->get();

        return $employees->reverse()->values()->all();
        // OR
        return array_reverse($employees->all());
    }
}

This is assuming your tables have a structure like this:

Schema::create('employees', function (Blueprint $table) {
    $table->id();
    ...    
});

Schema::create('designations', function (Blueprint $table) {
    $table->id();
    $table->foreignId('employee_id')->constained('employees');
    ...
});

Schema::create('positions', function (Blueprint $table){
    $table->id();
    $table->foreignId('employee_id')->constained('employees');
    ...
});

Since you’re using increments instead of id(), the code has to be a little different.

Schema::create('employees', function (Blueprint $table) {
    $table->increments('id')->unique();
    ...    
});

Schema::create('designations', function (Blueprint $table) {
    $table->increments('id')->unique();
    $table->unsignedInteger('employee_id');
    $table->foreign('employee_id')->references('id')->on('employees');
    ...
});

Schema::create('positions', function (Blueprint $table){
    $table->increments('id')->unique();
    $table->unsignedInteger('employee_id');
    $table->foreign('employee_id')->references('id')->on('employees');
    ...
});

0👍

I would recommend you to install phpstorm, it gives you hints of function parameters and you won’t have this kind of issues anymore.

correct format is:

return $this->hasMany('App\Comment', 'foreign_key', 'local_key');

in your designations model:

public function DesignationNames()
    {
        return $this->hasMany(\App\Models\Employees::class, 'designation_id', 'id');
    }

When you retrieve them in your controller you need to use the with() method as:

Designations::with(‘DesignationNames’)->get();

And then to access properties in the related employee collection you would need to:

$designation->DesignationNames->DesignationProperty

Leave a comment