Issue
i have a problem, i need to add the debt of a user
controller:
public function cliente($id){
// $nota = App\Nota::find($id);
//Aquí valida si existe sino redirije al 404
$datos = [
'category_name' => 'datatable',
'page_name' => 'multiple_tables',
'registro' => Registro::find($id),
];
$cliente = \App\Models\User::findOrFail($id);
return view('cliente', compact('cliente'))->with($datos);
}
blade.php
<div class="row">
<div class="col">
<h4>Debt Sum:</h4>
</div>
<div class="col">
{{ $registro->user_id->sum('deuda') }}
</div>
Database:
i need to add the field "deuda" of the user, the relationship is in user_id with the users table
the error that I get is: Trying to get property 'user_id' of non-object
help pls
Solution
It's better to calculate sum of the registrations in your controller:
public function cliente($id){
// $nota = App\Nota::find($id);
//Aquí valida si existe sino redirije al 404
$datos = [
'category_name' => 'datatable',
'page_name' => 'multiple_tables',
'registro' => Registro::find($id),
];
$cliente = \App\Models\User::findOrFail($id);
$sum = Register::where('user_id', $cliente->id)->sum('deuda');
return view('cliente', compact('cliente','sum'))->with($datos);
}
and in your view use sum variable
<div class="row">
<div class="col">
<h4>Debt Sum:</h4>
</div>
<div class="col">
{{ $sum ? $sum : 0 }}
</div>
Answered By - Mohammad Hosseini

0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.