use patch method to update model in laravel - laravel

i want to update a customer using patch methodn here is my route
Route::patch('/customers/updateCustomer', 'CustomerController#update')
->name('customers.update');
and here is my form :
<form method="POST" action="{{ route('customers.update') }}">
#csrf
#method('PATCH')
how to pass the id of the customer , the update method requires two parameters, when the first one is the data from the from , the second should be the id of the customer

You should send the customer id with the route() helper function.
Make sure to send the customer object during the form rendering.
<form method="POST" action="{{ route('customers.update', $customer['id']) }}">
#csrf
#method('PATCH')
And modify the route slightly.
Route::patch('/customers/updateCustomer/{customerId}', 'CustomerController#update')->name('customers.update');

add customer id to the route like
Route::patch('/customers/updateCustomer/{id}', 'CustomerController#update')->name('customers.update');
and then your form action should be like
<form method="POST" action="{{ route('customers.update',$customer->id) }}">
you have to send the customer object from where you are returning to the form.
and finally your update function
public function update(Request $request, $id)
{
//your things to do
}

You have to use like this
<form method="POST" action="{{ route('customers.update',['id'=>$customer->id]) }}">
And your route should be
Route::patch('/customers/updateCustomer/{id}', 'CustomerController#update')->name('customers.update');
And your controller should be
public function update(Request $request, $id){
//your code
}

Related

Form not showing due to routing problem in action parameter

I am working on an Excel import module as part of a CRM for my company. I want to import an excel sheet. I use the Maatwebsite Excel package, version 3.1. I want to show the form and then upload a sheet. However I can't even get to that point. I have already determined the issue is within the form route, just not sure what it is exactly that I am missing.
Routes that I use to display the page (index works fine)
Form used to get the Excel sheet imported
Navigation bar link in the menu
DataController (from which I am trying to call the import method)
If you know what may be wrong please do tell, this is really frustrating!
Route code:
Route::get('importeren', 'Datacontroller#index');
Route::post('import', 'Datacontroller#import');
<div class="container-fluid">
<form action="import" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="import_file">
<br>
<input type="submit" value="Import">
</form>
</div>
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Imports\DataImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class DataController extends Controller
{
public function index(){
return view('importeren');
}
public function import(Request $request){
Excel::import(new DataImport(), $request->file('import_file'));
return redirect()->route('/home');
}
}
Don't use route() method because you're not defining any routes name, use something like:
form action="/import" method="POST" enctype="multipart/formdata">
you should use route() only with route name, use this instead :
return redirect('/home');
You can give a name to your route:
Route::post('import', 'Datacontroller#import')->name('import');
and leave the form action as it is with the route() helper:
<div class="container-fluid">
<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="import_file">
<br>
<input type="submit" value="Import">
</form>
</div>
You could also use the url() helper and leave the route with no name, but I highly recommend the option of giving your route a name.
<form action="{{ url('import') }}" method="POST" enctype="multipart/form-data">
Note that in the controller return you are also using a redirect to a named route, so I suggest that you give that route a name and use that name in the redirect. For example:
Route::get('home', 'SomeController#someMethod')->name('home');
and
return redirect()->route('home');

How to pass variable to view without using url in Laravel?

guys. I'm a newbie of Laravel.
I'm just wondering that if I need to pass some sensitive info, which is been wrote on a form, to controller, then to another view.
How could I pass the info to the view without using URL?
I have made some small test, but the result is all the same (url would include the data).
Here is the code.
Html(a):
<form action="/data" method="GET" class="form-horizontal">
<div class="form-group">
<input type="text" class="form-control" name="Test">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">test</button>
</div>
web.php:
Route::get('/data', 'DataController#show');
DataController.php:
public function show(Request $request) {
return view('testShow');
}
Html(b):
<div class="ShowDataHere">
#if(!empty(request()->all()))
{{ request()->Test }}
#endif
Simply change your form to use POST instead of GET.
On the form...
<form action="/data" method="POST" class="form-horizontal">
In your route ...
Route::post('/data', 'DataController#show');
This will send all of the form fields to the server in a way that is less visable (not in the URL) but to make the form secure, you'll want to ensure it's served using HTTPS as well.
In Laravel when you use Route::post('/data', 'DataController#show') the Laravel expect a GET, because of the show function.
So you can use Route::post('/data', 'DataController#someFunction') and in your laravel controller get with:
public function someFunction(Request $request)
{
$array = $request->all(); //your fields
}
In this function you can get the attributes with $array = $request->all();

Undefined variable in view laravel

I have this code here which is listing all items from one user, user and item are two differents model and they have relationship with their primary keys.
#foreach($user->items as $item)
<p class="heading">
{{ $item->item_id }}
</p>
#endforeach
My question is how to send that item_id with post request to controller, because when I write like this for example <form class="form-horizontal" method="post" action="{{ route('preview', $item->item_id) }}" role="form" enctype="multipart/form-data"> it gives me that $item variable is not defined.
My controller:
public function preview($item_id){
return view('itempreview')->with('item_id', $item_id);
}
Any ideas how to send that item_id?
You should use array as 2nd param to route() method!
You are doing it wrong, you should send it like this:
route('preview', ['item_id' => $item->item_id]);
Make sure, your route should be defined as this:
Route::post('preview/{item_id}', 'ControllerName#preview')->name('preview');
Hope this helps!
You need to send $item_id as part of the POST:
#foreach($items as $item)
<form method="post" action="{{ route('preview') }}">
<input type="hidden" name="item_id" value="{{ $item->item_id }}">
</form>
#endforeach
Then in the method that receives the POST:
public function index() {
$item_id = request()->input('item_id');
return view('preview');
}

How to populate edit form fields in laravel 5.3

I have a problem in populating edit form fields in my first Laravel 5.3 application. In examples I have found online, they have used Form facade to do it. However, I prefer to use plain html.
This is my controller action
public function edit()
{
$profile = Profile::find(Auth::user()->id);
return view('profile.edit', ['profile' => $profile]);
}
In my view file I have a form like this
<form method="POST" action="{{ url('profile/update') }}">
{{ csrf_field() }}
<input id="name" type="text" name="name" value="{{ old('name') }}">
...
</form>
Problem is, above input field is not populated with the value of the $profile->name.
I can get it to work, setting the value attribute like
{{ (old('name')) ? old('name') : $profile->name }}
Since I'm new to Laravel framework I want to know if there is a better way to do that.
Use old($key, $default). It will return $default if $key is not found.

Laravel 5 Plain Html MethodNotAllowedHttpException

Hello so I am trying to make an update form in Laravel using plain html. The url of my edit form is http://localhost:8000/profile/sorxrob/edit, sorxrob is the username. And the code in that url is:
<form class="form-horizontal" role="form" action="" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
//more inputs
</form>
And in my controller named AccountController:
public function update(Request $request, $id)
{
$account = Accounts::findOrFail($id);
$input = $request->all();
$account->fill($input)->save();
return 'success';
}
And I am getting the error MethodNotAllowedHttpException when I click the update button. Is it because my action is equal to nothing? If yes, what is the correct way of routing there?
This is due to your action url which is not correct routing url. Use the following
(1) First define a route in your route.php file
Route::post('profile/{username}/edit', array('as' => 'profile.update', 'uses' => 'AccountController#update'));
(2) Change your action attribute from your form tag
action="{{ URL::route('profile.update', [$username]) }}"
here $username variable will be passed from your AccountController#edit method.

Resources