Laravel relationships on a table with two types of flags

假如想象 提交于 2020-01-15 03:55:13

问题


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();


来源:https://stackoverflow.com/questions/35057540/laravel-relationships-on-a-table-with-two-types-of-flags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!