2

I'm getting grade_id from the database:

$grade_id = DB::table('grades')->where('teacher_id',$teacher_id)->select('grade_id')->get();

and then I want to use that grade_id array in the where eloquent clause so I run

$home_feed = Home::join('home_grade', 'home_grade.home_id', '=', 'homes.id')
                               ->whereIn('grade_id', $grade_id)
                               ->get();

but when I run this I'm getting an error: Object of class stdClass could not be converted to string

What could be the problem? Thanks guys.

2
  • Try $grade_id = DB::table('grades')->where('teacher_id',$teacher_id)->select('grade_id')->first(); Commented Oct 9, 2016 at 13:18
  • 1
    Your first query probably returns collection object, so try $grade_id = DB::table('grades')->where('teacher_id',$teacher_id)->select‌​('grade_id')->get()-‌​>lists('grade_id')->‌​all(); or DB::table('grades')->where('teacher_id',$teacher_id)->pluck('grade_id')->all() then second query Commented Oct 9, 2016 at 15:34

4 Answers 4

2

Depending on laravels version your $grade_id is either an array or a collection of objects. What you need is an array or a collection of values. You can achieve that using the pluck() method insted of select() like IzzEps suggested.

But you can get the same result by passing a subquery to the whereIn() method:

$gradeSubquery = DB::table('grades')->where('teacher_id',$teacher_id)->select('grade_id');

$home_feed = Home::join('home_grade', 'home_grade.home_id', '=', 'homes.id')
                 ->whereIn('grade_id', $gradeSubquery)
                 ->get();

This way you will run only one query instead of two.

Update: Before version 5.2 you have to use lists() instead of pluck(). And the whereIn() method doesn't accept a Builder as second parameter. To get the same query you would need to use a closure:

$home_feed = Home::join('home_grade', 'home_grade.home_id', '=', 'homes.id')
    ->whereIn('grade_id', function($query) use($teacher_id) {
        $query->from('grades')
            ->where('teacher_id', $teacher_id)
            ->select('grade_id');
    })
    ->get();
Sign up to request clarification or add additional context in comments.

5 Comments

When I use the subquery I get the following error Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, object given
What is your laravel version?
@user3714932 i tested the following code on 5.2.39: $subQuery = DB::table('categories')->where('name', $catName)->select('id'); $products = App\Product::join('category_product as cp', 'cp.product_id', '=', 'products.id')->whereIn('category_id', $subQuery)->get();. And it executes the following working query: select * from `products` inner join `category_product` as `cp` on `cp`.`product_id` = `products`.`id` where `category_id` in (select `id` from `categories` where `name` = ?).
I'm using 5.0, maybe that's why
@user3714932 You're right - it's not supported before v5.2 :-( (just checked in laravels source code).
0

your first query is returning a collection, not the grade_id.

Try this instead: $grade_id = DB::table('grades')->where('teacher_id',$teacher_id)->pluck('grade_id');

2 Comments

Collection isn't the problem. The problem is that it contains objects, while whereIn() needs an array/collection of simple values. But your code seems to be correct.
When I use pluck only one result (the first one) is returned.
0

Using lists worked. $grade_id = Grade::where('teacher_id', $teacher_id)->lists('grade_id'); They return an array instead of a collection

Comments

0

You need to create the array correctly. To do this use two functions that Eloquent work with: pluck() and toArray(). Look at example below:

$grade_id = DB::table('grades')->where('teacher_id',$teacher_id)->pluck('grade_id')->toArray();

Comments

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.