我有一个多对多的关系,其中有两个模型彼此属于许多。因此,我想更新中间表,但首先我想知道这两个模型是否已经存在于中间表中。以下是我的两个模型中关系的定义:分析请求模型。
在我的另一个模型中:方法论模型。
这两个模型都有自己的数据透视表,并附带所有它们的ID。现在我想发生的是,当这两个模型之间存在已经附加的模型时,我希望复选框在视图中被选中,但我无法实现。
这是数据透视模型。
以下是我的代码控制器部分:
"在我看来"
这是一个关于问题的另一个参考,与我的问题相关。
我在视图中遇到了一个错误,它说:
public function request_methods()
{
return $this->belongsToMany('App\Models\Methodologies')->withTimestamps();;
}
在我的另一个模型中:方法论模型。
public function methods_requested()
{
return $this->belongsToMany('App\Models\AnalysisRequest')->withTimestamps();;
}
这两个模型都有自己的数据透视表,并附带所有它们的ID。现在我想发生的是,当这两个模型之间存在已经附加的模型时,我希望复选框在视图中被选中,但我无法实现。
这是数据透视模型。
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class AnalysisRequestMethodologies extends Model
{
public $table = 'analysis_request_methodologies';
public $fillable = [
'analysis_request_id',
'methodologies_id',
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'analysis_request_id' => 'integer',
'methodologies_id' => 'integer',
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'analysis_request_id' => 'required|unique_with:analysis_request_methodologies,methodologies_id',
'methodologies_id' => 'required',
];
}
以下是我的代码控制器部分:
public function edit($id)
{
$analysis_request = $this->analysisrequestRepository->findWithoutFail($id);
$checkeds = AnalysisRequest::find($id)->request_methods()->pluck('id');
return view('encoder-dashboard.analysis-request-methods.create', compact('analysis_request', 'checkeds'));
}
"在我看来"
<div class="form-group col-sm-12">
@forelse($analysis_request->category->methodology as $method)
<label>
<?php $checked = in_array($method->id, $checkeds) ? true : false; ?>
{{ Form::checkbox('methodologies_id[]', $method->id) }}
{{ $method->name }} with the lead time of {{ $method->lead_time }} and the DOH standard of {{ $method->doh_standard }}
</label>
@empty
<h3>The category you selected has no method yet</h3>
@endforelse
</div>
这是一个关于问题的另一个参考,与我的问题相关。
我在视图中遇到了一个错误,它说:
完整性约束冲突:1052列“id”在字段列表中是模糊的(SQL:select
id
frommethodologies
内部连接< code> analysis_request_methodologies onmethodologies
.< code> id =analysis_request_methodologies
的methodologies_id
whereanalysis_request_methodologies
的analysis_request_id
= 10)
在参考问题中,解决方案表示我需要使用lists
,但在Laravel 5中已被弃用,需要改用pluck
。但仍然无法解决问题。
如果有人能帮忙解决,将不胜感激。
提前致谢。
->pluck('methodologies.id');
吗?就像这里所示 https://dev59.com/i1kR5IYBdhLWcg3w7hXE#40636391 - Vladyslav Startsev