How do I insert data manually into the pivot table? Laravel - php

I have an application that contains 2 models: User and Event
There's a many to many relationship between them and I am using a pivot table (called participants). I need to insert the data into the pivot (participants) table manually (specify each column's value).
I can't use the relationship insert, because I am leaving the user_id column blank. (Therefore it won't be a "relationshipped" column.) Any ideas how to make this scenario real, please?

Related

searching a column form pivot table

I have 3 models Eg: Model-1, Model-2, Model-3.
Model-2 and Model-3 has many to many relationship,
So i'm keeping one pivot table like Model-2_Model-3.
Model-1 is has one relationship with model-2 and i'm listing all the data from model-1,
I need to search one column from pivot table for listing Model-1.
For Eg:
$data = Model-1::with('Model-2')->get();
I need to search Model-1 Using pivot table (Model-2_Model-3).
I return pivot connection code already. Pivot is working very well.
You can query pivot table with wherePivot()
Model-1::with('Model-2')->wherePivot('field_name', $value)->get();
If the pivot values are not added, you need to add it as withPivot('field_name')
You may like to use contains, as for many to many relationship you will get collection of second model something like $user->roles, so here you can check if $user->roles->contains('field_name'). as well as $user->roles()->wherePivot('role_id', 1)->get()->isEmpty() or $user->roles()->wherePivot('role_id', 1)->get() depending on your requirement.

How to get inserted id of pivot when I use attach() in Laravel 5.6 (Eloquent)?

I have this tables: users, programs and users_programs (pivot)
I use this sentence to relate users with programs (Many to Many)
$user->programs()->attach($program);
I want to get id to pivot table users_programs, it's posible? How I return id when save a row?
Thanks
Pivot tables don't have an increment id field in the realm of Eloquent.
If you want to get a specific program for a user in the pivot table, you can use something like the following approach as an example:
$user->programs()->attach($program->id);
$user = $user->fresh();
$userProgram = $user->programs->keyBy('program_id')->get($program->id);

how to define many to many relationship in laravel models?

I have two master tables. i.e, users and tasks. Primary key column names are in_user_id for users table and in_task_id for tasks table.
There is relational table users_tasks (to see a task are assigned to which users and a user is assigned with which tasks). Foreign key columns are in_task_id and in_worker_id in users_tasks table.
I want to define relationship of users and tasks (many-to-many) into both models. I have read this doc. But in my case, primary key column is in_user_id in users table. But foreign key column name is in_worker_id in users_tasks table.
So I don't know that how to handle fourth parameter in relationship function (return $this->belongsToMany('App\User', 'users_tasks', 'in_task_id', ?); in Task Model file). Either in_user_id or in_worker_id or something need to define more. If anyone knows the solution, it will be appreciated.
From laravel eloquent documentation on many-to-many relationships
In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to:
In your case it would be :
return $this->belongsToMany('App\User', 'users_tasks', 'in_task_id', 'in_worker_id');
Now this assumes a few things:
Your user and task model are working properly (since you have non-conventional identifiers you've defined them in your models).
Not sure if its still the case but in previous versions of Laravel, your pivot table (in this case users_tasks) also needs it's own identifier column, typically an auto-incremented id.
Try this
$this->belongsToMany('App\User, 'users_tasks', 'in_worker_id ',
'in_user_id ');
params -> model, relation table, foreignkey, primary key
And also relation tables should be in the alphabetical order. (best way)
It must be tasks_users

Laravel Soft Delete Not working in pivot table

I am using Laravel 5.2.
I have 3 tables: book, user and book_user (the pivot table).
I want to use soft deletes on my pivot table. When I attach a book to a user, the relationship is inserted into the book_user table. However when I detach this relationship, the record in the pivot table is deleted even though I added use SoftDeletes to the pivot table model.
How can I implement soft deletes for the records in my pivot table when I attach or detach?
I didn't try it using soft delete with pivot table but you're saying that it doesn't work.
Just an idea, maybe you can use sync instead of detach like this;
Before that you need to add deleted_at column to book_user table as DATETIME instead of TIMESTAMP. Because new version of MYSQL doesn't support NULL for TIMESTAMP type.
Soft Deleting
$user->books()->sync(array(1 => array('deleted_at' => DB::raw('NOW()'))));
Getting
Also you can put a constraint on the Eager Load:
public function books()
{
return $this
->hasMany('Book')
->whereNull('book_user.deleted_at');
}

Resolving many to many relationship MYSQL database

I have 3 tables,
users(id) , userproduct (user_id,product_id) and product (id).
When a user adds a product their user_id and the product_id is saved in USERPRODUCT table.
Now at the same time another user can save a product (kind of like "set as favorite") for this I need to save the user_id of the user who saved it and the product_id of the product. This will create another table (call it favorites) however this table is also a resolve to the many to many relationship between "users" table and "product" table.
the question: is it possible for this, to have 2 tables resolving a many to many relationship or is there any other way to solve my issue.
if it is possible how will one be draw this in the ER diagram
it depends on what you want.
you may use only one table and design your relations with boolean fields
USERS(id)
PRODUCTS(id)
USERPROD(userid, productid, isbought, isfavorite, isowned)
or you may use one many-to-many support table for every relation is up to you
maybe you want another field "owner" in PRODUCTS table to store the user id. this is a one-to-one relation
What you want to do is actually describe the kind of relation between user and product: a favorite, or he 'owns' it.
Just add another field to the userproduct-table defining the relation: favorite or owned

Categories