How to send a value to route in laravel - laravel

I want to send a value to route that i am received from the controller.!How can i do it.
My controller:
public function payment_history($id)
{
$payment_details=AmountDrvPaidHistoryModel::where('driver_id',$id)->get();
return view('admin.driver_payment_history')->with('payment_details',$payment_details)->with(array('id'=>$id));
}
Here is my blade:
<form action="{{URL::to('driver_payment_history/'.$id)}}" method="post">
How can i get the value of id in action tag that is send to another route.

just remove array from this
->with('id'=>$id);

You code will be:
public function payment_history($id)
{
$payment_details = AmountDrvPaidHistoryModel::where('driver_id',$id)->get();
return view('admin.driver_payment_history', compact('payment_details', 'id'));
}
With compact you can get the variable data, so $id will pass on to the view.
Than you can get the id with this code:
<form action="{{ url('driver_payment_history/'.$id) }}" method="post">
EDIT
<form action="{{ url('driver_payment_history/'.$payment_details[0]->driver_id) }}" method="post">
Hope this works!

You have to reference the $id inside your function, initiating it to be passed by the specific function.
public function payment_history($id) //<-this is the parameter passed into the function
{
$payment_details=AmountDrvPaidHistoryModel::where('driver_id',$id)->get();
$new_var_id = $id; //<- Assign the $id to a new variable (eg. $new_var_id)
return view('admin.driver_payment_history',compact('payment_details','new_var_id');
}
You will then access the variable as such
<form action="{{ url('driver_payment_history/'.$new_var_id) }}" method="post">
However keep in mind that you are already doing a get request with the $id variable, and sending all the results to the page. This means that you can just access the specific id in the view by accessing the objects properties without re initiating the $id variable.
<form action="{{ url('driver_payment_history/'.$payment_details->driver_id) }}" method="post">
If you do use this, you can remove $new_var_id(or whatever you named it) from your controller, as well as removing it from compact()

Related

Laravel collection querying

I have written a query to grab a collection of results, i've added a check to say if a record contains this field hide the record with the id of 2.
Controller method
$purchasedProducts = $user->products()->where('purchased', 1);
if ($user->products()->where('includes_bonus', 1)->first()) {
$purchasedProducts->where('benefits.id', '!=', 2);
}
$purchasedProducts->get();
Blade
in here i wrote out the foreach loop to be displayed within the blade.
#foreach($purchasedProducts as $product)
<div class="col-xl-6 p-0 p-xl-4 mb-5 mb-xl-0">
<form action="{{route('cancel.product', $product->id)}}" method="POST">
#csrf
error received
Trying to get property 'id' of non-object
<form action="<?php echo e(route('cancel.product', $product->id)); ?>" method="POST">
Can you see where i am going wrong?
You haven't assigned the results of the query to anything, you are just calling get() on the query you are building but are not doing anything with the returned results. Perhaps assign it to a variable:
$purchasedProducts = $purchasedProducts->get();

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">

The DELETE method is not supported for this route with laravel

i'am using laravel in my project , do i want to delete an appointement , but i get this error : The DELETE method is not supported for this route. Supported methods: GET, HEAD.
This is the controller :
public function destroy($id)
{
$rdv = DB::table('rdv')->where('id',$id)->delete();
return redirect()->back()->withSuccess('success delete !' ) ;
}
}
this is the form :
#if ( $getpat->Etat_de_rdv == 'en_attente')
<td><label class="badge badge-warning"> {{$getpat->Etat_de_rdv}} </label></td>
<form method="POST" action="{{ route('delete', $getpat->id) }}">
#method('DELETE')
#csrf
<button type="submit">Supprimer rendez-vous</button>
</form>
this is the web.php
Route::get('/delete', 'rendezv#destroy')->name('delete');
It should be
Route::delete('/delete/{id}', 'rendezv#destroy')->name('delete');
You're using Route::get(), but supplying #method('delete'); those are contradictory. Modify your route as follows:
Route::delete('delete', 'rendezv#destroy')->name('delete');
Additionally, you're not passing the $id parameter, so route('delete', $getpat->id) won't work. You can do this with a form field, or a URL parameter:
Route::delete('delete/{id}', 'rendezv#destroy')->name('delete');
the correct declaration of the route is:
Route::delete('/delete', 'rendezv#destroy')->name('delete');

update method issue(The POST method is not supported) in laravel

