Laravel relationships on a table with two types of

2019-07-07 16:59发布

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.

3条回答
啃猪蹄的小仙女
2楼-- · 2019-07-07 17:30

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

查看更多
爷的心禁止访问
3楼-- · 2019-07-07 17:32

try with this way :

$user_id = 1;
$my_image= User::find($user_id)->images()->where('flag', '=', 'user')->first();
查看更多
爷、活的狠高调
4楼-- · 2019-07-07 17:40

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');
    }
}
查看更多
登录 后发表回答