Closed AlexThousand closed 4 years ago
I'm using Laravel 7
Hi @AlexThousand
The correct method is this. {{ Widget::run('customMenu', [], $variable1, $variable2) }}
Could you check this, @widget('customMenu',[], ['page' => 'company'])
Its works, @AlexThousand?
It does not work with Laravel 7, i got the following exception:
@widget('recentNews', [], 'test')
Facade\Ignition\Exceptions\ViewException Unable to resolve dependency [Parameter #0 [ <required> $name ]] in class App\Widgets\RecentNews
But it's possible to passing the variables over the config array.
Getting same error as @gunmanix , when I try to pass variables to the widget in Laravel 7..
@widget('DisplayValues', [], $properties)
Unable to resolve dependency [Parameter #0 [
Only config variables work in laravel7..
I found a solution, but I am not sure if it is the way you intended to use these arguments. If I call the widget with:
@widget('recentNews.recentNews', [], 'Afganisthan')
You can access the parameter in the run method like this:
public function run()
{
$country = func_get_arg(0);
return view('widgets.recent_news.recent_news', [
'country' => $country,
]);
}
Was this the intended behavior? If so, then the README should be updated to reflect it.
The README states:
@widget('recentNews', ['count' => 10], 'date', 'asc')
...
public function run($sortBy, $sortOrder) { }
...
If this is the intended behavior, then this may help with discovering the problem.
When the parameters are sent to the LaravelApplicationWrapper::call() method, they look like this:
Array ( [0] => date [1] => sortOrder )
It then calls the Container::call() method for Laravel, and eventually tries to find the dependency in BoundMethod::addDependencyForCallParameter() method. Since the array does not have a key with the name 'sortBy' or 'sortOrder', it is unable to match up the dependency with your widget run method. So it throws the error in line 180.
One possible fix is to use an array instead:
@widget('recentNews', ['count' => 10], ['sortBy' => 'date', 'sortOrder' => 'asc'])
...
public function run($sortBy, $sortOrder) { }
...
However, I haven't found a way to implement this. In this situation, for some reason the array gets wrapped into an array, like this:
Array ( [0] => Array ( [sortBy] => date [sortOrder] => asc ) )
Which also cannot match up to the dependency. When I change the call method to this, it works:
return $this->app->call($method, $params[0]);
I hope this helps!
Hi!
I just send a PR to fix this issue by dynamically assigning the values to an associative array that will be used when calling the run method.
Unfortunately, Laravel has changed the behavior of method injection, so variables cannot be passed directly to run method in n Laravel >=7.
Please configure widgets using config array. Documentation is updated
Why is not works?
@widget('customMenu',['page' => 'company'])
Result:
Log.info: array('page' => null)