I have been trying to debug a problem for the past two days, my code, as follows, is first attempting to retrieve the category by its:
Controller
public function index(Request $request) { $stats = [ 'done' => UserTransaction::where('status', UserTransaction::STATUS['done']), 'canceled' => UserTransaction::where('status', UserTransaction::STATUS['canceled']), 'waiting_payment' => UserTransaction::where('status', UserTransaction::STATUS['waiting_payment']), ]; return view('admin.transaction.index', compact('stats')); }
UserTransaction.php
const STATUS = [ 'done' => 'done', 'canceled' => 'canceled', 'waiting_payment' => 'waiting_payment', ];
index.blade.php
<h3 class="info">{!! $stats['done'] !!}</h3>
I see this error
Object of class IlluminateDatabaseEloquentBuilder could not be converted to string (View: C:xampp3htdocsprojectswebafratessa-adminModulesTransactionResourcesviewsadminindex.blade.php)
Advertisement
Answer
I think you need to modify your query
$stats = [ 'done' => UserTransaction::where('status', UserTransaction::STATUS['done'])->first(), 'canceled' => UserTransaction::where('status', UserTransaction::STATUS['canceled'])->first(), 'waiting_payment' => UserTransaction::where('status', UserTransaction::STATUS['waiting_payment'])->first(), ];
also make sure $stats['done']
return object
.you should fetch like $stats['done']->status
also you can improve your code a bit
create a scope method in your model
public function scopeStatus($query,$status){ $query->where('status',$status); }
then you can access like this
UserTransaction::status(UserTransaction::STATUS['done'])->first();
Also you can keep status array in constant separately in config or you have to create each scope for each status
Updated Since you are looking for count of the status
$stats = [ 'done' => UserTransaction::where('status', UserTransaction::STATUS['done'])->count(), 'canceled' => UserTransaction::where('status', UserTransaction::STATUS['canceled'])->count(), 'waiting_payment' => UserTransaction::where('status', UserTransaction::STATUS['waiting_payment'])->count(), ];