0

I have this query en php laravel

$query = DB::select("SELECT * FROM solicitudes WHERE id = 5");

My table "solicitudes" have the columns "id, titulo, proveedor"

The query result is only one row, and I want to take the value of "titulo". I try it with something like this...

$nombrep = 'nombre'.$query->titulo;

but this throws an error

Trying to get property 'id' of non-object

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

The DB::select returns array. And If you want use -> . Try this:

$query = DB::select("SELECT * FROM solicitudes WHERE id = 5");
$query = Illuminate\Support\Collection::make($query);

$nombrep = 'nombre'.$query[0]->titulo;
Luocj
  • 1
0

Considering you have an Eloquent Model for solicitudes table :

$result = Solicitude::findOrFail(5);

This will return a single instance representing single row having id(primary key) is 5. If will throw error is there is no row present with id as 5;

When you use :

$query = DB::select("SELECT * FROM solicitudes WHERE id = 5");

It gives an array. And secondly even if you are anticipating this query should return a single array or object, it does not.

It returns an array which contains the single object representing the row.

If you have models defined, then using ELoquent query builders is the cleaner thing to do.

Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37