Bring a value of a model to my view laravel

1

I am sending from my controller name,app,tlf,cel,fechadeingreso all this goes in my variable $clients . In my view it shows well, now I want to calculate the time that has elapsed since the date that client was entered, for this I created the function:

public function actividad($query)
    {
        $diff = $query->diffForHumans(Carbon::now());
        return $this->where('actividad','=',$diff);
    }

in my model User and I try to bring the answer from my view

<td>{{ $cliente->actividad->actividad }}</td>

but it shows me this error

  

Trying to get property of non-object (View:

how to bring that response to my sight?

    
asked by Andres Condo 18.10.2016 в 16:40
source

3 answers

1

Without understanding very well because you want to call a method that seems to be a subquery, you could easily do it in the controller, assuming you do not have another layer to which you are delegating this kind of tasks.

I am based on this sentence of the question:

  

I want to calculate the time that has elapsed since the date that client was admitted

You also say that you already have a property fechadeingreso in $cliente .

I would try something like this in the controller:

$fechaIngreso = Carbon::createFromTimestamp($cliente->fechadeingreso);

$tiempoTranscurrido = $fechaIngreso->diffForHumans();
    
answered by 18.10.2016 в 17:43
0

The problem is that you are trying to access a method of a property, and not of the model.

Try this way:

public function actividad()
    {

        $diff = Carbon::now()->diffForHumans(Carbon::createFromFormat('Y-m-d',$this->fechadeingreso));

        /*Cambiar Y-m-d por el formato que tengas*/

        return $diff;
    }

In the view:

<td>{{ $cliente->actividad() }}</td>
    
answered by 20.10.2016 в 12:34
0

It works good friend, I had to change my query of this / $ clients = \ DB :: table ('users')                 - > join ('clients', 'users.id', '=', 'clients.users_id') - > join ('provinces', 'clients.province_idprovince', '=', 'provinces.id')                 - > where ('users.is_admin', '=', '0')                 -> select ('users.status',' users.id ',' users.activity ',' clients.name ',' clients.yourname ',' clients.dir1 ',' clients.dir2 ',' clients. telephone ',' clients.path ',' clients.celular ',' clients.email ',' provinces.prov ')                 - > paginate (10); / to $ clients = User :: select ('users.status',' users.id ',' users.activity as salesdate ',' clients.name ',' clients.lastname ',' clients.dir1 ',' clients.dir2 ',' clients.phone ',' clients.path ',' clients.cell ',' clients.email ',' provinces.prov ')                 - > join ('clients', 'users.id', '=', 'clients.users_id')                 - > join ('provinces', 'clients.province_idprovince', '=', 'provinces.id')                 - > where ('users.is_admin', '=', '0')                 - > get (); to be able to obtain a value of the model, in my view I add {{$ customer- > activity ()}} and in my model public function activity ()     {         $ diff = Carbon :: now () - > diffForHumans (Carbon :: createFromFormat ('Y-m-d', $ this- > entrydate));         / Change Y-m-d by the format you have /         return $ diff;     } and everything works to the hair thanks to all for the support

    
answered by 21.10.2016 в 14:39