0

After trying out every solution I found on Google, I still cannot seem to access my relationship's data. I keep getting the following error:

 Trying to get property of non-object (View: /home/eneko/foundry/biome/resources/views/home.blade.php)

Here are the files that load for this route: Fruitu.php

<?php namespace Biome;

use Illuminate\Database\Eloquent\Model;

use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;


class Fruitu extends Model implements SluggableInterface {

use SluggableTrait;

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'fruitus';

public function toString() {
    return $this->izenburua;
}

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = ['izenburua', 'irudia', 'edukia'];

public function egilea() {
    return $this->belongsTo('Biome\User');
}

protected $sluggable = array(
    'build_from' => 'izenburua',
    'save_to'    => 'slug',
);

}

The controller function:

public function index()
{
    $fruituak = Fruitu::all();
    $fruituak->load('egilea');
    return view('home')->with(compact('fruituak'));
}

The loop in the blade template:

@foreach ($fruituak as $fruitu)
    <div class="fruitua">
        <a href="{{ route('fruitu.show', $fruitu->slug) }}">
            <h3>{{ $fruitu->izenburua }}</h3>
        </a>
        {{ $fruitu->egilea->name }}
        <p class="eduk">{{ $fruitu->edukia }}</p>
    </div>
@endforeach

Thanks in advance for your help!

Eneko
  • 61
  • 1
  • 5
  • Either the relationship is not setup correctly, or there is no related record. Are you absolutely sure there is a related User for every Fruitu? If any Fruitu doesn't have a related User, your code will produce this error. I'm assuming the line causing the error is `{{ $fruitu->egilea->name }}`. Does it help shed some light if you change it to `{{ $fruitu->egilea ? $fruitu->egilea->name : 'No name' }}`? If every record shows 'No name', can you show your table definitions? – patricus Apr 26 '15 at 07:49
  • Every $fruitu->egilea corresponds to the user id (if I echo $fruitu->egilea I get the user ID), and running dd($fruituak) returns data relationship as it should (all user data) – Eneko Apr 26 '15 at 21:41
  • Echoing $fruitu->egilea should not just give you the user id, it should show a json string with all of the user information (unless you've overridden the `toString` method on the `User` class, as well). – patricus Apr 26 '15 at 22:50
  • Hm, I haven't overriden this method, so what could it be the issue? Should I provide any additional info? – Eneko Apr 27 '15 at 06:09
  • Can you show the two table definitions? – patricus Apr 27 '15 at 07:21
  • Found out the solution, both relationships required a parameter defining the table column. Thanks for your help! – Eneko Apr 28 '15 at 09:00
  • The extra parameters are not needed if your field names follow Laravel's conventions. That's why I was asking for the table definitions. Glad you got it figured out, though. – patricus Apr 28 '15 at 18:21

0 Answers0