Using relationship in custom view


December 1, 2020 07:35 AM

Hello, I'm a newbie with both Laravel and Voyager.

I have created a custom controller overriding the VoyagerBaseController for my Clients

And a custom view overrinding the read.blade.php view of Clients

Each clients has many Motscles

And each Motscles has many GoogleSERP

How can i display the 10 first GoogleSERP for each of my Clients' Motscles in my Clients read.blade.php view ?

Select One:

December 2, 2020 04:06 PM

Best Answer

Hi there Julien,

I think that what you need to do in this case is to threat it just as you would without having Voyager.

  • Define your relationship in your Laravel models as you would normally without Voyager

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Motscle extends Model
     * Get the comments for the blog post.
    public function googleserps()
        return $this->hasMany('App\Models\Googleserp');
  • Then Depending on the motorscles that your client is viewing you can get the relationship as follows in your controller:
$googleserps = App\Models\Motscle::find($MotscleID)->googleserps;

Then in your view you could loop through them

@foreach ($googleserps as $googleserp)

Hope that this helps!

Select One:

December 2, 2020 04:39 PM

yes it helps a lot...

i wasn't sure if redefining the relationship was the good way

Thank you.

Select One:

December 7, 2020 03:21 AM

Also make sure to eager load the relationship to avoid any n(+1) problems. Either by using ::with('googleserps') or lazily loading using $motscle->load('googleserps').

Select One:

Markdown Basics

DevDojo uses Markdown syntax for writing posts, commments, and other content throughout the site. Below you will find some commonly used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet

Bold & Italic

Italics *asterisks*

Bold **double asterisks**


Inline Code `backtick`

Code Block

Three back ticks and then enter your code blocks here.


# This is Heading 1
## This is Heading 2
### This is Heading 3
#### This is Heading 4
##### This is Heading 5

Here is an example of how each heading will looks

Heading 1

Heading 2

Heading 3

Heading 4
Heading 5


> type a greater than sign and start typing your quote.


You can add links by typing the alt text inside of [] and the link inside of (), like so:



You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the link inside of (), like so:

![alt text for image](

Embedding Codepens

You can also embed a codepen pen by writing the following:

{% codepen %}

You may also choose the default tabs you wish to show your pen by writing the default-tab like so: (default is result)

{% codepen default-tab=result,html %}

That's about it. It's time to start crafting your story.

Please login, or signup to write a response.