gpt4 book ai didi

php - Laravel Eloquent - 将请求保存、更新和删除到相关表中

转载 作者:行者123 更新时间:2023-11-29 02:13:44 25 4
gpt4 key购买 nike

我正在尝试使用 Laravel 5.4 开发某种 API,并一直在尝试保存、更新和删除销售及其相关的销售详细信息,但在尝试并浏览了大量代码示例后仍然没有取得任何进展这里。我希望能够:

  1. 保存销售及其销售明细(无需一一映射字段)
  2. 更新销售及其销售详细信息(无需逐一映射字段)
  3. 一次删除销售及其详细信息。

下面是我的代码

销售模式关系

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\SalesDetail;

class Sale extends Model
{
//
protected $fillable=[
'receipt',
'customer',
'total_cost',
'amount_paid',
'payment_type',
'discount_percent',
'discount_amount'

];

// a sale has many sales details
public function saleDetails(){
return $this->hasMany('App\Models\SalesDetail');
}
}

销售明细模型关系

 namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Sale;

class SalesDetail extends Model
{
//
protected $fillable=[
// 'sales_id',
'category',
'menu',
'unit',
'quantity',
'cost'

];

// A sales detail belongs to a sale
public function sale(){
return $this->belongsTo('App\Models\Sale');
}
}

销售 Controller - 保存功能(已更新 - 现在工作正常)

public function store(Request $request)
{
//Validate request data
$this->validate($request,['receipt'=>'required']);

//Save sales without sales details data
$sale = Sale::create($request->except(['sale_details']));

//Get sales details data
$saledetails = $request->sale_details;

//Loop through sales details
foreach ($saledetails as $data){

//Create a new instance of sales details model
$details = new SalesDetail;

//fill the model properties (mass assignment) with the data
$details->fill($data);

//Save and link sales details to sales
$sale->saleDetails()->save($details);

}

return 'Sale saved successfully';
}

销售总监 - 更新功能

 public function update(Request $request, $id)
{
//
$this->validate($request,['receipt'=>'required']);

$sale = Sale::with('saleDetails')->findOrFail($id);

$input=$request->all();
$sale->fill($input)->save();

return 'Sale updated successfully';
}

销售主管 - 删除功能

public function destroy($id)
{
//
Sale::with('saleDetails')->whereId($id)->first()->delete();

return 'Sale deleted sucessfully';
}

示例销售数据

{
"id": 1,
"receipt": "RT124558",
"customer": "John Dumelo",
"total_cost": 1000,
"amount_paid": 900,
"payment_type": "Cash",
"discount_percent": 0.1,
"discount_amount": 100,
"created_at": "2017-06-12 00:00:00",
"updated_at": "2017-06-12 00:00:00",
"sale_details": [
{
"id": 1,
"sale_id": 1,
"category": "Pizza",
"menu": "Meat Eater",
"unit": "Large",
"quantity": 10,
"cost": 50,
"created_at": "2017-06-14 00:00:00",
"updated_at": "2017-06-14 00:00:00"
},
{
"id": 2,
"sale_id": 1,
"category": "Pizza",
"menu": "Meat Deluze",
"unit": "Medium",
"quantity": 5,
"cost": 30,
"created_at": "2017-06-14 00:00:00",
"updated_at": "2017-06-14 00:00:00"
}
]
}

我是 Laravel 的新手,希望我能得到这方面的帮助。先感谢您。

最佳答案

尝试单独保存 Sale 和 SaleDetail 模型,然后通过以下方式将它们关联起来:

$sale->saleDetails()->associate($details); 

根据您的 SaleDetails 模型,您需要对此进行过滤:

"sale_details": [
{
"id": 1,
"sale_id": 1,
"category": "Pizza",
"menu": "Meat Eater",
"unit": "Large",
"quantity": 10,
"cost": 50,
"created_at": "2017-06-14 00:00:00",
"updated_at": "2017-06-14 00:00:00"
},
{
"id": 2,
"sale_id": 1,
"category": "Pizza",
"menu": "Meat Deluze",
"unit": "Medium",
"quantity": 5,
"cost": 30,
"created_at": "2017-06-14 00:00:00",
"updated_at": "2017-06-14 00:00:00"
}
]

只包括这个:

"sale_details": [
{
"category": "Pizza",
"menu": "Meat Eater",
"unit": "Large",
"quantity": 10,
"cost": 50
},
{
"category": "Pizza",
"menu": "Meat Deluze",
"unit": "Medium",
"quantity": 5,
"cost": 30
}
]

关于php - Laravel Eloquent - 将请求保存、更新和删除到相关表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44569216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com