I have a form and I'm trying to pass null value into the $dev-id parameter but it doesnt work, instead ti will replace the dev id parameter with the $id as shown in my code at the bottom,
The reason i needed it to be null because I'm using this function for 2 different page but one of the page doesn't have $dev_id. any help is appreciated
HTML:
<form method="post"
action="{{ route('admin.developers.admins.edit.post',['admin' => $admin, 'dev_id' => null, 'id' => $id]) }}" autocomplete="off" novalidate>
web.php :
Route::post('developers/{dev_id?}/admins/{id}/edit', 'Admin\DeveloperAdminController#postEditDeveloperAdmin')->name('admin.developers.admins.edit.post')->where(['dev_id' => '[0-9]+', 'id' => '[0-9]+']);
You can pass those parametres in the form of hidden input and in controller use request to get those values
Add these to yur form
<input type="hidden" value="anything" name="dev_id">
Instead you are using vue use `v-model` instead of name and value
Now modify your form like this
<form method="post"
action="{{ route('admin.developers.admins.edit.post',['admin' => $admin,'id' => $id]) }}" autocomplete="off" novalidate>
Now you are access that using $request object in your logic and check if the value is null or not
Related
I'm trying to pass an array, and an ID as parameters to my controller function, but it's not working.
I tried multiple time with differents ways, but it's still not working.
view.blade.php
<form method="post" action="{{ route('clients.insert_clients', [$campagne->id, 'importData_arr' => $importData_arr, 'id_campagne' => $campagne->id]) }}">
web.php
Route::post('clients/importer/{campagne}', 'CampagneController#upload_clients')->name('clients.upload_clients');
CampagneController.php
public function insert_clients($importData_arr, $id_campagne)
The error I get
Too few arguments to function App\Http\Controllers\CampagneController::insert_clients(), 1 passed and exactly 2 expected
Any idea?
Change,
<form method="post" action="{{ route('clients.insert_clients', [$campagne->id, 'importData_arr' => $importData_arr, 'id_campagne' => $campagne->id]) }}">
To
<form method="post" action="{{ route('clients.insert_clients', ['campagne' => $campagne->id, 'importData_arr[]' => $importData_arr]) }}">
Change,
public function insert_clients($importData_arr, $id_campagne)
To,
public function insert_clients($campagne, Request $request)
You have a required parameter (campagne) as defined by your route so you need to pass it explicitly in that array of data.
I think you're trying to achieve you goal in a very messy way. First of all your route
Route::post('clients/importer/{campagne}', 'CampagneController#upload_clients')
You're declaring a single variable campagne but in your controller you're declaring the corresponding fuction as
public function insert_clients($importData_arr, $id_campagne)
and that's why you're getting the error, you're passing a single variable ($campagne), but the controller's method expects two variables ($importData_arr, $id_campagne).
You should update the method as follow
public function insert_clients($campagne)
as well as your form:
<form method="post" action="{{ route('clients.insert_clients', ['campagne' => $campagne->id]) }}">
#foreach($importData_arr as $value)
<input type="hidden" name="importData_arr[]" value="{{ $value }}" />
#endforeach
<!-- Other fields -->
After submitting the form, you can recover your data as follows:
public function insert_clients($campagne) {
$importData_arr = request()->get('importData_arr');
}
You need to define your route with 2 params instead of 1.
Route::post('clients/importer/{importData}/{campagne}', 'CampagneController#upload_clients')->name('clients.upload_clients');
I send file from my html form and dump my file in controller. Dump result return correct result with file name.
dump($request->file);
But if I check for true or false then var_dump() return false.
var_dump($request->hasFile('file'));
That's because $request->file is a string and not instance of Illuminate\Http\UploadedFile.
You should use ->hasFile() only with files.
I'm just adding another thing.
When you don't add enctype="multipart/form-data" inside your form then you will get also this types of problem.
<form action="{{ route('store') }}" method="POST" enctype="multipart/form-data">
Or using form helper 'file' => true,
{!! Form::open(['route' => ['store'], 'file' => true]) !!}
I think this will help someone.
i have an update form to update the product details :
<form action="{{ path_for('product.update', {id: product.id}) }}" method="get">
// another fields
<input type="hidden" name="_METHOD" value="PUT">
<input type="submit" name="submit" class="submit" value="update">
routes :
$app->get('/admin/product/edit/{id}', ['maimana\Controllers\AdminController','editupdateProduct'])->setName('product.edit');
$app->get('/admin/product/update/product/{id}', ['maimana\Controllers\AdminController','updateProduct'])->setName('product.update');
and the controller :
public function updateProduct($id, Request $request, Response $response){
$product = $this->product->where('id',$id)->first()->update([
'title' => $request->getParam('title'),
'category' => $request->getParam('category'),
'slug' => $request->getParam('slug'),
'description' => $request->getParam('description'),
'price' => $request->getParam('price'),
'image' => $request->getParam('image'),
'stock' => $request->getParam('stock')
]);
return $response->withRedirect($this->router->pathFor('admin.product'));
}
everytime i hit the update button, i cant pass the parameter is automatically and it turns PAGE NOT FOUND. but when i add the id manually it works and redirect back to admin.product.
please help me im getting stuck for about 4 days and i need this for my college task
Slim also uses Restful URLs it seems. When you have the _method set to 'put' in the form and sends the request, it will interpret it as a PUT request rather than a GET. Since this is your college task, I think I've helped you with 99% of the problem here.
I want to use the value of only one field of a form with two fields as a route parameter to a controller. What I achieve so far was only a mess of query string parameters appended to the url.
My form:
{{ Form::open(['route' => ['anuncio.especificar_tipo_imovel', $valorCep = 'valorCEp'], 'method' => 'GET']) }}
<input type="hidden" value="14405024" id="valorCep" name="valorCep"/>
<label for="tbCep"/>
<input autocomplete="off" id="tbCep" style="width:400px;" name="cep" type="text" />
</label>
<input type="submit" value="continuar">
{{ Form::close() }}
I have a route like this:
Route::get('anuncio/especificar_tipo_imovel/{valorCep}', [
'as' => 'anuncio.especificar_tipo_imovel',
'uses' => 'AnuncioController#especificar_tipo_imovel'
]);
and a action method like this
public function especificar_tipo_imovel(Request $request, $valorCep)
{
return view('especificar_tipo_imovel');
}
The value I want to send is the value of the hidden field: valorCep
I want a url like
http://my_route/34834839
the number beeing the value of the hidden field and the $valorCep route parameter.
my url is this way:
http://my_route/valorCEp?valorCep=14405024&cep=Rua++jardim+pedreiras14405024
Note that you are assigning $valorCep in the view with the literal 'valorCep'. You should pass it from your controller.
public function especificar_tipo_imovel(Request $request, $valorCep)
{
return view('especificar_tipo_imovel', ['valorCep' => $valorCep]);
}
and in your view:
{{ Form::open(['route' => ['anuncio.especificar_tipo_imovel', $valorCep], 'method' => 'GET']) }}
You can use this
public function especificar_tipo_imovel(Request $request, $cep)
{
$valorCep = $request->valorCep;
return view('especificar_tipo_imovel', ['valorCep' => $valorCep]);
}
You can't pass the value of the hidden input to the route like this $valorCep = 'valorCEp', that way you are just passing the string 'valorCEp' as the parameter. Check your url, is the route with the parameter being the string, plus the input's values (cause the GET method).
Unless you have the value of valorCep input in a variable, and pass this variable instead the 'valorCEp' string, you will need some javascript. Something like this:
Script (with jQuery)
$('input[type=submit]').on('click', function(event){
event.preventDefault();
var valorCep = $('#valorCep').val();
$('form').attr('action', 'anuncio/especificar_tipo_imovel/'+valorCep);
$('form').submit();
});
I have this in my form in the viewpage.php:
<form action="{{ route('test.route'), ['id' => $params_id] }}" method="POST" >
And this in the route.php:
Route::post('/testing/{{id}}',[
'uses' => 'TestController#testMethod',
'as' => 'test.route'
]);
And this is my TestController:
public function avaliarSubordinor(Request $request, $uid){
return $uid;
}
I get an error which says 'Missing required parameters for[Route: test.route] [URI: testing/{{id}}]. Essentially What i want is to pass a variable to my controller using a route with a parameter when form is submitted..
I dont know if I am doing this properlly..if anyone can help me or point me to an example so i can understand what I am doing wrong..
Laravel 5.2 Missing required parameters for [Route: user.profile] [URI: user/{nickname}/profile]
Using the above link I found a solution.. I changed:
<form action="{{ route('test.route'), ['id' => $params_id] }}" method="POST" >
to
<form action="{{ route('test.route', [$params_id]) }}" method="GET" >
and this:
Route::post('/testing/{{id}}',[
'uses' => 'TestController#testMethod',
'as' => 'test.route'
]);
to
Route::get('/testing/{id}',[
'uses' => 'TestController#testMethod',
'as' => 'test.route'
]);
and for reading value :
if ($request->id) {
}
And It works! But I wonder if anyone else can get a POST version working, or is GET the only way? I dont really know much about GET/POST request, only that it's used in forms and ajax.. Would really like to learn more about HTTP GET/POST, if anyone has anything to add please share!! thanks! Hope this answer will help someone!
For me this worked pretty well.
{{ Form::open(array('route' => array('user.show', $user->id))) }}
with class name
{{ Form::open(array('route' => array('user.show', $user->id), 'class' => 'section-top')) }}
Although it's an old post hopefully it will help others in future.
For me in Laravel 5.8 the POST method just worked fine.
HTML form:
<form method="POST" role="form" action="{{route('store_changed_role', [$user_id, $division_id])}}">
Route:
Route::post('/sotre_changed_role/{user_id}/{division_id}', 'Admin\UserController#store_changed_role')->name('store_changed_role');