douyi8732 2019-03-27 17:30
浏览 85

在关系中使用变量

I have some customfield functionality on my site where users can add custom fields to a model.

I've just found that if you enter data into one of the fields the same field won't show up for another model because of the way I get the empty fields for a model

public function Customfield()
{
    return $this->hasMany(Customfield::class);
}

public function getEmptyCustomfields($model)
{
    return  $this->has('customfield', '<', 1)
                        ->select('customfieldslabels.id as label_id', 'customfieldslabels.datatype as datatype', 'customfieldslabels.label_name as label_name',
                                'customfieldslabels.customfield_tab_id as customfield_tab_id', 'customfieldstabs.name as name')
                        ->join('customfieldstabs', 'customfieldstabs.id', '=', 'customfieldslabels.customfield_tab_id')
                        ->where('customfieldstabs.model', '=', $model)
                        ->get();

}

public function getEmptyCustomfieldsByTab($tabIDm)
{
    return $this->has('customfield', '<', 1)->where('customfield_tab_id', '=', $tabID)->get();
}

This is what I had originally and I realised that if a label has a field that is assigned to another model then the relationship exists and won't return as empty for another model.

So I'm trying to also check against the model ID now but I'm not quite sure how to use the model ID while checking the relationship

public function Customfield()
{
    return $this->hasMany(Customfield::class);
}

public function customfieldByModel($modelID)
{
    return $this->has('customfield' => function ($query) use ($modelID){
        $query->where('model_id', '=', $modelID);
    })->get();
}

public function getEmptyCustomfields($model, $modelID)
{
    return  $this->has($this->customfieldByModel($modelID), '<', 1)
                        ->select('customfieldslabels.id as label_id', 'customfieldslabels.datatype as datatype', 'customfieldslabels.label_name as label_name',
                                'customfieldslabels.customfield_tab_id as customfield_tab_id', 'customfieldstabs.name as name')
                        ->join('customfieldstabs', 'customfieldstabs.id', '=', 'customfieldslabels.customfield_tab_id')
                        ->where('customfieldstabs.model', '=', $model)
                        ->get();

}

public function getEmptyCustomfieldsByTab($tabIDm, $modelID)
{
    return $this->has($this->customfieldByModel($modelID), '<', 1)->where('customfield_tab_id', '=', $tabID)->get();
}

I would like to return empty fields for one model that they haven't been filled in for yet.

They return just find once there is data for a model assigned to a field as it doesn't need to check for empty fields anymore.

UPDATE

I've tried using a left join which I think will work but I can't get to the method because I get an error saying: 'The method name must be a string'

I changed this

public function getEmptyCustomfieldsByTab($tabID, $modelID)
{
    return $this->has($this->customfieldByModel($modelID), '<', 1)->where('customfield_tab_id', '=', $tabID)->get();
}

To this:

public function getEmptyCustomfieldsByTab($tabID, $modelID)
{
    return $this->has('customfieldbymodel', '<', 1)->where('customfield_tab_id', '=', $tabID)->get();
}

But now I can't send the $modelID as a parameter. Is it possible to sned a parameter while doing a has?

MY SOLUTION

I have created a protected variable on the model that just gets reassigned everytime.

protected $modelID;


public function customfieldByModel()
{
    return $this->hasMany(Customfield::class)->where('model_id', '=', $this->modelID);
}

public function getEmptyCustomfields($model, $modelID)
{
    $this->modelID = $modelID;
    return  $this->has('customfieldByModel', '<', 1)
                        ->select('customfieldslabels.id as label_id', 'customfieldslabels.datatype as datatype', 'customfieldslabels.label_name as label_name',
                                'customfieldslabels.customfield_tab_id as customfield_tab_id', 'customfieldstabs.name as name')
                        ->join('customfieldstabs', 'customfieldstabs.id', '=', 'customfieldslabels.customfield_tab_id')
                        ->where('customfieldstabs.model', '=', $model)
                        ->get();

}

public function getEmptyCustomfieldsByTab($tabID, $modelID)
{
    $this->modelID = $modelID;
    return $this->has('customfieldByModel', '<', 1)->where('customfield_tab_id', '=', $tabID)->get();
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大