You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Eloquent:无关联跨表列相乘并展示的实现方案咨询

Hey there! Let's work through how to calculate those multiplied values between your Carts and Pricings tables using Laravel. Since you're already fetching data with the DB facade, we have a couple of straightforward approaches to get this done:


Approach 1: Calculate totals directly in the database query

Since both tables share the sp_id field, we can join them in the query and compute the multiplied values right at the database level (this is efficient because the heavy lifting happens before data hits your application).

Update your cart method like this:

public function cart($sp_id, $service_id) {
    // Join carts and pricings, then calculate line-item totals
    $cartWithTotals = DB::table('carts')
        ->join('pricings', 'carts.sp_id', '=', 'pricings.sp_id')
        ->where('carts.service_id', '=', $service_id)
        ->where('carts.sp_id', '=', $sp_id)
        ->where('carts.user_id', '=', auth()->user()->id)
        ->select([
            'carts.*',
            'pricings.Shirt as shirt_price',
            'pricings.Pant as pant_price',
            'pricings.Tie as tie_price',
            // Calculate multiplied totals with raw SQL
            DB::raw('carts.q_Shirt * pricings.Shirt as shirt_total'),
            DB::raw('carts.q_Pant * pricings.Pant as pant_total'),
            DB::raw('carts.q_Tie * pricings.Tie as tie_total')
        ])
        ->first(); // Use ->get() instead if you have multiple cart entries

    return view('your-cart-view', compact('cartWithTotals'));
}

Then in your Blade view, you can display the values cleanly:

<div class="cart-summary">
    <p>Shirts: {{ $cartWithTotals->q_Shirt }} × {{ $cartWithTotals->shirt_price }} = {{ $cartWithTotals->shirt_total }}</p>
    <p>Pants: {{ $cartWithTotals->q_Pant }} × {{ $cartWithTotals->pant_price }} = {{ $cartWithTotals->pant_total }}</p>
    <p>Ties: {{ $cartWithTotals->q_Tie }} × {{ $cartWithTotals->tie_price }} = {{ $cartWithTotals->tie_total }}</p>
</div>

Approach 2: Calculate totals after fetching data (PHP/Collection level)

If you want to keep your queries separate (no joins), you can compute the totals once you've retrieved both the cart and pricing data.

Modify your cart method like this:

public function cart($sp_id, $service_id) {
    // Get pricing data for the service provider
    $prices = DB::table('pricings')
        ->where('sp_id', '=', $sp_id)
        ->first();

    // Get the user's cart entries
    $cart = DB::table('carts')
        ->where('service_id', '=', $service_id)
        ->where('sp_id', '=', $sp_id)
        ->where('user_id', '=', auth()->user()->id)
        ->first(); // Again, use ->get() for multiple entries

    // Calculate totals only if both datasets exist
    $lineTotals = [];
    if ($cart && $prices) {
        $lineTotals = [
            'shirt' => $cart->q_Shirt * $prices->Shirt,
            'pant' => $cart->q_Pant * $prices->Pant,
            'tie' => $cart->q_Tie * $prices->Tie
        ];
    }

    return view('your-cart-view', compact('cart', 'prices', 'lineTotals'));
}

And in your view:

@if($cart && $prices)
    <div class="cart-summary">
        <p>Shirts: {{ $cart->q_Shirt }} × {{ $prices->Shirt }} = {{ $lineTotals['shirt'] }}</p>
        <p>Pants: {{ $cart->q_Pant }} × {{ $prices->Pant }} = {{ $lineTotals['pant'] }}</p>
        <p>Ties: {{ $cart->q_Tie }} × {{ $prices->Tie }} = {{ $lineTotals['tie'] }}</p>
    </div>
@endif

Handling multiple cart entries

If your cart query returns multiple rows (using ->get()), use Laravel's Collection methods to add totals to each item:

public function cart($sp_id, $service_id) {
    $prices = DB::table('pricings')->where('sp_id', '=', $sp_id)->first();

    $cartItems = DB::table('carts')
        ->where('service_id', '=', $service_id)
        ->where('sp_id', '=', $sp_id)
        ->where('user_id', '=', auth()->user()->id)
        ->get();

    // Add totals to each cart item using collection map
    $cartItemsWithTotals = $cartItems->map(function($item) use ($prices) {
        $item->shirt_total = $item->q_Shirt * $prices->Shirt;
        $item->pant_total = $item->q_Pant * $prices->Pant;
        $item->tie_total = $item->q_Tie * $prices->Tie;
        return $item;
    });

    return view('your-cart-view', compact('cartItemsWithTotals', 'prices'));
}

Then loop through the items in your view:

@foreach($cartItemsWithTotals as $item)
    <div class="cart-item">
        <h4>Cart Entry #{{ $item->id }}</h4>
        <p>Shirts: {{ $item->q_Shirt }} × {{ $prices->Shirt }} = {{ $item->shirt_total }}</p>
        <p>Pants: {{ $item->q_Pant }} × {{ $prices->Pant }} = {{ $item->pant_total }}</p>
        <p>Ties: {{ $item->q_Tie }} × {{ $prices->Tie }} = {{ $item->tie_total }}</p>
    </div>
@endforeach

内容的提问来源于stack exchange,提问作者mkirr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:27:54