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

Zaphieon's avatar

converting sql query to laravel ( 5.2 )

I have this sql query -

SELECT t1.id as album_id, t3.id as photo_id, t3.hash FROM ow_photo_album t1 JOIN ow_protectedphotos_passwords t2 ON t2.albumId = t1.id JOIN ow_photo t3 ON t3.albumId = t1.id WHERE t1.userId = '$id' AND t2.privacy = 'password' ORDER BY t3.id ASC

what i need this for is to pull specific 2 collumns from the 3rd join that has to be filtered by the 2nd join.

I can pull these with {{$data->photo_id}}{{$data->hash}} wrapped in a '@foreach($photo_album as $data)' correct?

just do not know exactly how to convert this.. just now getting into laravel, so any help will be great.

0 likes
5 replies
jlrdw's avatar

With no lessons completed, I'd highly suggest viewing some videos, many are free. In fact one of the free ones Jeffrey covers some basic relations.

Code is also free on Github to download and study.

Usually a member attempts something and if they get stuck, then ask.

Also format code with backticks. https://help.github.com/categories/writing-on-github/

Zaphieon's avatar

lol I have attempted it of course. here is my laravel code.

protected $table = 'photo_album as t1';

public function user()
{
    return $this->belongsTo(User::class, 'userId');
}

public static function getPhotoAlbum($userId)
{
    $result = self::select('t1.id as album_id', 't3.id as photo_id', 't3.hash')
              ->join('ow_protectedphotos_passwords as t2' , 't2.albumId', '=', 't1.id')
              ->join('ow_photo as t3' , 't3.albumId', '=', 't1.id')
              ->where('t1.userId', $userId)
              ->where('t2.userId', 'password')->get();

    return $result;
}
jlrdw's avatar
jlrdw
Best Answer
Level 75

This don't look right:

->where('t2.userId', 'password')

'password' ?

Zaphieon's avatar

late night coding ... .

should be

->where('t2.privacy', 'password')->get();

that fixed my issue.. thanks

Please or to participate in this conversation.