Xanger's avatar

Retrieve page slug with livewire

I have a page where I'm implementing livewire, but I don't understand how to pass a variable from the controller to livewire, and that would be the page slug variable, where it goes to retrieve the data on the query:

Controller:

  public function getProfiloVote($slug)	{
    $profile =	XIPB_Core_Members::where('members_seo_name', $slug)->firstorfail();
    $staff	=	User::where('slug', $slug)->first();
    if (app('setting')['debug'] == 'false') {
      setLocation('Visualizzando il profilo di '. $profilo->name);
    }
    return view('front.pages.user.main', compact('staff','profilo'));
  }

Livewire:

class ProfileVote extends Component
{
    use WithPagination;
    public function paginationView()
    {
        return 'livewire.pagination';
    }
    public function render()
    {
        $item = VotoGiochi::where('id_utente', '1')
        ->paginate(10);
        return view('livewire.profile-vote', [
            'item' => $item
        ]);
    }
}

At User ID I have 1, and I would like to have them:

$profile->member_id

which goes to fetch from the controller, otherwise I could call the profile variable in the livewire, but how do I get the slug of the page in that case?

0 likes
2 replies
LaryAI's avatar
Level 58

To pass the page slug variable from the controller to Livewire, you can use Livewire's mount method. In the controller, you can pass the slug as a parameter to the Livewire component and then retrieve it in the mount method. Here's an example:

Controller:

public function getProfiloVote($slug) {
    $profile = XIPB_Core_Members::where('members_seo_name', $slug)->firstOrFail();
    $staff = User::where('slug', $slug)->first();
    if (app('setting')['debug'] == 'false') {
        setLocation('Visualizzando il profilo di '. $profilo->name);
    }
    return view('front.pages.user.main', compact('staff', 'profilo', 'slug'));
}

Livewire:

class ProfileVote extends Component
{
    use WithPagination;

    public $slug;

    public function mount($slug)
    {
        $this->slug = $slug;
    }

    public function paginationView()
    {
        return 'livewire.pagination';
    }

    public function render()
    {
        $profile = XIPB_Core_Members::where('members_seo_name', $this->slug)->firstOrFail();

        $item = VotoGiochi::where('id_utente', $profile->member_id)
            ->paginate(10);

        return view('livewire.profile-vote', [
            'item' => $item
        ]);
    }
}

In the Livewire component, we define a public property $slug and use the mount method to retrieve the slug passed from the controller. We can then use this slug to retrieve the $profile object in the render method and use its member_id property to retrieve the VotoGiochi records.

1 like
JabatoForever's avatar
Level 2

Yes as Lary said a prop would be the best option, what is passed as a param in the mount method is a prop in LW, now to then pass the variable to the component in your view just

<livewire:profile-vote :slug="$slug" />

remember to pass the $slug to your view in order to access it and pass it to your LW class. Here you can find the dedicated section in the docs : https://laravel-livewire.com/docs/2.x/rendering-components#parameters

Please or to participate in this conversation.