I want to create update method and this is the code:
Route::get("/allProducts/edit/{id}","AllproductController#edit")->name('Allproduct.edit');
Route::post("/allProducts/update/{id}","AllproductController#update")->name('Allproduct.update');
<form class="form-horizontal tasi-form" method="post" enctype="multipart/form-data"
action="{{ route('allProducts.update' , [ 'id'=>$allproduct->id ]) }}">
{{ csrf_field()}}
public function update(Request $request, $id)
{
$data = Allproduct::find($id);
$data->name = $request->name;
$data->save();
return redirect(route('allProducts.index'));
}
when I click on submit button it shows me :
"The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE" error!
what is the problem?
Your route names do not match.
in routes:
name('Allproduct.update');
in the form:
allProducts.update
Also, you can always check the name of the routes thanks to the console command:
php artisan route:list
if you want use method PUT:
you can change method in routes:
Route::post to Route::put
and add next in form:
<input type="hidden" name="_method" value="PUT">
OR
#method('PUT')
this is if your laravel version is 6 and if your version another, check correct way to use PUT method in forms at laravel.com/docs/6.x/routing with your version.
As said here
HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
So your form should look like this:
<form class="form-horizontal tasi-form" method="post" enctype="multipart/form-data"
action="{{ route('Allproduct.update' , [ 'id'=>$allproduct->id ]) }}">
#csrf
#method('PUT')
You had a typo in your route name and it was missing the method field.
Change your route to this:
Route::put("/allProducts/update/{id}","AllproductController#update")->name('Allproduct.update');
This will work, but
i strongly recommend you read this laravel naming conventions, and then change the name of your controller and model to AppProductController, AppProduct.

The PUT method is not supported for this route

I cannot submit form information through to store function in laravel controller. The form needs to create a - new - profile for a registered user.
I have even recreated the project, and redone the form - moving back into plain html as I suspect that the laravelCollective functions may be causing it but still the same error.
I have even rearranged the the form attributes as suggested in another post/thread.
I have even recreated the project, and redone the form - moving back into plain html as I suspect that the laravelCollective functions may be causing it but still the same error.
I have even rearranged the the form attributes as suggested in another post/thread.
The Form:
< form method="POST" enctype="multipart/form-data" action="{{ url('users/profile') }}" accept-charset="UTF-8" >
#csrf
...
// input fields here
...
< /form >
The Routes:
Route::resource('users/profile', 'ProfileController');
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('users', 'UserController');
Route::post('users/profile', 'ProfileController#store')->name('profile.store');
The ProfileController#store function:
//some code omitted
public function store(Request $request)
{
$this->validate($request, [
'firstname'=>'required',
'lastname'=>'required',
...
'desc'=>'required'
]);
//handle file upload
if($request->hasFile('cover_image')) {
//Get file name with extension
$fileNameWithExt = $request->file('cover_image')->getClientOriginalName();
//Just file name
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
//Just Ext
$ext = $request->file('cover_image')->getClientOriginalExtension();
//FileName to Store
$fileNameToStore = $fileName.'_'.time().'_'.$ext;
//upload image
$path = $request->file('cover_image')->storeAs('public/users/'.auth()->user()->id.'cover_images/'.$request->input('firstname').'_'.$request->input('lastname').'_'.auth()->user()->id.'/',$fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
/*
*/
$profile = new Profile;
$profile->firstname = $request->input('firstname');
$profile->lastname = $request->input('lastname');
...
$profile->desc = $request->input('desc');
$profile->save();
return redirect('/users/profile');//->with('success','Profile Created');
}
The famous error:
Symfony \ Component \ HttpKernel \ Exception \
MethodNotAllowedHttpException The PUT method is not supported for this
route. Supported methods: GET, HEAD, POST.
Not sure what is causing the error, help appreciated.
If I understand it correctly this is for store function right? then you don't have to put #method('PUT') inside your form it should POST. The route of store in resource is POST.
this is your code that i deleted the #method('PUT')
< form method="POST" enctype="multipart/form-data" action="{{ url('users/profile') }}" accept-charset="UTF-8" >
#csrf ...
// input fields here ...
< /form >
The Routes: Route::resource('users/profile', 'ProfileController');
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('users', 'UserController'); Route::post('users/profile', 'ProfileController#store')->name('profile.store');
and the PUT method is used for updating. When update in controller you need to pass id in your form that should look like this.
< form method="POST" enctype="multipart/form-data" action="{{ url('users/profile', $data->id) }}" accept-charset="UTF-8" >
#method('PUT')
#csrf ...
// input fields here ...
< /form >
I hope it helps!
you have problem in your routes file simply change your edit route to this route
Route::match(['put', 'patch'], 'the path you want /{id}','controllername#functionname');
you should notice that if you are new to laravel you should pass the id to this route as shown in this part {id} so that your edit function could display the previous data of it and also if you want to submit a the form it should have the put method and the html basic forms doesn't support that so you should find a way to submit it like using laravel collective or maybe put a hidden method in your form
if it doesn't work please give me a call
When using the Laravel resource method on a route, it makes things pretty specific in terms of what it is expecting. If you take a look at the chart on the manual, it is looking for a uri with the updating element id returned as part of the uri. The example looks like: /photos/{photo}. I'm not sure that this is how you've structured your html form.
You said you were using the LaravelCollective to get this working. This usually works fine, and has the massive advantage of easy form-model binding. But it helps to include the named route, which includes 'update' for the update resource. For example:
{!! Form::model($yourModel, array('route' => array('yourRoute.update',
$yourModel->id), 'method'=>'PUT', 'id'=>'Form'))!!}
I have not had an issue with the Collective using this method.

Resources