mercredi 20 mai 2020

Laravel hasmanythrough or mysql groupby

I have three tables

category

id | category_name | created_at | updated_at

profile

id | category_id | profile_name | created_at | updated_at

photos

id | profile_id | photo_name | photo_path | photo_type | created_at | updated_at

Now i need to list photos by category and also only one photo from each profile by photo_type

I have tried using hasManyhrough but descending by created_at not working

public function categoryPhotos()
  {
        return $this->hasManyThrough(ProfilePhoto::class,Profile::class);


  }


 $response=Category::with(['categoryPhotos'=>function($query){
            $query->where('profile_photos.photo_type',2);
            $query->orderBy('profile_photos.created_at','ASC');

          $query->groupBy('profile_photos.profile_id');

        }])->whereHas('categoryPhotos')->get();

if its not possible via laravel relation then mysql query also fine for me.Thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire