'Laravel Delete and Update
I created a Post and a Get Request, Looks like this:
public function getAllCustomer()
{
$customer = \App\model\Customer::get();
return $view = View::make('test')->with('customer', $customer);
}
public function addNewCustomer(Request $request)
{
$validatedData = $request->validate([
'Title' => 'required',
'Name' => 'required|max:255'
]);
return \app\model\Customer::create($request->all());
}
So I can read all the data and create some new one, but now I want to delete them and/or update them, I couldnt find something really helpful, I would be very thankful for some help!
Solution 1:[1]
for updating you can define a route like this
Route::put('customer/{customer}' , 'CustomerController@update');
you should define a method_field('put') in your form and create a function called update in your controller
pulblic function update (Request $request , Customer $customer){
// validate you inputs
$validatedData = $request->validate([
// validation rules goes here
]);
// better approach would be using form requests
$customer->update($validatedData);
}
to update the fields that you want to be updated, you have to add them to $fillable array in your customer model, read about [Mass Assignment] 1 in laravel and that because all Eloquent models are protected against mass-assignment by default.
for delete : route:
Route::delete('customer/{customer} , 'CustomerController@destroy');
a function for deleteing customer in your controller
public function destroy (Customer $customer){
$customer->delete();
}
and add method_field('delete') to your form
Form example:
<form action="{{url("customer/{$customer->id}")}}"
method="post">
{{method_field('delete')}}
//Html Elements Here
</form>
after you get familiar with routes and controller you can make benefits of using
Route::resource('customer','CustomerController');
which will create all necessary route for you
Edited: please note that {customer} in route should be the id of the Customer you want to update or delete
Update: passing request->all() to update or create is not a good practice so i changed it with validated data, here is a link for laravel Form Request Validation
Solution 2:[2]
So to do delete you do in routes
Route::delete('customer/{customerId}', 'CustomerController@delete')
In controller
public function delete(Request $request, $customerId)
{
$user = $request->user();
$customer= $user->customers()->find($customerId);
$customer->delete();
return back();
}
To do update in routes
Route::patch('customers/{customerId}', 'CustomerController@update')
In controller
public function update(Request $request, $customerId)
{
$user = $request->user();
$customer= $user->customers()->find($customerId);
$customer->update($request->all());
return back();
}
More info as always in the docs https://laravel.com/docs/5.6/eloquent#updates
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Marcus |