Sử dụng Postman để post dữ liệu theo cách mới Customer::create($request->all()) (ok)
Chú ý 😒😒😒 thông thường chúng ta hay dùng $customer->save() để ghi dữ liệu như sau
C:\xampp\htdocs\vl\app\Http\Controllers\CustomerController.php
<?php
namespace App\Http\Controllers;
use App\Models\Customer;
use Illuminate\Http\Request;
class CustomerController extends Controller {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
return Customer::orderBy('id', "DESC")->paginate(5);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
$customer = new Customer();
$customer->name_customer = $request->name_customer;
$customer->phone_customer = $request->phone_customer;
$customer->email_customer = $request->email_customer;
$customer->city_customer = $request->city_customer;
$customer->address_customer = $request->address_customer;
if ($request->image_customer) {
$get_image = $request->image_customer;
$path = 'public/uploads/';
$get_name_image = $get_image->getClientOriginalName();
$name_image = current(explode('.', $get_name_image));
$new_image = $name_image . rand(0, 99) . '.' . $get_image->getClientOriginalExtension();
$get_image->move($path, $new_image);
$customer->image_customer = $new_image;
}else {
$customer->image_customer = "default.png";
}
$customer->save();
}
/**
* Display the specified resource.
*
* @param \App\Models\Customer $customer
* @return \Illuminate\Http\Response
*/
public function show(Customer $customer) {
return Customer::find($customer);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Customer $customer
* @return \Illuminate\Http\Response
*/
public function edit(Customer $customer) {
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Customer $customer
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $customer) {
$customer = Customer::find($customer);
$customer->name_customer = $request->name_customer;
$customer->phone_customer = $request->phone_customer;
$customer->email_customer = $request->email_customer;
$customer->city_customer = $request->city_customer;
$customer->address_customer = $request->address_customer;
$customer->save();
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Customer $customer
* @return \Illuminate\Http\Response
*/
public function destroy(Customer $customer) {
return $customer->delete();
}
}
Nhưng sau đây là cách mới :)
C:\xampp\htdocs\api\app\Http\Controllers\CustomerController.php
<?php
namespace App\Http\Controllers;
use App\Models\Customer;
use Illuminate\Http\Request;
use App\Http\Resources\CustomerResource;
use App\Http\Resources\CustomerCollection;
class CustomerController extends Controller {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
// return Customer::all();
// return CustomerResource::collection(Customer::paginate());
return new CustomerCollection(Customer::paginate());
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
$request->validate([
'name_customer' => 'required',
'phone_customer' => 'required',
'address_customer' => 'required',
'email_customer' => 'required',
'city_customer' => 'required',
]);
$customer = Customer::create($request->all());
}
/**
* Display the specified resource.
*
* @param \App\Models\Customer $customer
* @return \Illuminate\Http\Response
*/
public function show(Customer $customer) {
return new CustomerResource($customer);
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Customer $customer
* @return \Illuminate\Http\Response
*/
public function edit(Customer $customer) {
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Customer $customer
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Customer $customer) {
//
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Customer $customer
* @return \Illuminate\Http\Response
*/
public function destroy(Customer $customer) {
//
}
}
PreviousCách sử dụng ngắn gọn để chuyển về json thay vì tạo custom ép về dạng json (ok)Next[ERROR] 'mix' is not recognized as an internal or external command (ok)
Last updated