Laravel passing 2 parameters for update method - laravel

I have an EDIT button
<td>Edit</td>
And when i press it, it should edit an item based on ID and then when i update it, it should update it based on ID which works, but when I want to redirect back to index page I have to pass argument for that index method. So i added that inventory_id parameter to be passed along ID parameter but it wont recognize my inventory_id parameter in mine form.
<form method="put" action="{{action('InventoryItemController#update', $id, $inventory_id)}}">
But i get this error
Undefined variable: inventory_id
my route is like this
Route::post('inventory-items/{id}/{inventory_id}', 'InventoryItemController#update');

Error is here:
<form method="put" action="{{action('InventoryItemController#update', $id, $inventory_id)}}">
The route should look like:
route('InventoryItemController#update', ['id' => $id, 'inventory_id' => $inventory_id ])
Good luck!

I found easier way to do it, I just used one of the parameters ($id) in controller to find second parameter. Thanks for answers.

Related

Missing required parameter on form action

First of all, I would like to say that my English is not so good and is my first time posting here, so excuse me if I did something wrong! Well, I am starting practicing with Laravel and I am trying to create a URL for users can like posts. my URL and controller is right now this Route::post('/post/{post}/like', [LikeController::class, 'postLike'])->name('post.like'); where post is the posts id that i am trying to pass through my form action attribute. Here is my form:
#props(['id'])
<div {{ $attributes->merge(['class' => 'card-footer d-flex justify-content-around']) }}>
<form action= "{{ route('post.like' , $id) }}" method="post" >
#csrf
<button>submit</button>
</form>
</div>
If you wonder if the id is not actually passed into the component, I checked {{ dd($id) }} and it printed it.
My controller is this which it doesn't actually do anything right now. I am just trying to pass the id:
class LikeController extends Controller
{
public function postLike(Post $post) {
dd($post);
}
}
After all this i am getting the error:
Missing required parameter for [Route: post.like] [URI: post/{id}/like] [Missing parameter: id]. (View: blog\resources\views\components\post\interaction.blade.php)
I am having two days to find the problem and I am still trying this moment I am sending this... I can't found where is the mistake! If you could help me would be much appreciated! Ty in advance
You need to pass the ID as an associative array in your route().
{{ route('post.like', ['id' => $id]) }}
If the named route defines parameters, you may pass the parameters as the second argument to the route function. The given parameters will automatically be inserted into the generated URL in their correct positions and you should name 'post' instead of 'id':
Route::post('/post/{post}/like', [LikeController::class, 'postLike'])->name('post.like');
And you can call this route anywhere
route('post.like', [$postId])
If that still doesn't work for you. Then it might be issue of Route Cache Run:
php artisan route:clear
Use same name parameter
When you use "id" keyword as a parameter in web.php then also same name pass in function argument in the controller
Route::post('/post/{id}/like', [LikeController::class, 'postLike'])->name('post.like');
Controller
class LikeController extends Controller
{
public function postLike(Post $id) {
dd($id);
}
}
Sorry for my bad English.
I have no idea this could be the problem but after many changes i cast the id of the post into an integer and it actually worked <form action= "{{ route('post.like' , (int)$id) }}" method="post" > . I don't know why i should cast the id into an integer because its already an integer by default and i checked that with {{ dd(getType($id)) }} and printed "integer"! I am really confused but it works! But i am not happy because i have no idea why i should cast an integer into an integer to make it work! Doesn't make any sense! If anyone has an answer to this would be great!

Route Exception with a post Method

