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

bincy123's avatar

Query result from database

I have two tables : galleries and shared galleries. Structure of galleries: (for storing images of individual students. One student contains multiple images)

id, student_id, classroom_id, image

Structure of shared gallery (for storing images which are common to all students in a classroom. One classroom contains many images):

id,classroom_id,image

I need to get a query so that I can display the images stored in 'galleries' for a student and those stored in shared gallery of the classroom in which that student belongs in a single page. How can I achieve this ? Something like this returns duplicated results :

select galleries.id as gid, shared_galleries.id as sid,galleries.student_id, galleries.classroom_id from galleries inner join shared_galleries on galleries.classroom_id=shared_galleries.classroom_id where galleries.student_id=31 and galleries.classroom_id=28

0 likes
17 replies
Vilfago's avatar

Use or instead of and in your where clause

But isn't better to have :

  • images table
  • students table (with a column classroom_id)
  • classroom table
  • image_student (to link the many to many relation between both)
  • classroom_image (for the same as above between image and classroom)
lanatel's avatar

In your Student model

public function galleries()
{
        return $this->hasMany(Gallery::class);
}

public function classroom()
{
        return $this->belongsTo(Classroom::class);
}

In Classroom model:

public function galleries()
{
        return $this->hasMany(SharedGallery::class);
}

And than you can do:

Student::with(['galleries', 'classroom.galleries'])->get();
Vilfago's avatar

select galleries.id as gid, shared_galleries.id as sid,galleries.student_id, galleries.classroom_id from galleries cross join shared_galleries on galleries.classroom_id=shared_galleries.classroom_id where galleries.student_id=31 or galleries.classroom_id=28

bincy123's avatar

@lanatel How can I add a where condition to the code Student::with(['galeries', 'classroom.galleries'])->get();

to get the data of a student

lanatel's avatar

@bincy123

Student::where('id', $student_id)->with(['galeries', 'classroom.galleries'])->first();
lanatel's avatar

@bincy123 can you dump your result? You should get results from both tables by using $student->galleries and $student->classroom->galleries.

lanatel's avatar

@bincy123 if you don't get any errors, be sure to fill your db tables with data. Maybe you just get empty result

bincy123's avatar

I do have data in database and I got data for $student->galleries , but got the error 'Trying to get property of non-object' while printing $student->classrooms->galleries

This is my Student model :

namespace App;

use Illuminate\Database\Eloquent\Model;

class Student extends Model { public function parentDetails(){ return $this->hasOne('App\ParentDetail'); }

public function classrooms(){
    return $this->belongsTo('App\Classroom');
}

public function galleries(){
    return $this->hasMany('App\Gallery');
}

}

And this is my Classroom model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Classroom extends Model {

public function users(){
    return $this->belongsToMany('App\User');
}

public function students(){
    return $this->hasMany('App\Student');
}

public function galleries()
{
    return $this->hasMany('App\SharedGallery');
}

}

lanatel's avatar

first of all rename you classrooms relation to classroom. And maybe try just get $student->classroom. Are there any results?

bincy123's avatar

Yea now I got the result. But can I use this array of data to paginate them

Vilfago's avatar

If your objective is to have x image per page that come from shared image or not, I think my suggestion of database will make your life easier.

If you can't change it, you probably must create your own pagination as you have to paginate from 2 different tables.

Vilfago's avatar

I thought my first reply was enough, but here more information :

Tables

| images |
| -- | -------- | ----- |
| id | alt_text | image |

| students |
| -- | ---- | ------------ |
| id | name | classroom_id |

| classrooms |
| -- | ---- | --- |
| id | name | ... |

| image_student |
| -------- | ---------- |
| image_id | student_id |

| classroom_image |
| -------- | ------------ |
| image_id | classroom_id |

| image_user |
| -------- | ------- |
| image_id | user_id |

Models

class Image extends Model {

    public function users(){
        return $this->belongsToMany('App\User');
    }

    public function students(){
        return $this->belongsToMany('App\Student');
    }

    public function classrooms(){
        return $this->belongsToMany('App\Classroom');
    }
}

class Student extends Model {

    public function images(){
        return $this->belongsToMany('App\Image');
    }

    public function students(){
        return $this->belongsTo('App\Classroom');
    }
}

class Classroom extends Model {

    public function students(){
        return $this->hasMany('App\Student');
    }

    public function images(){
        return $this->belongsToMany('App\Image');
    }
}

Query

$studentId = 31;
$student_with_images = Student::with('images')
    ->with('classroom.images')
    ->find($studentId);
dd($student_with_images);

I didn't test it, just wrote in this post. Hope it works without error.

1 like

Please or to participate in this conversation.