Reputation: 1563
I have a many to many relationship where I had a two models that belongsToMany to each other.
So I would like to update the pivot table but first I want to know if the two models are already existing on this pivot tables.
So here is the definition of my relationship in my two models below:
AnalysisRequest model
public function request_methods()
{
return $this->belongsToMany('App\Models\Methodologies')->withTimestamps();;
}
and in my other model: Methodologies model
public function methods_requested()
{
return $this->belongsToMany('App\Models\AnalysisRequest')->withTimestamps();;
}
So this two models has their own pivot table with all their ID attached. Now what I want to happen is when the two models has existing attached models to each other I want the checkbox to be checked on the view which I couldn't achieve.
Here is the pivot model
<?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',
];
}
Here is my code controller below:
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'));
}
And in my view
<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>
Anothere reference of question that can be related to my question.
I have an error in my view that sais:
Integrity constraint violation: 1052 Column 'id' in field list is ambiguous (SQL: select
id
frommethodologies
inner joinanalysis_request_methodologies
onmethodologies
.id
=analysis_request_methodologies
.methodologies_id
whereanalysis_request_methodologies
.analysis_request_id
= 10)
In the referenced question the solution said I need to use lists
but it was depreciated in Laravel 5 I need to use pluck
instead. But still can't solve the puzzle.
Appreciate if someone could help. Thanks in advance.
Upvotes: 3
Views: 916
Reputation: 2901
I assumed that your related table name is methodologies
, so your query should be like below
$checkeds = AnalysisRequest::find($id)->request_methods()->pluck('methodologies.id');
You have to specify the table name because eloquent creating a query and joining tables when you say that request_methods()
mysql should know which table is id belongs to
Upvotes: 2