in my larvel blade template i have a table in which i want to add another column after this code
<td>{{format_price($mission->amount)}}</td>
i added this
@php
$amount_to_be_collected = DB::table('shipments')
->select('amount_to_be_collected')
->where('mission_id', $mission->id)
->get();
@endphp
<td>{{format_price($amount_to_be_collected)}}</td>
could someone please tell me what is wrong with this code thanks in advance
CodePudding user response:
First of all, You should not put DB query code in your blade.
Now, when you run a query using eloquent and call get()
, the response is a Collection::class
instance that can be treated as an array but cannot be automatically transformed into a number/string.
If you only need the value of on field for one entry, use value()
instead.
$amount_to_be_collected = DB::table('shipments')
->where('mission_id', $mission->id)
->value('amount_to_be_collected');
CodePudding user response:
Note: It is not good to use db queries in blade views, you can also use helper
Try this before using DB facade you need to call this class in blade view Running Database Queries
use Illuminate\Support\Facades\DB;