lundi 26 octobre 2015

Two models into one paginated query with eager loaded relations using Laravel

I have two Models that I would like to merge into one timeline. I have been able to do this by creating a View in mysql that normalizes and unions the tables. I created a Model for this view, NewsFeed. This works well if I do not want related Comment model. I have gotten close to this by overriding the getMorphClass method on the model. This allows me to get the related comments for the pictures, but not the posts, because when getMorphClass is called the model doesn't have any data.

I am open to any approach on how to solve this, not just the way I am proposing.

NewsFeed

    <?php

    namespace App\Users;

    use App\Pictures\Picture;
    use App\Social\Comments\CommentableTrait;
    use App\Posts\Post;
    use App\Users\User;
    use Illuminate\Database\Eloquent\Model;

    class UserFeed extends Model
    {
        use CommentableTrait;

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

        public function getMorphClass(){
            if ($this->type == 'post'){
                return Post::class;
            }
            return Picture::class;
        }
    }

MySQL View

CREATE VIEW 
   `user_feeds`
AS SELECT
   `posts`.`id` AS `id`,
   `posts`.`user_id` AS `user_id`,
   'post' AS `type`,
   NULL AS `name`,
   NULL AS `thumbnail`,
   `posts`.`body` AS `body`,
   `posts`.`updated_at` AS `updated_at`,
   `posts`.`created_at` AS `created_at`
FROM 
    `posts` 
UNION SELECT
    `pictures`.`id` AS `id`,
    `pictures`.`user_id` AS `user_id`,
    'picture' AS `type`,
    `pictures`.`name` AS `name`,
    `pictures`.`thumbnail` AS `thumbnail`,
    `pictures`.`description` AS `body`,
    `pictures`.`updated_at` AS `updated_at`,
    `pictures`.`created_at` AS `created_at` 
FROM
    `pictures`;

pictures table

    id
    user_id
    title
    img
    img_width
    img_height
    img_other
    description
    created_at
    updated_at

posts

    id
    user_id
    title
    body
    created_at
    updated_at



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire