4

Ok so i am trying to perform a mysql query to join to tables and return the results.

So in my controller i have an array of serviceIDs that when print_r() looks like this:

Array
(
    [0] => 50707
    [1] => 50709
)

The name of this array is $serviceIDS

Okay Then i now call a function from one of my models. This is a scope as seen below:

$services = Services::getWatchListInfo($serviceIDS)->get();

This is the scope function in my model:

public function scopegetWatchListInfo($serviceIDS){

    $services = DB::table('services')
                ->join('reviews','services.serviceID','=','reviews.serviceID')
                ->select('services.name','services.type','services.review_count_approved','reviews.escalate','reviews.average_rating')
                ->whereIn('serviceID',$serviceIDS);
    return $services;
}

Okay so this should get results from both my services and reviews table where service id is in the array.

Instead i am getting the following error.

Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, object given, called in /Users/user/sites/informatics-2/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 311 and defined

Any ideas?

1 Answer 1

2

You're not using Eloquent scopes correctly. If you read the docs, you're attempting to use a Dynamic Scope, so you need to define your scope as:

/**
 * getWatchList Eloquent Scope
 * 
 * @param  object $query
 * @param  array  $servicesIDS
 * @return object $query
 */
public function scopegetWatchListInfo($query, $serviceIDS = []) {
    return $query
        ->join('reviews','services.serviceID','=','reviews.serviceID')
        ->select('services.name','services.type','services.review_count_approved','reviews.escalate','reviews.average_rating')
        ->whereIn('serviceID', $serviceIDS);
}

The DB::table('services') should not be necessary if you're defining a Scope within your Services model (because the services table is automatically handled by Eloquent:

Now, let's look at an example Flight model class, which we will use to retrieve and store information from our flights database table

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a lot this worked! however the results i get back are actually 6 , when only 2 ids went in. So its bring back the same 2 results 3 times which is strange
I think that's probably happening due to join() statement. You can check the SQL query being generated by changing ->get() to ->toSql(). If you put that SQL into PHPMyAdmin (or whatever SQL app you're using), you'll be able to check the SQL that's being generated, and determine how to write the correct JOIN statement.
Thanks, it does the same in phpmyadmin, but i dont know why aha :(
Create a new question on StackOverflow, with your database structure, and your SQL. I (or someone else), will be able to help you with it.
its fine now i managed to sort it out. Thanks for the help :)

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.