-2

I am working on a query in Laravel 8 project for retrieving company details. Below is the structure and MySQL query.

# Getting company param from request.

SELECT c.company_name, p.position_name, s.salary
FROM tbl_company as c
JOIN tbl_company_type as t ON t.id = c.company_type_id
JOIN tbl_company_position as p ON p.company_id = c.id
JOIN tbl_comapny_salary as s ON s.position_id = p.id
where c.id = 1

Table structure:

tbl_company_type
id
type
description
tbl_company
id
company_type_id
company_name
description
tbl_company_position
id
position_name
company_id
tbl_comapny_salary
id
position_id
salary

Company Models

 namespace App\Models;

 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
protected $table = 'tbl_company';

public function listtype()
{
    return $this->hasOne(Type::class, 'id', 'company_type_id');
}

 public function listposition()
{
    return $this->hasOne(Type::Position, 'company_id', 'id');
}
}


namespace App\Models;

 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;

 class Type extends Model
 {
  protected $table = 'tbl_company_type';
 }



 namespace App\Models;

 use Illuminate\Database\Eloquent\Factories\HasFactory;
 use Illuminate\Database\Eloquent\Model;

 class Position extends Model
 {
 protected $table = 'tbl_company_position';

 public function listsalary()
 {
    return $this->hasOne(Type::Salary, 'position_id', 'id');
 }
 }


  namespace App\Models;

  use Illuminate\Database\Eloquent\Factories\HasFactory;
  use Illuminate\Database\Eloquent\Model;

  class Salary extends Model
  {
  protected $table = 'tbl_comapny_salary';
  }

Retrieving comany details using relation

   $companydetails = Company::with('listtype', 'listposition','listsalary')- 
  >whereRaw("tbl_company = 2")->get();

Getting this error

message": "Call to undefined relationship [listsalary] on model [App\Models\Position]

halfer
  • 19,824
  • 17
  • 99
  • 186
deepu sankar
  • 4,335
  • 3
  • 26
  • 37
  • 3
    Not sure, but, i think the error is coming because the relationship listsalary is declared on the Position Model and you are trying to access it from the Company Model – Kevin Jun 24 '21 at 12:26
  • 3
    But I think you're calling the relationship method in the Company model, if you want to load that, I think you have to rewrite like ``$companydetails = Company::with('listtype', 'listposition.listsalary')- >whereRaw("tbl_company = 2")->get();``, since the Position is the one with that relationship – Héctor William Jun 24 '21 at 12:29

1 Answers1

0

Your company model doesn't have any listsalary relationship.


class Company extends Model
{
    protected $table = 'tbl_company';

    public function listtype() 
    {
        return $this->hasOne(Type::class, 'id', 'company_type_id');
     }

    public function listposition()
    {
        return $this->hasOne(Type::Position, 'company_id', 'id');
    }
}

When you are calling Company::with() you are calling the relations within that Company Model only. To access listsalary you can do


   $companydetails = Company::with('listtype','listposition','listposition.listsalary')- 
  >whereRaw("tbl_company = 2")->get();

// or 

   $companydetails = Company::with([
      'listtype',
      'listposition' => function ($query) { 
          $query->with('listsalary'); // $query is your listposition relation
     }

   ])
  ->whereRaw("tbl_company = 2")->get();

Should definitely have a better naming convention.

Aryan Ahmed Anik
  • 466
  • 1
  • 9
  • 23
  • Sidenote, you don't need both `'listposition', 'listposition.listsalary'`, just `'listposition.listsalary'`; that will include `listposition` and `listsalary` :) – Tim Lewis Jul 05 '21 at 19:40