Missing required parameter on form action - laravel

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!

Related

Laravel named route with {parametr}

Route::match(['patch','put'],'/edit/{id}', 'TestController#update')->name('update');
using route() helper in form action I expected to see
https://example.com/edit/1
And what I get using {{ route('update', $article->id) }} is https://example.com/edit?1
Any ideas how to resolve this?
Try passing the id in as an array:
route('update', ['id' => $article->id])
and make sure the form's method attribute is post as well as setting the correct _method value within the form:
<form action="{{ route('upate', ['id' => $article->id]) }}" method="post">
{{ method_field('patch') }}
</form>
I tried your example and it seems to work as expected. Going by the ? in the URL, my guess would be that it is a GET instead of POST in the form? Could you confirm that?

can I use route() to make a DELETE request in laravel

I'm using laravel and trying to delete something. Is it possible to specify the DELETE method on laravel's route()??
e.g
route('dashboard-delete-user', ['id' => $use->id, 'method'=> 'delete'])
or something like that??
EDIT:
What I meant was could I specify that in a link or a button in my blade template. Similar to this:
href="{{ route('dashboard-delete-user') }}
Yes, you can do this:
Route::delete($uri, $callback);
https://laravel.com/docs/master/routing#basic-routing
Update
If for some reason you want to use route only (without a controller), you can use closure, something like:
Route::get('delete-user/{id}', function ($id) {
App\User::destroy($id);
return 'User '.$id.' deleted';
});
No or at least I haven't figure out how to.
The only way for this to work out of the box would be to build a form to handle it. At the very minimum, you would need...
<form action="{{ route('dashboard-delete-user') }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" value="submit">Submit</button>
</form>
Or you can just create the get route which you are trying to link to and have it handle the logic. It doesn't need to be a route which only respondes to delete requests to delete a resource.
Yes you can, using a URL helper. https://laravel.com/docs/5.2/helpers#urls
There are several options to choose from.

laravel passing parameters from view to route

Using a view with user input. I then want to pass to a route. This what I found so far:
href="{{URL::to('customers/single'$params')}}"
I want to pass the user input as the above $params to my route. This is sample of my route:
Route::get('customer/{id}', function($id) {
$customer = Customer::find($id);
return View::make('customers/single')
->with('customer', $customer);
As soon as I can pass the parameter I can do what I want with the route, which I know how.
Basically you can pass parameter to routes by doing:
Route::get('user/{name}', function($name)
{
//
})
->where('name', '[A-Za-z]+');
In your anchor tag, instead of doing href={{URL...}}, do something like:
{{ URL::to('user/$param') }}
For more information on routing, visit link
This is what I have and works:
<a <button type="button" class="buttonSmall" id="customerView" href="{{URL::to('customer',array('id'=>'abf'))}}" >View</button></a>
But I need the array value 'abf' to be the value of a textbox.
This worked for me in my view anchor tag
href="{{ URL::to('user/'.$param) }}"
instead of what was specified above
href="{{ URL::to('user/$param') }}"
You can user it in View as I used:
<a class="stocks_list" href="/profile/{{ Auth::user()->username }}">Profile</a>
Hope it helps you.

Laravel: delete photo by photo id

I'm trying to delete a photo by it's id, but the routes do not work and I receive a MethodNotAllowedHttpException. What I do:
First I create a form (in my blade template):
{{ Form::open(array("action" => array("cms/albums/destroyphoto", $photo['id']), "method" => "DELETE")) }}
<button type="submit">Delete</button>
{{ Form::close() }}
Then i create my route:
Route::post('cms/albums/destroyphoto/{id}', 'AlbumsController#destroyphoto');
And create my function in the Albumscontroller:
public function destroyphoto($id)
{
dd('Welcome photo');
}
Any suggestions where the routing goes wrong?
Thanks in advance.
Ps. I did composer dump-autoload
When you open your form using "action" you should pass the controller class and action name. You also don't need to specify the method since you're using Route::post
Like this:
{{ Form::open(array("action" => array("AlbumsController#destroyphoto", $photo['id']))) }}
More information

Dropdownlist in a edit.blade.php form

please how can I put dropdown list in an edit form with the old selected value on default?
here is my example:
<div class="form-group">
{{ Form::label('Container', 'Container:') }}
{{ Form::select('Select_cont', $containers) }}
</div>
I don´t know where to put the code of the old new value selected in my view and what should it be.
Please don´t forget that when directing to edit.blade.php I wrote thisin the function of my controller
return View::make('audio.edit',array($container))
->with('containers', $containers)
thanks a lot for your help :)
Selected value is the 3rd param of Form::select, so:
{{ Form::select('Select_cont', $containers, $selectedPreviouslyKey) }}
How to get that key depends on your code and what's in the dropdown
Pass it to the view like this
return View::make('audio.edit',array('containers' => $containers, 'selectedPreviouslyKey' => $selectedKey));
or use compact() / with etc
The View::make call isn't entirely right. It should be:
return View::make('audio.edit')->with('containers', $containers);
or
return View::make('audio.edit')->withContainers($containers);
or
return View::make('audio.edit', array('containers' => $containers));
or
return View::make('audio.edit', compact('containers'));
Also: make sure $containers exists.
The documentation on the select tag. http://laravel.com/docs/html#drop-down-lists

Resources