5

I can't find a solution to a seemingly simple problem with passing related data to a view. I have a model 'Companies' that belongs to 'Regions'. I've created the belongsTo relationship in my Companies model

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
protected $table = 'companies';
protected $primaryKey = 'Comp_id';
protected $fillable = ['industrylink_id', 'region_id','companyname','contactno','regno','vatno','salaryroll','numemployees'];

public function employees(){
    return $this->hasMany('App\Models\Employee');
}

public function region(){
    return $this->belongsTo('App\Models\Region');

Also the hasMany relation in the Region model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Region extends Model
{
protected $table = 'regions';

public function companies(){
    return $this->hasMany('App\Models\Company');
}    
}

My show Controller passes the variable for the session row successfully to the view, but the moment I want to access the related table data with the following... (table 'region' with column 'region')

<p>Region: {{ $company->region->region }}</p> -->

...I get the error

"Trying to get property of non-object: (View:..."

I've changed my models location to app\Models and I've made all the necessary namespace changes in the models & controllers, as well as the autoload in composer.json. I did a composer dump & config:clear and all of the other mvc references work.

I did not create migrations for the related tables since the tables exist and have standard reference data in it. I assume the relation created in the model is sufficient?

I'd appreciate some assistance.

PDevH
  • 63
  • 1
  • 10
  • Have you ensured that the existing table foreign keys are declared in your Laravel Model relationship methods? I see in your `Company` model that you declared a `$primaryKey`. What's the foreign key that references a `region` in your `companies` table? Declare it in the 2nd parameter of the `belongsTo` method. Example: `$this->belongsTo('App\Models\Region', 'unusual_region_id')`; – Sandyandi N. dela Cruz Jan 25 '17 at 09:35
  • No, I kept it simple with the Regions table - it's just `id` – PDevH Jan 25 '17 at 11:35
  • But what is its reference field in the `companies` table? – Sandyandi N. dela Cruz Jan 25 '17 at 11:36
  • It's Region_id (with a capital R). Maybe we're onto something here. I suspect that the Laravel convention works with all lower case letters. I'll specify it as you suggested and revert. – PDevH Jan 25 '17 at 13:53
  • It works! Thanks so much @Sandy. Can you post your response as an answer so that I can accept it? I'm new here - don't know if I can accept your comment as answer. – PDevH Jan 25 '17 at 14:00
  • I'm glad to help. I posted an answer, @PDevH – Sandyandi N. dela Cruz Jan 25 '17 at 22:30

3 Answers3

2

Usually, you get this error when variable or relationship is empty, so make sure company is not empty and it has a region. If you're sure company is not empty, just check the relationship:

{{ empty($company->region) ? 'No region' : $company->region->region }}
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Nope, I have data in both tables. Echo'd your code in controller and it returns `No region`. I'm stumped. – PDevH Jan 25 '17 at 11:51
  • @PDevH this means some companies do not have region. – Alexey Mezenin Jan 25 '17 at 11:56
  • No, can't be that. I have several companies in the table and ALL of them have an unsigned integer value in the `region_id` column, linking to distinct regions. – PDevH Jan 25 '17 at 12:38
1

Have you ensured that the existing table foreign keys are declared in your Laravel Model relationship methods? I see in your Company model that you declared a $primaryKey. What's the foreign key that references a Region in your companies table? Declare it in the 2nd parameter of the belongsTo method. Example: $this->belongsTo('App\Models\Region', 'unusual_region_id');

-2

Have you tried to :

dd($company->region)

and see what data it return.

Gold-Banana
  • 25
  • 1
  • 4