0
votes

I need a little clarification.

This error message "Trying to get property of non-object" to what is referred to when using Eloquent? I'm going crazy. I am using a function:

public function squadra_post()
{
return $this->hasMany('Squadra','id_squadra');
}

that is the model that extends Eloquent Post. When the call:

<?php $squadra = Post::find($roles->id_squadra)->squadra_post; ?>

gives me the error that I mentioned before.

EDIT

his is the class post with their methods. What to give problems is "squadra_post ()" I need to extract the name of the team by id_squadra post content in table.

class Post extends Eloquent implements UserInterface, RemindableInterface 
{
    protected $table = 'post';
    protected $primaryKey = 'id_post';
    public $timestamps = false;

    public function commenti() 
    {
        return $this->hasMany('Commento','id_post');
    }

    public function apprezzamenti() 
    {
        return $this->hasMany('Apprezzamenti','id_post');
    }   

    public function squadra_post() 
    {
        return $this->hasMany('Squadra','id_squadra');
    } 
}

This is the code of the class "squadra"

class Squadra extends Eloquent implements UserInterface, RemindableInterface 
{
    protected $table = 'squadra';
    protected $primaryKey = 'id_squadra';
    public $timestamps = false;
}

And finally, this is the code that gives me problems. That is what gives me the error: Trying to get property of non-object

@foreach($post['post'] as $roles)
    @if($roles->id_squadra != 0)
        $squadra = Post::find($roles->id_squadra)->squadra_post;
        @foreach($squadra as $squadra_post)
           {{ $squadra->nome }}
        @endforeach
    @endif
@endforeach
3

3 Answers

0
votes

When you tired attempting to find the particular post, it couldn't find it and hence it is throwing an exception saying "Trying to get property of non-object". If it had found the post, it would have returned the squadra_post and it would have been stored in the variable.

But instead it could not find that particular and could not construct an object with it and hence it could not find that particular property name squadra_post. Therefore the error "Trying to get property of non-object".

You could handle this exception by putting the code in a try and catch block.

0
votes

In the line

$squadra = Post::find($roles->id_squadra)->squadra_post;

the squadra_post() method

returns a collection of objects (posts I guess) and not an object because of the nature of your relationship (1 to many) while Post::find() returns the object. I think this is where the error occurs.

try the following code

 $squadra = Post::find($roles->id_squadra); 

 if ($squadra) {
   echo $squadra->name;

  foreach ($squadra->squadra_post as $post) {
       echo $post->title;
       echo $post->content; //etc
  }
}

EDIT

Also you'll have to be sure that the foreign key is the id_squadra that you declare in your squadra_post() method.

0
votes

First off, your relation won't work this way, becuase Laravel 4+ expects relation names to be camelCased and only then you can use dynamic properties.

So rename it to squadraPost()and call like $post->squadraPost, and in case of hasMany you will get a Collection here, no matter if there is any related model or not.

Next thing is checking if related model exists - for this read the answer here: Laravel check if related model exists

And last, but not least: you may get null from find() method, so first make sure this:

$post = Post::find(..)

returns your model, not null. Only then can you call its relation:

if ($post) $post->squadraPost;