boldsinas101
boldsinas101

Reputation: 330

Trying to get property of non-object Laravel 5,4

I'm new in Laravel. I just want to ask if my code is correct. I want to display the the name of the floor and building of the area from a certain assignment in index.blade.php in Collection folder. Collections belong to an Assignment, and Assignments belongs to an Area.

My code in Collection index.blade.php

@foreach ($collections as $collection)
  <tr>    
     <td>
         {{ $collection->assignment->area['floor'] }} Floor
         {{ $collection->assignment->area['building'] }}
     </td> 
  <tr>
@endforeach

That code generates an error "Trying to get property of non-object". So I'm asking the correct way of displaying it.

Here are the models

Collection.php

public function assignment()
{
    return $this->belongsTo(Assignment::class);
}

Assignment.php

public function collections()
{
    return $this->hasMany(Collection::class);
}
public function area()
{
    return $this->belongsTo(Area::class);
}

Area.php

public function assignments()
{
    return $this->hasMany(Assignment::class);
}

CollectionsController.php

public function index() //shows the table of the collections
{
    $collections = Collection::all();
    $disposals = Disposal::all();
    return view('collections.index', compact('collections', 'disposals'));
}

Upvotes: 0

Views: 824

Answers (4)

HSLM
HSLM

Reputation: 2022

"UPDATE": The problem is from the unclosed tr tag you opened it <tr><tr> like this but it should be <tr></tr> :D that's it, check the solution and tell me if it works. but look at the bright side, your code is cleaner and more pragmatic now :D

chaining is bad :), try to create a function that gets you the area or else...

or you can use withDefault and return a new instance of the related model

also, your database scheme should look like this:

collections

  • id
  • assignment_id

assignments

  • id
  • area_id

laravel withDefault doc

but for your example I could do:

Collection.php

public function assignment()
{
    return $this->belongsTo(Assignment::class)
        ->withDefault();
}

Assignment.php

public function collections()
{
    return $this->hasMany(Collection::class);
}
public function area()
{
    return $this->belongsTo(Area::class)
        ->withDefault();
}

Area.php

public function assignments()
{
    return $this->hasMany(Assignment::class);
}

CollectionsController.php

public function index() //shows the table of the collections
{
    $collections = Collection::all();
    $disposals = Disposal::all();
    return view('collections.index', compact('collections', 'disposals'));
}

also for usage:

@foreach ($collections as $collection)
  <tr>    
     <td>
         {{ $collection->assignment->area->floor }} Floor
         {{ $collection->assignment->area->building }}
     </td> 
  <tr/>
@endforeach

if everything is ok, and still no result, please check the data itself if it is exists, so check if there is any assignment for the collection, or any area for the assignment returned...

Upvotes: 1

Gautam Patadiya
Gautam Patadiya

Reputation: 1412

Might be chance like all records doesn't have connected data into subtable in the case error will rise. To overcame error you can use optional() method.

Method will auto handle the null relation.

optional($collection->assignment)->area

For more information you can check this link Good Luck

Upvotes: 0

lagbox
lagbox

Reputation: 50541

It is entirely possible that nearly any part of the following could be returning a null:

$collection->assignment->area

If the relationship from Collection to Assignment doesn't exist for this particular Collection there would be a null returned for it. This would go for all singular relationships down the line.

You will need to check for these, figure out how to return some default in the case of null, or guarantee that the relationship always exists in the DB.

Upvotes: 0

Abdi
Abdi

Reputation: 608

you should use area like this

$collection->assignment()->area()->building

Upvotes: 0

Related Questions