2

我有几张桌子,想加入他们,使用雄辩的关系,但我不知道如何。

所有表格的图片:

所有表

第一个表(Leads)使用关系与表 sphere_attributes 连接:

Leads.sphere_id = sphere_attributes.sphere_id

从这个关系我想得到列“标签”。

这些标签的值存储在另一个表“sphere_attribute_options”的“值”列中。

Leads与 sphere_attribute_options 相关为:

    leads.sphere_id = sphere_attributes.sphere_id
    sphere_attributes.id = sphere_attribute_options.sphere_attr_id
    AND sphere_attribute_options.ctype=’agent’

我只需要来自 sphere_attribute_options 的这些值,其中表 sphere_bitmask_XX (XX = sphere_id) 的字段 fb_AID_OID=1 (AID= sphere_attributes.id, OID= sphere_attribute_options.id)。

sphere_bitmask_XX表和之间的关系leads

sphere_bitmask_XX.user_id=leads.id AND sphere_bitmask_XX.type='lead'</p>

我的模型:

带领

namespace App\Models;

use Cartalyst\Sentinel\Users\EloquentUser;

class Lead extends EloquentUser
{

    protected $table = "leads";

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'agent_id', 'sphere_id',
    ];


    public function sphere()
    {
        return $this->hasOne('App\Models\Sphere', 'id', 'sphere_id');
    }

    public function labels()
    {
        return $this->hasMany('App\Models\SphereAttr', 'sphere_id', 'sphere_id');
    }
}

SphereAttrOptions

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SphereAttrOptions extends Model
{
    protected $table = 'sphere_attribute_options';

    protected $fillable = ['sphere_attr_id', 'ctype', '_type', 'name', 'value', 'icon', 'position'];

    public function attribute()
    {
        return $this->belongsTo('App\Models\SphereAttr', 'id', 'sphere_attr_id');
    }
}

球体属性

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SphereAttr extends Model
{
    protected $table = 'sphere_attributes';
    protected $fillable = ['_type', 'label', 'icon', 'required', 'position'];

    public function options()
    {
        return $this->hasMany('App\Models\SphereAttrOptions', 'sphere_attr_id', 'id')
            ->where('ctype', '=', 'agent')->orderBy('position');
    }

    public function sphere()
    {
        return $this->belongsTo('App\Models\Sphere', 'id', 'sphere_id');
    }

}

球面罩

namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\Model;

class SphereMask extends Model
{

}
4

0 回答 0