Voyager: How do I properly output data from a field with a relationship using blade?

Created on 18 Feb 2017  路  7Comments  路  Source: the-control-group/voyager

Hey, guys.

I am having a problem trying to call a data field with a relationship using blade. I have followed the steps mentioned in the documentation (https://the-control-group.github.io/voyager/docs/v0.10/#core-concepts-bread-relationships) still with no luck. I did exactly as instructed, but on my 'posts' table.

In a page I have a blade @foreach that displays the post fields. All the fields output data as expected, except the post author. I am calling the field using {{ $posts->authorId()->name }} and I get the following error:

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name (View: /home/anderson/atom-projects/planeta-gundam/resources/views/blog/index.blade.php)

Am I doing something wrong? I'm using the latest version of Voyager (0.10.15) and Laravel 5.3. Thanks.

screenshot_20170218_003717

question

Most helpful comment

I was way off track with this. I wanted to do this on my front end, not in my admin. After reading the documentation, I created two functions in my Post model:

public function author()
{
      return $this->belongsTo(User::class);
}

public function category()
{
      return $this->belongsTo(Category::class);
}

Then on my front end template I called {{ $post->author->name }} and... Voila! it worked.
I assumed that what the documentation showed would also work on the frontend, but its sole purpose is to make stuff come up in the dropdowns in the admin. Lesson learned. Thanks for your help.

All 7 comments

Please read the Laravel documentations about relationships.

{{ $post->authorId()->first()->name }}

I was way off track with this. I wanted to do this on my front end, not in my admin. After reading the documentation, I created two functions in my Post model:

public function author()
{
      return $this->belongsTo(User::class);
}

public function category()
{
      return $this->belongsTo(Category::class);
}

Then on my front end template I called {{ $post->author->name }} and... Voila! it worked.
I assumed that what the documentation showed would also work on the frontend, but its sole purpose is to make stuff come up in the dropdowns in the admin. Lesson learned. Thanks for your help.

i don't think you need to add the custom functions now you can $post->authorid->name simply now

tried but says
Trying to get property of non-object

This issue has been closed for 8 months now and was for a much older version. Please open your own issue if you're having trouble

I have this problem too, I am new in voyager 1.0 and laravel 5.5.*, I solved in this way

in web.php

Route::get('/posts', function () {
$posts = TCG\Voyager\Models\Post::all();
return view('posts', compact('posts'));
});

Route::get('post/{slug}', function($slug){
$post = TCG\Voyager\Models\Post::where('slug', '=', $slug)->firstOrFail();
return view('post', compact('post'));
});

and use in view

{{ $post->authorId()->first()->name }}

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kevinjon27 picture kevinjon27  路  3Comments

wp-src picture wp-src  路  3Comments

rayqiri picture rayqiri  路  3Comments

Nagendra1421 picture Nagendra1421  路  3Comments

craigb88 picture craigb88  路  3Comments