Laravel 5.5 更新多对多关系模型的复选框

3
我有一个多对多的关系,其中有两个模型彼此属于许多。因此,我想更新中间表,但首先我想知道这两个模型是否已经存在于中间表中。以下是我的两个模型中关系的定义:分析请求模型。
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 from methodologies 内部连接< code> analysis_request_methodologies on methodologies .< code> id = analysis_request_methodologies methodologies_id where analysis_request_methodologies analysis_request_id = 10)

参考问题中,解决方案表示我需要使用lists,但在Laravel 5中已被弃用,需要改用pluck。但仍然无法解决问题。

如果有人能帮忙解决,将不胜感激。
提前致谢。


你尝试过使用 ->pluck('methodologies.id'); 吗?就像这里所示 https://dev59.com/i1kR5IYBdhLWcg3w7hXE#40636391 - Vladyslav Startsev
1个回答

2
我假设您的相关表名为methodologies,因此您的查询应如下所示。
$checkeds = AnalysisRequest::find($id)->request_methods()->pluck('methodologies.id');

您需要指定表名,因为Eloquent在创建查询并加入表时,当您使用request_methods()时,MySQL需要知道id属于哪个表。


我尝试了一下,但现在出现了一个错误,提示“in_array()期望参数2为数组,但给定的是对象”。 - Jaaayz
已经使用 toArray() 方法修复了它,非常感谢! - Jaaayz
@jaaayz 使用 toArray() 方法,pluck 返回集合。 - Teoman Tıngır
感谢您:)) - Jaaayz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接