user3557451
user3557451

Reputation: 134

Laravel eloquent issue getting db datas

I'm having a problem getting some datas from my database via eloquent:

at first I do this to get 'id' and 'capMax' from clase_schedule table:

$plazas = DB::table('clase_schedule')->select(['schedule_id', DB::raw('SUM(capMax) as capMax')])->groupBy('schedule_id')->get();

and then i want to get the the name of table schedule where the id i got before coincides with the one on this table, like this:

$nom_horarios = DB::table('schedule')->select('name')->where('id', 'in', $plazas->schedule_id)->get();

I get this error:

"Trying to get property of non-object"

Upvotes: 1

Views: 38

Answers (3)

Drudge Rajen
Drudge Rajen

Reputation: 7995

Since, $plazas is an array so you should loop the $plazas to get the value in next query as below :

  foreach ($plazas as $plaza) 
    {
     $nom_horarios[] = DB::table('schedule')->select('name')->where('id', 'in',$plaza->schedule_id)->get(); 
    } 
    print_r($nom_horarios);

Upvotes: 2

Rômulo M. Farias
Rômulo M. Farias

Reputation: 1523

What is returning in the $plazas variable?

I think the problem is you are getting a collection and not a item... If you want to get just one plaza you should ->first() instead of ->get();

Upvotes: 0

Vilius
Vilius

Reputation: 1026

$plazas returns an array of items, instead of get(), use first(). This will return single item.

$plazas = DB::table('clase_schedule')->select(['schedule_id', DB::raw('SUM(capMax) as capMax')])->groupBy('schedule_id')->first();

Or loop plazas in foreach

Upvotes: 0

Related Questions