Laravel 5.5 Update a checkbox of many to many relationship model

Jaaayz

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 from methodologies inner join analysis_request_methodologies on methodologies.id = analysis_request_methodologies.methodologies_id where analysis_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.

Teoman Tıngır

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

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Laravel 5 Eloquent Relationship - Has Many Though

分類Dev

Laravel save / update many to many relationship

分類Dev

View Many to Many Relationship in laravel

分類Dev

Friendship system with Laravel : Many to Many relationship

分類Dev

Laravel One to Many of One to Many relationship

分類Dev

ASP.NET MVC 5 - Scaffolding for Many to One Relationship

分類Dev

Many to Many relationship in Asp.Net MVC 5 with Identity table and Custom table

分類Dev

Laravel Many to Many and One to Many Table Model and Controller

分類Dev

Using Android Data Binding with many-to-many relationship in model

分類Dev

Eloquent ORM - retrieve models not related to the given model (Many to Many relationship)

分類Dev

Ef core 5 many to many filter

分類Dev

Laravel fetch only pivot columns in many to many relationship

分類Dev

Laravel, pivot table conflicts with many-to-many relationship

分類Dev

Laravel: Add a join to self referencing Many-to-Many relationship?

分類Dev

Synchronizing a one-to-many relationship in Laravel

分類Dev

How to insert a one to many relationship in Laravel

分類Dev

Many to many relationship CRUD in Room

分類Dev

Many-to-many relationship as list

分類Dev

backward filter and many to many relationship

分類Dev

Power Pivot Many to Many Relationship

分類Dev

Querying a many to many relationship in SQL

分類Dev

Too many fillable fields in model Laravel?

分類Dev

Attach model has_and_belongs_to_many relationship in rails

分類Dev

Convert existing many to many to reverse many to many relationship using XSLT

分類Dev

Flask/SQLAlchemy - Difference between association model and association table for many-to-many relationship?

分類Dev

Creating one to many relationship with pivot table of many to many relationship

分類Dev

Laravel Eloquent Eager Loading Multiple Has Many Relationship

分類Dev

Laravel 5:値に基づいてMany()を保存する方法

分類Dev

How to eager load only 1 result from many to many relationship Laravel

Related 関連記事

  1. 1

    Laravel 5 Eloquent Relationship - Has Many Though

  2. 2

    Laravel save / update many to many relationship

  3. 3

    View Many to Many Relationship in laravel

  4. 4

    Friendship system with Laravel : Many to Many relationship

  5. 5

    Laravel One to Many of One to Many relationship

  6. 6

    ASP.NET MVC 5 - Scaffolding for Many to One Relationship

  7. 7

    Many to Many relationship in Asp.Net MVC 5 with Identity table and Custom table

  8. 8

    Laravel Many to Many and One to Many Table Model and Controller

  9. 9

    Using Android Data Binding with many-to-many relationship in model

  10. 10

    Eloquent ORM - retrieve models not related to the given model (Many to Many relationship)

  11. 11

    Ef core 5 many to many filter

  12. 12

    Laravel fetch only pivot columns in many to many relationship

  13. 13

    Laravel, pivot table conflicts with many-to-many relationship

  14. 14

    Laravel: Add a join to self referencing Many-to-Many relationship?

  15. 15

    Synchronizing a one-to-many relationship in Laravel

  16. 16

    How to insert a one to many relationship in Laravel

  17. 17

    Many to many relationship CRUD in Room

  18. 18

    Many-to-many relationship as list

  19. 19

    backward filter and many to many relationship

  20. 20

    Power Pivot Many to Many Relationship

  21. 21

    Querying a many to many relationship in SQL

  22. 22

    Too many fillable fields in model Laravel?

  23. 23

    Attach model has_and_belongs_to_many relationship in rails

  24. 24

    Convert existing many to many to reverse many to many relationship using XSLT

  25. 25

    Flask/SQLAlchemy - Difference between association model and association table for many-to-many relationship?

  26. 26

    Creating one to many relationship with pivot table of many to many relationship

  27. 27

    Laravel Eloquent Eager Loading Multiple Has Many Relationship

  28. 28

    Laravel 5:値に基づいてMany()を保存する方法

  29. 29

    How to eager load only 1 result from many to many relationship Laravel

ホットタグ

アーカイブ