PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, March 18, 2022

[FIXED] I can't find the user and sum

 March 18, 2022     eloquent, laravel, laravel-5, laravel-8     No comments   

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:

enter image description here

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing