Routing problems

What seems to be the problem: Livewire mount seems to error out on me but diedumps fine.

Steps to Reproduce: Just a simple rendering the route parameter to the commponent.

Are you using the latest version of Livewire: Yes 2.4

Do you have any screenshots or code examples:

image

my counter.blade.php

<div style="text-align: center">
 <button wire:click="increment">+</button>
  <h1>{{$name}}</h2>
</div>

my route

Route::get('{name}', Counter::class);

Then gives me this error

Illuminate\Contracts\Container\BindingResolutionException
Unable to resolve dependency [Parameter #0 [ <required> $name ]] in class App\Http\Livewire\Counter (View: E:\wamp64\www\test\resources\views\layouts\app.blade.php)

Do you get the same error if you completely remove the mount method?

No, I think I found the problem, I’m using inline components, I think mounts only works on full page components? I want to use inline components and getting the param value, is there any way?

Either way, you don’t need to use the mount method like you would a constructor, whether it’s passing the parameter inline or through route model binding/route parameters. Livewire has a convention that if there’s a public property of that name, it will be assigned automatically, so you only need the mount method if there’s other logic to it.

Docs explain it better than I can on my phone. https://laravel-livewire.com/docs/2.x/rendering-components

This worked in rendering the page but it wont load the data, it works if I dd($banner)
but wont work if I’m calling it on the blade file
image

It’s a different project but same goal I’m just getting data from the database.

I didn’t read this right the first time, sorry for that. So to actually answer your question, there’s a few ways you can do that.

First, you can pass that route parameter through your controller into the view, and then pass it to livewire the inline way. Going this route, there’s no need for a mount method, the public $name property in your component will automatically be assigned what you pass it.

<livewire:counter :name="$name">

Another is to access the route parameter in your mount method. It’s generally not a good idea to use the request in livewire because it’s not going to give you the info you’re looking for unless you understand the livewire lifecycle, because it will be giving you the request details of the xhr request for each round trip. However the mount method is only called on the initial page load, so you can use it the way you would elsewhere in your laravel app, and it will work in the way you’re used to.

With this way, you wouldn’t pass it inline, just <livewire:counter />, then in your component:

public $name;

public function mount(Request $request)
{
    $this->name = $request->route()->parameter('name');
}

You can auto resolve from the container through DI like in the example, use the request helper, or use request facade; however you like to go about it. Just keep in mind that using it anywhere outside of the mount method is probably a bad idea.