Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AlexSteele's avatar

executing this kind of query possible?

I am trying to make this kind of query, but I am lost on how to do this in laravel. i know about the DB facade, but not how to structure this in there. any casts or posts that show it - please point me in their direction!!!

basically there are two pivot tables, the similar key therein is a stream id. once this runs, I will filter the array but I need to get it to run before I can filter it. this is the kind of code I would have done bfore.... not not sure what do do...

$makeItOne = "select * FROM " . 'project_stream' . " ps, " . 'stream_users' . " su WHERE ps.project_id = " . $project->id . " AND su.stream_id = ps.stream_id " ;

    $getEm = $db->Execute($makeItOne);
0 likes
4 replies
jlrdw's avatar

Use getPdo, https://laracasts.com/discuss/channels/guides/getpdo-usage

You should be able to write a regular query you familiar with.

I use regular queries quite often.

Just quick example

    public function getOne($dogid)
    {
        $sql = "SELECT * FROM " . PREFIX . "dogs WHERE dogid = :dogid";
        $sth = DB::getPdo()->prepare($sql);
        $params = [':dogid' => $dogid];
        $sth->execute($params);
        return $sth->fetch(\PDO::FETCH_OBJ);
    }

Make sure you import the DB facade.

1 like
thewebartisan7's avatar
Level 14

Laravel DB facade return a collection, so instead of using ->get() you can use ->first() , example:

$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;

In your case should be something like:

$users = DB::table('project_stream')
            ->leftJoin('stream_users', 'stream_users.stream_id', '=', 'project_stream.stream_id')
            ->where('project_stream.project_id', $project->id)
            ->first();

Reference in doc:

https://laravel.com/docs/8.x/queries#joins

https://laravel.com/docs/8.x/queries#retrieving-results

1 like
AlexSteele's avatar

this is very similar to what I wound up with!

AlexSteele's avatar

thank you to both responses, I am very grateful!

Please or to participate in this conversation.