Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel belongsTo relationship not returning values

Im trying to run a hasMany but i really dont know why i dont get any result! two tables : Company , Joborder, every joborder has a company name and address that i want to get using belongsTo relationship in laravel

<?php
namespace App;

class Company extends Model
{
    protected $table      = 'company';
    protected $primaryKey = 'company_id';

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

My Joborder Page

<?php
namespace App;

class Joborder extends Model
{
    protected $table      = "joborder";
    public    $primaryKey = "joborder_id";
    protected $dates      = [
        'start_date',
        'created_at',
        'updated_at'
    ];

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

this is PageController:

public function joblist() {
    $joblist     = Joborder::all();
    $newInstance = new Joborder;
    $company     = $newInstance->company();

    dd($company->name);
    dd($joblist);

    return view('pages.joblist')->with('jobs',$joblist);
} 

this is a dd from my page after running

BelongsTo {#217 ▼
  #foreignKey: "company_id"
  #otherKey: "company_id"
  #relation: "company"
  #query: Builder {#218 ▶}
  #parent: Joborder {#205 ▼
    #table: "joborder"
    +primaryKey: "joborder_id"
    #dates: array:3 [▼
      0 => "start_date"
      1 => "created_at"
      2 => "updated_at"
    ]
    #guarded: array:1 [▼
      0 => "id"
    ]
    #fillable: []
    #connection: null
    #keyType: "int"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: []
    #original: []
    #relations: []
    #hidden: []
    #visible: []
    #appends: []
    #dateFormat: null
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: false
    +wasRecentlyCreated: false
  }
  #related: Company {#212 ▼
    #table: "company"
    #primaryKey: "company_id"
    #guarded: array:1 [▼
      0 => "id"
    ]
    #fillable: []
    #connection: null
    #keyType: "int"
    #perPage: 15
    +incrementing: true
    +timestamps: true
    #attributes: []
    #original: []
    #relations: []
    #hidden: []
    #visible: []
    #appends: []
    #dates: []
    #dateFormat: null
    #casts: []
    #touches: []
    #observables: []
    #with: []
    #morphClass: null
    +exists: false
    +wasRecentlyCreated: false
  }
}

I have company_id in company as pkey and in joborder as fkey Thanks for any help!

like image 719
Pc Monk Avatar asked Jan 02 '23 00:01

Pc Monk


2 Answers

You should call

$company = $newInstance->company()->get();

instead of

$company = $newInstance->company();

in PageController:jobList.
Right now you are returing the relation itself by not the related data.

Alternatively you can access company as a property which should automatically return the related data instead of the relation itself:

$company = $newInstance->company;
like image 187
Tschitsch Avatar answered Jan 05 '23 15:01

Tschitsch


You need to specify both foreignKey and localKey in your both relation, because you don't have id as primary key

Company Model

public function jobs() {
     return $this->hasMany('App\Joborder','company_id','company_id');
}

Joborder Model

public function company(){
     return $this->belongsTo('App\Company', 'company_id', 'company_id');
}

Now fetch it like this

public function joblist() {
    $joblist = Joborder::with('company')->all();

    dd($joblist);

    return view('pages.joblist')->with($joblist);
} 

If you need to fetch selected field of company then you can specify it in eager loading $query->select('name', 'address');

public function joblist() {
    $joblist = Joborder::with(['company' => function($query){
                         $query->select('name', 'address');
               }])->get();

    return view('pages.joblist')->with($joblist);
} 

In blade you can access company data like this

@foreach($joblist as $job)
   @if($job->company)
      {{$job->company->name}}
      {{$job->company->address}}
   @endif
@endforeach
like image 29
rkj Avatar answered Jan 05 '23 16:01

rkj