PHP Laravel Error: Object of class stdClass could not be converted to string

I am developing a feature for an app using Laravel, and I am coming across this weird error that I can't figure out. I have the following code as a helper function to my controller, which was working just fine before I added a call to the following function:

protected function f($p){

    $cIds = $cs->select('cs.id')->get();

    $cs = DB::table('cs')
            ->select('cs.id')
            ->join('pucs', 'cs.id', '=', 'pucs.c_id')
            ->where('pucs.p_id', '=', (string)$p->id)
            ->whereIn('cs.id', $cIds)->lists('cs.id');
    return $cs;
}

where pucs has a foreign key to cs and a column called p_id, and I only want to return rows where p_id = $p->id. $p is the result of a previous query, and $p->id is an int (I think?). When I open the page on my website, I get the following error:

Missing argument 1 for Illuminate\Database\Query\Builder::lists()

Does anyone have any insight as to what might be causing this problem? My initial thought was that $p->id was an int and that was causing the problem, so I cast it to a string using the (string) operator, as well as trying strval($p->id). I'm out of ideas.

UPDATE: using pluck('cs.id') gives the same error. I thought the issue might have been (string)$p->id, but when I comment out ->where('pucs.p_id', '=', (string)$p->id) I still get the same issue. For reference, I have a very similar line of code in the same helper file:

$bIds = $bs->select('bs.id')->get();
$bs = DB::table('bs')
    ->whereIn('bs.id', $bIds)->lists('bs.id');

I'm pretty sure it was working before, even while I was getting errors on the earlier piece of code, but now it's not working either.

Anyway, the reason I'm doing this is because I have these awkward tables that have been joined and unioned with other tables, and then selected a subset of cs.id (or bs.id) from that, and I want just the result of the original tables. Also, I was getting some weird errors because MYSQL apparently has a problem sorting unioned tables by id?

Upvotes: 3

Views: 577

Answers (1)

FatBoyXPC
FatBoyXPC

Reputation: 951

When using lists(), you need to pass in the name of the key you want the values for. Normally you would call get() here instead of lists(), but if you call lists('cs.id') then you should get an array of the matching cs.id's.

Upvotes: 3

Related Questions