Store row in variable

2

You see, let's imagine that we have a table called Type and imagine that I want to get only the element with value of% co_of% of 3. In that case, I would use this:

$tipo=Tipo::where('id',3)->get();

This would return a vector of a single element with the row of Type whose value of% co_of% is equal to 3. But I want it to be directly in a variable so I do not have to be putting id to do any operation with this row. How do I achieve it?

    
asked by Miguel Alparez 17.11.2018 в 13:53
source

2 answers

2

There are 2 problems with what you try to do first the query to get the result you want is

$tipo = Tipo::where('id','=',3)->first();

You need the = in your query, plus you are using get() that will always return the result in an array since it is used when you want to extract more than one result.

In your case you have to use first() , which returns a single record. So you can get the data from your table, for example:

$tipo->nombre;
    
answered by 17.11.2018 / 14:15
source
1

The answer that was accepted as correct, works, but could be simplified and improved a bit in addition to the fact that the part of the statement requiring the "=" is false, just as it is not necessary to use where ('id ',' 3 '), since for that you have a specific command find (), which could be written in a cleaner and more direct way.

$tipo = Tipo::find(3);

In addition to that, the part of placing the '=' is important to remember it, because with it you can use the rest of the symbols (& lt ;, & gt ;,).

Tipo::where('id','<',4)->get(); //(listado de todos los tipos con de menores a 4)
    
answered by 19.11.2018 в 23:47