I have a simple app for generating some code using Laravel, now I am trying to pass unique id to to my controller with the url.
This is this method that should receive the ID:
public function code($code_id)
{
$settings = Setting::find($code_id);
return view('pages.settings.code', compact('settings'));
}
And this is my view-file where I pass the ID:
<a href="{{ route('settings.code', $settings->code_id) }}">
{{ __('Generate Code') }}
</a>
When I check the URL I get:
http://127.0.0.1:8001/settings/code?K1zMXRZG4
Here is my route:
Route::get('settings/code', [
'as' => 'settings.code',
'uses' => 'SettingController#code'
]);
Route::resource('settings', "SettingController");
But I get the following error:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR)
Too few arguments to function App\Http\Controllers\SettingController::code(), 0 passed and exactly 1 expected
What is wrong with my code?
You need to define any parameters that should be passed to the method in your route:
Route::get('/settings/code/{code_id}', 'MyController#code')
->name('settings.code');
That will pass $code_id to the code()-method in your controller.
If you donĀ“t define the parameter in your route, it will be passed as a parameter in Request-object instead - like all querystrings - and you need to fetch it like this in your controller:
$code_id = $request->query('code_id');
Related
I have this route:
Route::resource('riskfields', RiskfieldsController::class)->except('show');
Within the RiskfieldController/edit I'm calling the edit view as:
public function edit(Riskfield $riskField)
{
return view('riskfields.edit', [
'riskField' => $riskField
]);
}
where Riskfield is the model.
So, inside edit.blade.php I have:
{{ Form::open(['route' => ['riskfields.update', $riskField->id], 'method' => 'put', 'autocomplete' => 'off']) }}
when I access to this endpoint: /admin/riskfields/1/edit
I get:
Missing required parameter for [Route: riskfields.update] [URI: admin/riskfields/{riskfield}] [Missing parameter: riskfield]. (View: /var/www/html/resources/views/riskfields/edit.blade.php)
The problem's that I have inject the model, so I don't know what's happening there.
This is the update method:
public function update(Riskfield $riskField, Request $request): RedirectResponse
Someone could help?
UPDATE:
dd ouput:
This problem stopped me for half an hour, but I hope my answer will help someone. Essentially I have to change this:
public function edit(Riskfield $riskField)
into:
public function edit(Riskfield $riskfield)
The parameter must have the same name convention defined within the route, so I've executed php artisan route:list and it was admin/riskfields/{riskfield}
Hope to help someone.
I have a problem with the Laravel site's form, when I send the form through the site it shows an error, using devTools it is noted that it is an error 500 of the Post method, but the form is sent anyway.
the following error appears in the laravel log file
[2021-12-15 11:27:49] production.ERROR: Missing required parameters for [Route: contato] [URI: {lang}/contato]. {"exception":"[object] (Illuminate\Routing\Exceptions\UrlGenerationException(code: 0): Missing required parameters for [Route: contato] [URI: {lang}/contato]. at /home/corstonecom/public_html/vendor/laravel/framework/src/Illuminate/Routing/Exceptions/UrlGenerationException.php:17)
In the form view it like this:
<form id="frm-contato" class="site-form" action="{{ route('contato-enviar', app()->getLocale()) }}" method="post">
In the routes file web.php it like this:
Route::view('/contato', 'fale-conosco')->name('contato');
Route::post('/contato', 'HomeController#enviarContato')->name('contato-enviar');
and in the controller it like this:
public function enviarContato(EnviaContatoRequest $request)
{
$inputs = $request->all();
$inputs['localidade'] = $inputs['cidade'] . '/' . $inputs['uf'];
$contato = Contatos::create($inputs);
Lead::fastSave([
'name' => $inputs['nome'],
'email' => $inputs['email'],
]);
Mail::send(new FaleConosco($contato));
Session::flash('contato_enviado', 'sucesso');
return redirect()->route('contato');
}
Where am i going wrong?
You would need to pass the lang parameter when calling the route method when doing the redirect in your Controller method as the route contato requires the lang parameter:
return redirect()->route('contato', ['lang' => app()->getLocale()]);
It is best to get in the habit of using an associative array for the parameters as any more than 1 parameter and they will need to be in an associative array for the URL generator to match them up.
If you don't want to have to constantly be passing this app()->getLocale() value to the URL helpers you can set a default for the lang parameter and it will be added for you. You could use a middleware to do this. Example of the functionality of setting a default value for a parameter:
public function handle($request, $next)
{
\URL::defaults([
'lang' => app()->getLocale(),
]);
return $next($request);
}
Now you don't have to pass the parameter for lang when generating URLs as it has a default value set.
If you already have a "locale" middleware that is handling the locale you can add this to it.
When you pass parameters to the route, you should pass it as an array with keys, so that Laravel knows what the parameter is. In this case, you would do
<form id="frm-contato" class="site-form" action="{{ route('contato-enviar', ['lang' => app()->getLocale()]) }}" method="post">
See https://laravel.com/docs/8.x/routing#generating-urls-to-named-routes
Can anyone please help me to pass multiple parameters in GET method, I have following codes -
in blade -
#if(!isset($model->id_car_type))
<a class="btn btn-search-red" href="{{ route('frontend.model', [Request::segment(2),$model->year,$model->niceName]) }}">Select</a>
#else
<a class="btn btn-search-red" href="{{ route('frontend.model', array(Request::segment(2),$model->year,$model->niceName, $model->id_car_type)) }}">Select</a>
#endif
In route -
Route::get('/model/{make}/{year}/{niceName}/{type?}', 'GeneralController#trimShowByNiceName')->name('frontend.model');
But it is throwing error -
Missing required parameters for [Route: frontend.model] [URI:
model/make/{make}/year/{year}/niceName/{niceName}/{type?}]
To pass parameters in a route use an array with the paramter names as keys:
{{ route('frontend.model', ['make' => 'Ford', 'year' => 1988, 'niceName' => 'Ford Escort']) }}
https://laravel.com/docs/5.7/routing#named-routes
In Laravel 5.2 use the following example:
In the view.blade.php
Page
In the route.php
`Route::get('users/{id}{name}', 'UsersController#searchUsers'); // this gets the id, name` from the view url.
In the controller method, pass the parameters as function parameters as follows:
public function searchUsers($id, $name)
{
// your code here that use the parameters
}
I am using this module .
I defined my Breadcrumbs, and now I'm trying to render in my blade template by doing the following :
{!! Breadcrumbs::render($breadcrumbs) !!}
The value of $breadcrumbs being "controlled" by my controller.
The problem is that I would like to be able to pass an array of arguments to this render() method, and not only simple strings. Indeed, here are some Breadcrumbs I declared :
Breadcrumbs::register('home', function($breadcrumbs)
{
$breadcrumbs->push('Home', route('home'));
});
/* .... etc .... */
Breadcrumbs::register('style', function($breadcrumbs, $style_name, $style_slug)
{
$breadcrumbs->parent('styles');
$breadcrumbs->push($style_name, route('style', $style_slug));
});
In this situation, I need to be able to pass an array of arguments to the render() method, which will be sent by the Controller to the View.
I tried the following :
{!! call_user_func_array(Breadcrumbs::render, $breadcrumbs) !!}}
But I get the following error :
Undefined class constant 'render'
This module has a renderArray() method. Next time, I'll read the documentation till the end :)
Form :
{{ Form::open(array('url' => 'user/create', 'files' => true)) }}
Route :
Route::resource('user', 'UserController');
UserController.php
class UserController extends BaseController {
public function index()
{
return 'hi11';
//return View::make('home.index');
}
public function create()
{
return 'hi22';
//return View::make('home.index');
}
}
This code gives
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
I'd just like to add my own discovery along these lines... Maybe this will save someone else the head-scratching I just performed.
I too implemented the Route::resource mechanism. I couldn't figure out why my create was working but my update was not. It turns out you can't reuse the same form code exactly, the form that does an update must use the method PUT or PATCH. Why update couldn't be a POST is beyond me.
That is to say, the opening form tag for an update must look like this:
Form::model($thing, array(
'method' => 'PUT',
'route' => array('things.update', $thing->id)
)
Without specifying method => PUT, you get this not-helpful error.
Because in your roures you use resourse controller, you can use only specific paths and actions, described in documentation http://laravel.com/docs/controllers#resource-controllers.
user/create ( UserController::create ) is where you need to show the form for adding a new user.
The actual storage of the user should be done in user/store i.e. your form must be send data to UserController::store() method.
In your case if you POST your form only to 'url' => 'user', this should automatically send data to the correct method.
Laravel 4 resources have named routes - just use those:
{{ Form::open(array('route' => 'user.create', 'files' => true)) }}
this is how I'm doing it, it might help someone, can be improved, but this would be main idea.
#if(isset($data))
{{ Form::open(['route'=>['blog.update', isset($data) ? $data->slug : null],'method' => 'patch','role' => 'form', 'class' => 'blog-form form-horizontal']) }}
#else
{{ Form::open(['route'=>'blog.store','role' => 'form', 'class' => 'blog-form form-horizontal']) }}
#endif