0

I am getting an error

Trying to get property of non-object (View: C:\xampp\htdocs\laravel\proj\resources\views\mycases.blade.php)

I have defined a relationship between two models Ccase and Hiring.

public function hirings()
{
    return $this -> hasMany('App\Hiring', 'case_ID')->orderBy('id','desc');
}

and paginating the results using a method below

public function getHiringsPaginateAttribute($perPage)
{
    return $this->hirings()->paginate($perPage);
}

The other model 'Hiring' has a method to define relationship with Ccase as follows:

public function ccase() 
    {

        return $this->belongsTo('App\Ccase', 'id');  
    }

In my controller, I have following code:

if(isset($search_term))
{
    $search_term = preg_replace('/\s+/', ' ', $search_term);
    $search_term = trim($search_term);
    if (strlen($search_term) > 0 && strlen(trim($search_term)) == 0)
        $search_term = NULL;

    $search_terms = explode(' ',$search_term);

    $fields = array('id', 'title', 'case');

    $hirings = $hirings->whereHas('ccase', function($q) use ($search_terms, $fields){
            foreach ($search_terms as $term)
            {
                foreach ($fields as $field)
                {
                   $q->orWhere($field, 'LIKE', '%'. $term .'%');
                }

            }
    });

 }

$hirings = $hirings->getHiringsPaginateAttribute($results_per_page);

In mycases.blade.php, my code is

{{$hiring->ccase->id}}

This line is throwing the above said error while the output of {{$hiring->ccase}} is:

{"id":1,"case":"HI this is a sample case i am putting just for test.","created_at":"2015-02-22 11:54:09","updated_at":"2015-02-22 11:54:09"}

What might be wrong with the code?

mininoz
  • 5,908
  • 4
  • 23
  • 23
Hassan Saqib
  • 2,597
  • 7
  • 28
  • 51
  • You're probably in a loop and at least one of the hirings doesn't have a ccase assigned. – lukasgeiter Apr 13 '15 at 08:28
  • My problem was with `public function ccase() { return $this->belongsTo('App\Ccase', 'id'); }` and I should be like `public function ccase() { return $this->belongsTo('App\Ccase', 'case_ID', 'id'); }` – Hassan Saqib Apr 13 '15 at 08:49

2 Answers2

0

Unfortunately, you can't use related models in views. Here's the detailed explanation why.

Your case can be solved by specifying the name of the associated column on the parent table:

return $this->belongsTo('App\Ccase', 'ccaseId', 'id');

In model Hiring, it will be like

public function ccase() 
{
    return $this->belongsTo('App\Ccase', 'case_ID', 'id'); 
}

And now in view use it like this:

{{ $hiring->ccase->id }} 
Community
  • 1
  • 1
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
  • Thanks @limonte... I solved it by modifying the relation in Hiring `public function ccase() { return $this->belongsTo('App\Ccase', 'case_ID', 'id'); }` Is it a right approach? – Hassan Saqib Apr 13 '15 at 08:42
  • @HassanSaqib why not, even better! I'll amend my answer acording to your comment – Limon Monte Apr 13 '15 at 08:51
0

Not sure, but i think that you could use relations in view, you should use an eager loading in controller where you are quering for $hirings, just add a:

with(['hirings.ccase'])

Could you please provide a peace of code from controller where you make a query to Hiring model for clear?