pivot table is not attaching laravel relation

zbyshekh

here's my code:

Project::find($project)->users()->attach($user)// $project = '1', $user = '2'

and the error:

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `project_user` (`project_id`) values (2))

and if you need migration

Schema::create('project_user', function(Blueprint $table)
    {
        $table->increments('id');
        $table->bigInteger('project_id')->unsigned();
        $table->bigInteger('user_id')->unsigned();

What could be the problem?

Welder Lourenço

It looks like you have an error in your relationship signature, make sure it's as below:

// In your Project model

public function users()
{
    return $this->belongsToMany('App\User', 'project_user', 'project_id', 'user_id');
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Laravel observer not attaching on pivot table

Access relation of pivot table in Laravel

Attaching records to a Laravel hasManyThrough relation

Laravel relation from a pivot

Laravel many many relation cannot get pivot table

inserting data into database laravel 5.6 relation belongstomany pivot table

Laravel - one-to-one relation through pivot table with eager load

How can I write a relation for pivot table in Laravel?

How to assign relation when working with pivot table in Laravel 4?

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

PHP-Laravel get data from pivot table in manytomany relation

Laravel Eloquent get a relation that matches a value in pivot table

laravel belongsToMany self referencing attaching Relation

Timestamps are not updating while attaching data in pivot table

Laravel fresh() needed after attaching to BelongsToMany with Pivot

Eloquent relation pivot table with table

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

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

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

ManyToMany relation - how update attribute in pivot table

One-to-one relation through pivot table

Setting property in pivot table in many to many relation

L5.6 - Relation on pivot table

Laravel observer for pivot table

Laravel eloquent with pivot table

Laravel relationship with pivot table

Laravel Eloquent - Pivot Table

Laravel update pivot table

Laravel: JSON and pivot table