Update Query in Laravel Using Eloquent ORM
First of all I have changed routes>web.php file
After that I have create last two functions edit and update. In the edit function there are customer key in the compact which is used to get data in the customer blade file.
";
print_r($request->all());
$customers = new Customers;
$customers->name = $request['name'];
$customers->email = $request['email'];
$customers->password = md5($request['password']);
$customers->city = $request['city'];
$customers->state = $request['state'];
$customers->address = $request['address'];
$customers->gender = $request['gender'];
$customers->dob = $request['dob'];
$customers->save();
return redirect('/customer');
}
public function view(){
$customers = Customers::all();
// echo "";
// print_r($customers->toArray());
// echo "";
$data = compact('customers');
return view('customer-view')->with($data);
}
public function delete($id){
// find targets primary key
$customer = Customers::find($id);
if(!is_null($customer)){
$customer->delete();
}
return redirect('customer');
// echo "";
// print_r($customer);
}
public function edit($id){
$customer = Customers::find($id);
if(is_null($customer)){
// not found
return redirect('customer');
}else{
// found
$title = "Customer Updatation";
$url = url('customer/update') . "/" . $id;
$data = compact('customer', 'url', 'title');
return view('customer')->with($data);
}
}
public function update($id, Request $request){
$customer = Customers::find($id);
$customer->name = $request['name'];
$customer->email = $request['email'];
$customer->city = $request['city'];
$customer->state = $request['state'];
$customer->address = $request['address'];
$customer->gender = $request['gender'];
$customer->dob = $request['dob'];
$customer->save();
return redirect('customer');
}
}
After that is change customer blade file
{{$title}}
And the data resulted on customer-view blade file
Observe the code and understand to concepts carefully. Thats all about update query in Laravel using eloquent ORM.
Comments
Post a Comment