I have a MethodNotAllowedHttpException with a button for a post method , it's really strange because i made the same process with a lot of others things in the project but with this route don't want to work . Where i made a mistake ?
thanks a lot in advance friends :)
Here my route :
Route::post('licencies_to_update/{id}', 'LicencieController#Renouveller')->name('licencie.renouveller');
Here my button in my blade view :
{!! link_to_route('licencie.renouveller', 'Effectuer le Renouvellement' , [$licencie->id], ['class' => 'btn btn-primary']) !!}
Here the begin of my controller :
public function Renouveller(Request $request, $id)
{
$licencie = Licencies::findOrFail($id);
dd($licencie);
....
Use:
Route::get('licencies_to_update/{id}', 'LicencieController#Renouveller')
->name('licencie.renouveller');
instead of POST method. Because in the link of your button you are not requesting the url with a POST method but GET method. Besides, you are not doing anything related to POST variable. You are passing a simple variable id in your route param. So, no need to use POST param here.
The link you are creating is an anchor to the route you provided, but the link is a GET request, while you specify in your routes file that you want POST requests on that url.
Either create a form or change the method that route accepts (or let it also accept GET requests)
Edit:
Change your route to
Route::get('licencies_to_update/{id}', 'LicencieController#Renouveller')->name('licencie.renouveller');
to have the expected result the fastest!

Laravel 5 routing within blade

up until this point I have essentially been using resource routing. One of my routes is for projects. If I create a project and then SHOW it, I see a URL in the form of
myUrl/projects/1
On the show page for a project, I want to be able to add a document. I have set up the relationships so a project can have one document and a document belongs to a project. I then set up the following route to handle the saving of the documents data
Route::post('projects/{id}/docOne', 'DocOneController#store');
So I add an a form in projects/show.blade.php, which opens like so
{!!
Form::model(new App\DocOne, [
'class'=>'form-horizontal',
'route' => ['docOne.store']
])
!!}
I then have my form fields and a save data button. Because of this new form within my projects show page, when I now show a project, it complains that the route for this new form is not defined.
How can I get this route to work within the projects show page?
Thanks
First of all you need to define a route name to your route, if you want to call it by his name.
So your route would be like:
Route::post('projects/{id}/docOne', [ //you need an array to set a route name
'as' => 'docOne.store', //here define the route name
'uses' => 'DocOneController#store' //here the callback
]);
Second you need to change your laravel form to use your route name and set the id
{!! Form::model(new App\DocOne, [
'route' => ['docOne.store', $project], //if you have setted the id variable like $id blade it gonna retturn it automatically only by passing the object, else, you can set $project->id
'method' => 'POST']
) !!}
EDIT:
You can't get an instance of a model on your view.
So the part:
{!! Form::model(new App\DocOne,
gonna fails every time you trye, also, the form:model needs an instance of a class that should have your vars filled with the info that the inputs should have (when you edit it).
You have two solutions:
If it's a new Doc and never before exist on your dataBase
I recomend to change your
Form::model
to:
Form::open
if it's a Doc thath already exist on your DB, like an edit, so in your controller you need to pass your existing Docas $docand remplace the: {!! Form::model(new App\DocOne, to:
{!! Form::model($doc,
and it works.
Form model was created to fill the input values with the data existing in your object instance, like when you edit someting.
So you need to have a correct instance.
Another think it's the MVC scope, a view shouldn't have acces to models, except if are passed by the controller.
Ok that's all.

Laravel 4 - Showing edit form with OLD data input as well as DB information

Im making a edit form for my app and i was wondering if someone could tell me how to get the data from the database into my text field.
I can locate the record i need to edit based on the users click, and i can display the information if i do the following:
value="{{ $letter->subject }}"
BUT, the problem im having is that when i run it through the validation and there is an error, it comes back with the database information instead of the OLD data.
So my questions is. Is there a way to serve up the database information first and then when it goes through the validatior, validate the information the user has edited?
Currently to validate the text field and bring the data back incase of error, im using
Input::old('subject')
Is there a parameter for that old bit that allows me to put in the DB data?
Cheers,
Hey you could validate and return ->withInput() and then in your actual form, check if there is Input::old() and display it, otherwise display from the db.
example:
<input type="text" name="subject"
value="{{ (Input::old('subject')) ? Input::old('subject') : $letter->subject }}">
Or you could go the other way and define the variable and do a regular if statement, instead of the ternary one! Up to you to decide what you want to use!
All you need is form model binding http://laravel.com/docs/html#form-model-binding:
{{ Form::model($letter, ['route' => ['letters.update', $letter->id], 'method' => 'put']) }}
// your fields like:
{{ Form::text('someName', null, ['class' => 'someHTMLclass' ...]) }}
// no default values like Input::old or $letter->something!
{{ Form::close() }}
This way you form will be populated by the $letter data (passed from the controller for example).
Now, if you have on your countroller:
// in case of invalid data
return Redirect::back()->withInput();
then on the redirect your form will be repopulated with input values first, not the original model data.
Make it more simple and clean
<input type="text" name="subject" value="{{ (Input::old('subject')) ?: $letter->subject }}">
I'm not sure for Laravel 4 but in Laravel 5, function old takes second param, default value if no old data in session.
Please check this answer Best practice to show old value

adding class and id in form_dropdown

Is there any possible way of adding class and id attributes in form_dropdown of CodeIgniter?
I tried form_dropdown('name',$array,'class','id') and it's not changing anything, please tell me how to implement it?
Edited:
with my dropdown form like this form_dropdown('name',$array,set_value('someValue'),'id="myId"'); if I see my source from my browser it's look like this <select name="provinsi" id="provinsi_id">, but if I write like your way form_dropdown('name',$array,set_value('someValue'),'class="myClass"','id="myId"'); than like this in my browser source <select name="provinsi" class="myClass">
that was I mean
thank you
Like this:
form_dropdown('name', $array, '', 'class="my_class" id="my_id"')
The third parameter is for the value you wish to be selected and the fourth is for the additional data. Read more.
You can defined class, id or any other HTML attribute in the forth parameter of form_dropdown() function as an associative array like below:
form_dropdown('name', $array, set_value('someValue'), ['class' => 'myClass', 'id' => 'myId']);

Resources