Laravel named route with {parametr} - laravel

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?

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!

getting errors The POST method

I have a problem with my edit page. When I submit I get this error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods:
POST
.
I have no clue where it comes from as I am pretty new to Laravel.
web.php
Route::post('/admin/add_reseller','ResellerController#addReseller');
Controller.php
public function addReseller(){
return view ('admin.resellers.add_reseller');
}
add_reseller.blade.php
<form class="form-horizontal" method="post" action="" name="addreseller" id="addreseller">
{{ csrf_field() }}
Tip:
First of all, I would use named routes, that means you add ->name('someName') to your routes. This makes the use of routes way easier and if you decide that your url doens't fit you don't need to change it everywhere you used the route.
https://laravel.com/docs/7.x/routing#named-routes
e.g. Route::post('/admin/add_reseller',
'ResellerController#addReseller')->name('admin.reseller');
Problem:
What I see is, that you lack the value for the action attribute in your <form>. The action is needed, so that the right route is chosen, if the form gets submitted.
Solution:
I guess you just have to add action="{{route('admin.reseller')}}" for the attribute, so that the request goes the right route.
<form class="form-horizontal" method="post" action="" name="addreseller" id="addreseller">
Your action in the form is empty, you need to add the corresponding route there.
I'd suggest you to use named routes.
https://laravel.com/docs/7.x/routing#named-routes
In web.php
Route::post('/admin/add_reseller','ResellerController#addReseller')->name('admin.add.reseller');
and then in your blade file you could refer to the route using the route() function by passing the name of the route as an argument
<form class="form-horizontal" method="post" action="{{route('admin.add.reseller')}}" name="addreseller" id="addreseller">

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.

Why does not work POST routing in Laravel

I use the standard authentication mechanism in Laravel.
Route path is:
Route::post('auth/register', 'Auth\AuthController#postRegister');
HTML form is:
<form method="POST" action="/auth/register">
When I submit form I get 404 error.
But path for GET methos works:
Route::get('auth/register', 'Auth\AuthController#getRegister');
Use Laravel's helper function url() to generate an absolute URL. In your case the code would be:
<form method="POST" action="{{ url('auth/login') }}">
You could also check out the laravelcollective forms package. These classes were removed from the core after L4. This way you could build HTML forms using PHP only:
echo Form::open(['url' => 'auth/login', 'method' => 'post'])
you can use {{ URL::route('register') }} function in the action method. In This case the method will be:
action="{{ URL::route('register') }}"
and your route file will be:
`
Route::get('auth/register',array('as' =>'register' ,'uses' => 'Auth\AuthController#getRegister'));
Route::post('auth/register', array('as' =>'register' ,'uses' =>'Auth\AuthController#postRegister'));
`

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

Resources