How to insert pivot table in case of many to many relation? (Laravel 5.3)

moses toh

My form to add data is like this :

enter image description here

When klik save, It will call controller

My controller is like this :

public function store(Request $request)
{
    $param = $request->only('account_name','account_number','bank_id','branch');
    $result = $this->user_service->addUserBank($param);

    if($result)
        $status='success';
    else
        $status = 'failed';
    return redirect('member/profile/setting/account')->with('status',$status);
}

My service is like this :

public function addUserBank($param)
{
    $instance = User::where('id', '=', auth()->user()->id)->first();
    $param['user_id'] = auth()->user()->id;
    $param['status'] = 0;
    $instance->banks()->attach([
                'status' => $param['status'], 
                'account_name' => $param['account_name'],
                'account_number' => $param['account_number'],
                'branch' => $param['branch']
            ]);
    return $result;
}

My model user is like this :

<?php
namespace App;
use App\Models\MasterData;
use Collective\Html\Eloquent\FormAccessible;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
    use Notifiable, FormAccessible;
    protected $fillable = [
        'name', 'email', 'password', 'api_token','birth_date','mobile_number','gender','full_name'
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];
    public function banks()
    {
        return $this->belongsToMany(MasterData::class, 'users_banks', 'user_id', 'bank_id')  ->withPivot('status','account_name','account_number','branch')->withTimestamps();
    }
}

So I have 3 table : users table, users_banks table (pivot table), and master_datas table

List of the names of the banks located in the master_datas table with type bank

Users table have field id, name, email, password etc => See model user

Master_datas table have field id (this is bank id), name (this is bank name), type (there exist type of bank, order status etc. So, get type = bank)

Users_banks table have field id, user_id, bank_id, status, account_name, account_number, branch

When run, it does not successfully insert into the pivot table (table users_banks).

It looks like my way to insert into the pivot table, not true.

Can you help me?

Additional

Table Master_datas is like this :

enter image description here

Amit Gupta

The problem is that you are not passing bank_id in your addUserBank() method. you can do it as:

public function addUserBank($param)
{
    $param['status'] = 0;
    auth()->user()
          ->banks()
          ->attach($param['bank_id'], array_only($param, ['status', 'account_name', 'account_number', 'branch']);
    return true;
}

Note: You don't need to set user_id explicitly here as Laravel will automatically do it for you.

Docs

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel Many to Many, how to insert/get data inside the pivot table?

retrieve data from pivot table many to many relation ship laravel 5

Laravel many many relation cannot get pivot table

Laravel 8 Eloquent Many To Many insert into pivot table not working

how to insert one to many relation in laravel?

Setting property in pivot table in many to many relation

How can i filter pivot table with One to Many relation inside - Laravel 7

insert data with many to many relation in Laravel Eloquent

how to save many data in pivot table in laravel

Laravel 8: How to seed a pivot table in many to many relationship

save many to many relation in Laravel elequent 5?

Laravel 5.8 many to many relation using custom column names for pivot table

How to insert entity with many to many relation?

Laravel Many To Many Pivot Table different Database

Laravel Many-to-Many Pivot Table

Laravel Many to Many relation

Laravel: Get pivot data for specific many to many relation

Laravel / Eloquent - Many to many pivot model with morphToMany relation

Codeigniter Insert into Many-to-Many Joining Table/Pivot Table

How do I insert values into a many:many relation table for posts and tags?

How to insert constant value in a join table with Hibernate in a many-to-many relation?

Laravel - Eloquent relation - many-to-many - get intermediate table columns

Laravel many-to-many relation with custom table names and IDs

laravel 5.2 many to many relation retrieve data with intermediate table

Laravel: hasManyThrough relation to get many to many table values

How to select records in the case of a many to many relation with ponyorm

Laravel Many-to-Many Relation

Laravel self relation many to many

Search in many to many relation Laravel