Livewire public function mount() not recieving parametter from route model binding - php

I am running into the following problem, I want to pass from one livewire controller a series of variables to another livewire controller which is in another view.
From what I've read the most efficient method would be to pass the parameter through the route (Route Model Binding), and receive it via the mount() method in the livewire controller.
But when receiving the parameter from the livewire controller, I get the following error. (Even though the parameter appears in the route)
Unable to resolve dependency [Parameter #0 [ <required> $prioridad ]] in class App\Http\Livewire\ResultadoConsultaPresupuesto
Controller that redirects to the required route by passing the variable
$this->validate($validationMoto);
return redirect()->route('resultado-presupuesto', ['prioridad' => $this->Prioridad]);
Second controller (receives the parameter)
class ResultadoConsultaPresupuesto extends Component{
public $Prioridad;
public function mount($prioridad){
$this->Prioridad = $prioridad;
}
public function render()
{
return view('livewire.resultado-consulta-presupuesto');
}
}
Route (web.php)
Route::get('/resultado-presupuesto/{prioridad}', [PageController::class, 'showResultadosPresupuesto'])->name('resultado-presupuesto');
I have already made
php artisan route:clear && php artisan route:cache
If instead of receiving the parameter in the mount function I receive it in the following way if it works, but I don't understand why the first one doesn't work
public function mount(){
$prioridad = \Route::current()->parameter('prioridad');
$this->Prioridad = $prioridad;
}

Looking at this you are using standard Laravel controller. So you should pass variable to Livewire component in your view
Something like:
<livewire.resultado-consulta-presupuesto :prioridad="$proridad" />
and before you need to set it it controller

Related

Laravel 8 Route not defined even route is defined

This error is comeup while i working on laravel 8
Route [product.details] not defined. (View: C:\xampp\htdocs\ecommerce\resources\views\livewire\shop-component.blade.php)
here is the route i made Route Page web.php
Route::get('/product/{slug}',DetailsComponent::class)->name('product.details');
this is line of view where i want that route View Page shop-component.balde.php
<a href="{{route('product.details',['slug'=>$product->slug]) }}" title="{{$product->name}}">
and this one is the Details Component code
namespace App\Http\Livewire;
use Livewire\Component;
class DetailsComponent extends Component
{
public $slug;
Public function mount($slug){
$this->slug = $slug;
}
public function render()
{
$product = Product::where('slug', $this->slug)->first();
return view('livewire.details-component',['product'=>$product])->layout('layouts.base');
}
}
I think this php code doesnot get route of product.details at the same time i create middleware file and define a different route in it this route is working in this page but not Product.details.
Kindly help me i am stuck from 1 week here
You can use
php artisan route:list
to show a list of registered routes - make sure it shows up there. If it doesn't, use
php artisan route:clear
to clear Laravel's route cache
It does not work because of your route. It's not write in the correct way. You have first to make your DetailsComponent::class is bracket then you have to give the name of the method you want to use in this class.
Route::get('/product/{slug}', [DetailsComponent::class, 'method name'])->name('product.details');

Lumen 7 controllers bug

I am using lumen 7 framework.
I had a bug. In the web.php file I put:
$router->get('/getAll/{param1:[0-9]+|2A|2B}/{param2:[0-9]+}', 'TestController#getAll');
So, in the TestController, I create the function like that :
public function getAll($param1, $parm2)
{
....
}
The isssue is :
Illuminate\Contracts\Container\BindingResolutionException: Unable to resolve dependency [Parameter #1 [ $param2 ]] in class App\Http\Controllers\TestController
I did some tests, I add this bloc to provider, but it didn't work.
$this->app->singleton(\Illuminate\Contracts\Routing\ResponseFactory::class, function() {
return new \Laravel\Lumen\Http\ResponseFactory();
});
Also, I checked if I had done something wrong in the web.php file but I changed the function in the controllers to :
public function getAll($param1)
{
$parm2=1;
....
}
and it works fine.
How can I fix this bug, because in the url I need the two params.
Thanks,
The issue was that the names in controller and in the route was not the same.
The names in the controller was a little different from the route.
I did a upgrade from lumen 5.6 to lumen 7.0 and I thinks since Lumen 5.8 the names must be the same.
In route, you're passing only one param that's why you're getting this error.
$router->get('/getAll/{param1:[0-9]+|2A|2B}/{param2:[0-9]+}/{param2:[0-9]+|2A|2B}/{param2:[0-9]+}', 'TestController#getAll');
In controller.
public function getAll($param1, $param2)
{
....
}

Laravel 5.5 Resource Controller and Dependecy Injection

I am working on a Laravel 5.5 application. When I use php artisan make:model SomeModel -mr it creates the model, migration and resource controller.
I've been noticed that some methods have by default only one parameter: the model:
public function show(SomeModel $someModel)
{
...
}
If you look into the $someModel variable it has an empty SomeModel object.
I was reading on Laravel Documentation that it looks like the Containers or Facades but I am not sure how to use this. Do you?
Edit 1:
I had my routes defined in routes/web.php as: Route::resource('users', 'UserController');
Now I had to define all the routes manually since automatic binding was not working:
Route::get('users', 'UserController#index');
Route::get('users/create', 'UserController#create');
Route::post('users', 'UserController#store');
Route::get('users/{user}/edit', 'UserController#edit', function(App\User $user) {});
Route::post('users/{user}', 'UserController#update', function(App\User $user) {});
Route::post('users/{user}/delete', 'UserController#destroy', function(App\User $user) {});
So, should I replace every resource controller route to manual routing like this?
The resource controller is expecting you to use route model binding. In your routes file, each route that corresponds to a controller action with an injected model will need to have a matching parameter.
For example:
Route::get('user/{user}', 'UserController#show');
Using the above route, the following controller action would receive a user instances that corresponds to the user ID passed as a URL parameter.
class UserController extends Controller
{
public function show(User $user)
{
...
}
}
The reason you're seeing an empty model now is that Laravel will just pass and fresh model to the controller if it is not bound to a route parameter. In other words, if you forget to bind the model in your routes file automatic injection will just give you a new instance.
Note that if you are using a route resource the resulting routes should already have the correct parameters
Route::resource('users', 'UserController');
You can run php artisan route:list to confirm that your actual routes are correct.
Your problem is your controller is expecting two parameters like below:
public function show($id, User $user)
if you try:
public function show(User $user)
it should work correctly.
In your route you are passing only a single param like:
user/{user}
So if you dd the first param it will display the number 1 but if you pass that
to the model it will return the corresponding user as per what id you pass in the route.
The reason your User model was returning an empty object was because there was no value passed to it.
Also make sure your route placeholder: /{user} matches the variable name in
the controller: public function show(User $user).
Hope this helps.
I too came across with the same problem.
If your model having two or more words, you have to use only small letters like $modeModel as $somemodel.
public function show(SomeModel $somemodel)
{
...
}

Laravel routing failing for controller

Its Laravel 5.
When the route.php contains this:
Route::get('/foo', function () {
return 'Hello World';
});
then the page shows with the text "Hello World".
However, as soon as I add this new line in route.php:
Route::get('/foo2', 'IndexController');
then the page show this error:
UnexpectedValueException in Route.php line 567: Invalid route action: [App\Http\Controllers\IndexController]
I previously created a controller with artisan which now looks like this:
class IndexController extends Controller
{
public function index()
{
echo 'test';
}
}
what am I doing wrong?
You have to specify wich method will be executed:
Route::get('/foo2', 'IndexController#index');
If you are using get method of Route. Normally first argument provided should be the url and second argument should be the method (there are other ways argument could be passed)
Route::get('/foo2', 'IndexController#index');
If you want to resourceful route . Normally first argument should be the resource name and the second argument should be RESTful controller name. (there are other ways argument could be passed).Example: photo is the resource name and PhotoController is the controller name.
Route::resource('photo', 'PhotoController');
in your case it should work this way
Route::resource('/foo2', 'IndexController');
or
Route::get('/foo2', 'IndexController#index');
so when you visit
yoursite.com/foo2
you will be displayed with IndexController index method
See reference more to learn laravel's restful resource controller
reference: https://laravel.com/docs/5.1/controllers#restful-resource-controllers
You need to specify the function inside the controller not just the controller:
Route::get('/foo2', 'IndexController#index');
You have to reference Controller#method as:
Route::get('/myroute', ['uses' => 'MyController#methodName']);

Laravel beforeFilter in controller on POST request throws non-object exception

I try to set some specific filter on all controller methods with:
public function __construct() {
$this->beforeFilter(function(){
//whathever
});
}
and it's working well on normal GET methods, problem occures when there is some POST method:
Route::post('settings/menu-order/{direction}', array(
'as' => 'setting.menu-order.move',
'uses' => function($direction) {
$controller = new CMSSettingsController();
return $controller->doMoveMenu($direction);
}));
after click in a button which send POST with $direction, I'v got
Call to a member function filter() on a non-object
in vendor/laravel/framework/src/Illuminate/Routing/Controller.php
protected function registerClosureFilter(Closure $filter)
{
$this->getFilterer()->filter($name = spl_object_hash($filter), $filter);
return $name;
}
If I use already registred filter it's working, so what's going on?
I have few controllers which need specific function todo before running controller methods, so I can't make global universal filter. Is there any other good solution?
The problem could be that you are calling the controller action directly instead of letting the Router do it for you. When the router tries to apply the filters, instead of applying them on the controller, it ends up attempting to apply them on the output of the doMoveMenu action - which, of course, is not a Controller object and has no method filter.
Instead, your route should look like this:
Route::post('settings/menu-order/{direction}', array(
'as' => 'setting.menu-order.move',
'uses' => 'CMSSettingsController#doMoveMenu'));
The reason you don't need to do the method call manually is that since your Route has a parameter in it and your method accepts a parameter, the Router will automatically pass the parameter into the action method. Additionally, since you are providing a method name as the uses value, Laravel knows that it has to instantiate the Controller and run the filters.

Categories