Laravel relationships on a table with two types of

2019-07-07 16:37发布

问题:

I have two tables

products and users

Both of this objects has images associated with it in a table

images

The schema for the images table is

id | image_id | resource_id | flag

1 | 567575 | 1 | user

2 | 423423 | 1 | product

Based on this flag i am identifying whether its a users image or whether its a products image.

If I need to eager load a users image how do it do it?

User model

<?php

namespace App\Entities;

use Illuminate\Database\Eloquent\Model;

class User extends Model implements Transformable
{
    use TransformableTrait;
    protected $table      = 'users';
    protected $primaryKey = 'users_id';
    public function images()
    {
        return $this->hasMany('App\Entities\Image','resource_id');
    }
}

Product model

<?php

namespace App\Entities;

use Illuminate\Database\Eloquent\Model;

class Product extends Model implements Transformable
{
    use TransformableTrait;
    protected $table      = 'products';
    protected $primaryKey = 'products_id';
    public function images()
    {
        return $this->hasMany('App\Entities\Image','resource_id');
    }
}

images model

<?php

    namespace App\Entities;

    use Illuminate\Database\Eloquent\Model;

    class Image extends Model implements Transformable
    {
        use TransformableTrait;
        protected $table      = 'images';
        protected $primaryKey = 'images_id';
        public function products()
        {
            return $this->hasOne('App\Entities\Product','products_id');
        }
       public function users()
        {
            return $this->hasOne('App\Entities\User','users_id');
        }
    }

Is there a way I can pass a flag in the relationship function of images() so that it will fetch the record based on the flags?

Please help out.

回答1:

You can try this adding a conditional inside your images() method:

<?php

namespace App\Entities;

use Illuminate\Database\Eloquent\Model;

class User extends Model implements Transformable
{
    use TransformableTrait;
    protected $table      = 'users';
    protected $primaryKey = 'users_id';

    public function images($filtered=false)
    {
        if ($filtered) {
            return $this->hasMany('App\Entities\Image','resource_id')->where('flag','user');
        }
        return $this->hasMany('App\Entities\Image','resource_id');
    }
}

and try the same logic to your Product model



回答2:

Change your Image model to:

<?php

namespace App\Entities;

use Illuminate\Database\Eloquent\Model;

class Image extends Model implements Transformable
{
    use TransformableTrait;

    protected $table      = 'images';
    protected $primaryKey = 'images_id';

    public function product()
    {
        return $this->belongsTo('App\Entities\Product', 'products_id', 'resource_id');
    }

    public function user()
    {
        return $this->belongsTo('App\Entities\User', 'users_id', 'resource_id');
    }
}


回答3:

try with this way :

$user_id = 1;
$my_image= User::find($user_id)->images()->where('flag', '=', 'user')->first();