1

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"
4
  • what do you get on var_dump($plazas) ?? Commented Mar 16, 2016 at 11:15
  • I get an array with two values, schedule_id and capMax that is the result of sum(capMax) for each group of values: array:2 [▼ 0 => {#483 ▼ +"schedule_id": "2" +"capMax": "221" } 1 => {#482 ▼ +"schedule_id": "3" +"capMax": "12" } ] Commented Mar 16, 2016 at 11:20
  • if you are getting array then you should use loop to query again with the value in array . Commented Mar 16, 2016 at 11:29
  • doing the foreach i dont know why i just get the first one with this: foreach ($plazas as $plaza) { $nom_horarios = DB::table('schedule')->select('name')->where('id', 'in', $plaza->schedule_id)->get(); } Commented Mar 16, 2016 at 11:30

3 Answers 3

2

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);
Sign up to request clarification or add additional context in comments.

2 Comments

This one got the solution, i was missing the array sign in $nom_horarios thnks
Great !! If you find this answer helpful then please tick it so that it can be useful to others in future ::) .
0

$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

4 Comments

the thing is that what i want to get is the name of each of the items in the array
doing the foreach i dont know why i just get the first one with this: foreach ($plazas as $plaza) { $nom_horarios = DB::table('schedule')->select('name')->where('id', 'in', $plaza->schedule_id)->get(); }
do var_dump($plazas->count()); ans see how many plazas you're returning
im getting two items on the array
0

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();

3 Comments

I get an array with two values, schedule_id and capMax that is the result of sum(capMax) for each group of values: array:2 [▼ 0 => {#483 ▼ +"schedule_id": "2" +"capMax": "221" } 1 => {#482 ▼ +"schedule_id": "3" +"capMax": "12" } ] What i want to get is the name of each id in table schedule
You can do $plazas->only('schedule_id'), that way you'll get just the ids, then call the in operator. Like this $nom_horarios = DB::table('schedule')->select('name')->where('id', 'in', $plazas->only('schedule_id'))->get();
Sorry, the only will get in separetad arrays still.. The map function can fix $this $plazas->map(function($obj){return $obj->schedule_id;});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.