簡體   English   中英

如何正確使用 Laravel 關系呢?

[英]How to use Laravel relationships it correctly?

我有以下表結構:

users:
    id
    name
    
products:
    id
    name    
    
orders:
    id
    user_id
    product_id
    paid (boolean)

order_items:
    order_id
    product_id
    

使用關系,獲得用戶 1 購買和支付的所有產品的最佳方式是什么? 另外,如何傳遞產品 ID,並檢查用戶 1 是否已為該產品付款?

user_id添加到order_items表中,這將使事情變得更容易。 然后你可以為你的模型添加必要的關系:

用戶:

public function orders(): HasMany 
{
    return $this->hasMany(Order::class);
}

public function paidOrders(): HasMany 
{
    return $this->hasMany(Order::class)
        ->where('paid', true);
}

public function products(): BelongsToMany
{
    return $this->belongsToMany(
        Product::class, 
        'order_items', 
        'user_id', 
        'product_id'
    );
}

public function paidProducts(): BelongsToMany
{
    return $this->products()
        ->whereRelation('orders', 'paid', true);
}

public function hasPaidForProduct(Product|string $product): bool
{
    if($product instanceof Product) {
        $product = $product->getKey();
    }

    return $this->paidProducts()
        ->wherePivot('product_id', $product)
        ->exists();
}

產品:

public function orders(): BelongsToMany 
{
    return $this->belongsToMany(
        Order::class, 
        'order_items', 
        'product_id', 
        'order_id'
    );
}

public function users(): BelongsToMany 
{
    return $this->belongsToMany(
        User::class, 
        'order_items', 
        'product_id', 
        'user_id'
    );
}

命令:

public function products(): BelongsToMany 
{
    return $this->belongsToMany(
        Product::class, 
        'order_items', 
        'order_id', 
        'product_id'
    );
}

public function user(): BelongsTo 
{
    return $this->belongsTo(User::class);
}

創建訂單將如下所示:

$user = $request->user();

$products = ... // e.g. Cart::getItems();

$orderData = [
    'paid' => false,
    ...
];

$attachData = [];

// Here, we are preparing order products and the additional data, 
// aka user_id, for order_items table. The table will be like this:
// +----------+------------+---------+
// | order_id | product_id | user_id |
// +----------+------------+---------+
// |        1 |          1 |       1 |
// |        1 |          2 |       1 |
// |        1 |          3 |       1 |
// +----------+------------+---------+
foreach($products as $product) {
    $attachData[$product->id] = [
        'user_id' => $user->id,
    ];
}

$order = DB::transaction(function() use ($user, $orderData, $attachData) {
    $order = $user->orders()
        ->create($orderData);

    $order->products()
        ->attach($attachData);

    return $order;
});

然后;

// All products that the user has ordered.
$user->products;

// All products that the user has ordered and paid for.
$user->paidProducts;

$inquiredProduct = Product::findOrFail(1);

$user->hasPaidForProduct($inquiredProduct) // true/false

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM