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

Bosstone's avatar

Fetch Data from two tables

Hi Folks,

I have 2 Tables (projects and images) which I can fetch like this:

$projects = Project::with(['images' => function($query){
        $query->where('state' , 0 );
      }])->whereHas('user', function ($query) use ($user) {
                        $query->where('id', '=', $user->id);

                    })->get();

The Project Table has a field "stat" where the stat of the project is defined:

0 = uploaded by user 1 = deleted by user 2 = verified by admin 3 = rejected.

The Image Table has a field "state" where 0 = ok, 1 = deleted

Currently I am fetching all the projects with images with state 0 from the logged in user. What can I do to fetch only the projects with stat = 0 || stat = 2 || stat = 3?

Thank you for your advice!

Stefan

0 likes
1 reply
Bosstone's avatar
Bosstone
OP
Best Answer
Level 1

Solved id:

$projects = Project::with(['images' => function($query){
        $query->where('state' , 0 );
      }])
      ->where('stat', '!=', '1')
      ->whereHas('user', function ($query) use ($user) {
                        $query->where('id', '=', $user->id);

                    })->get();

:-)

Please or to participate in this conversation.