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:
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>
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
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

