how to get data from pivot table inside a many to one relation in laravel 5.6 - laravel

I have 3 tables name like "product" , "user", "product_type" so in my case user and product_type having many to many relationship and user and product having one to many relationship and product and product_type having one to one relationship.
I create one pivot table for user and product_type. inside that pivot table, I added one more column for description. so in product listing page I need to display description from that pivot table.
My code look like this:
Product::with('user)->with('product_type')->get();

To get extra fields from pivot table you need to use withPivot in function of your model Like this:
public function product_type() {
return $this->belongsToMany('App\Product','product_type','product_id','user_id')->withPivot('column1', 'column2');
}
you may also refer laravel docs for it:
https://laravel.com/docs/5.7/eloquent-relationships

Related

Third Table lookup on Eloquent

People Table:
id
name
Places Table:
id
name
Placeable Table:
people_id
places_id
relation_id
Relation Table:
id
label
In the people model, I can do:
return $this->morphToMany(Places::class, 'placeable')->withPivot('relation_id')
And I'll get the id number of the relationship in the pivot table. But, in my blade file, I'd like to put the label from the relationtable. "Manager" instead of "2", for example.
All the ways I've come up with so far create a lookup at every iteration in the blade. It seems like it would be great if I could append a "JOIN" to my eloquent in model to retrieve the label as it grabs everything else, but I'm not finding how to do that.

laravel eloquent ORM Joint multiple table without Foreign key And Multiple model

I have two tables. One is user and the other is category, I have written this Eloquent ORM query to retrieves data from category
$category = Category::where(['status'=>'active'])->orderBy('id','asc')->get();
In category table here has a field called user_id. I want to join the user table and want to retrieve the user name where category table 'user_id' == user table 'id'.
How can I do this in Laravel eloquent ORM without Foreign key And Multiple models.

Retrieve data from Table A based on Table B using Eloquent

I have two tables: requestgenerals and requestinformations.
The relationship between the 2 tables is:
requestinformations belongsTo requestgenerals
requestgenerals hasMany requestinformation.
Below are the tables:
requestgenerals table
and
requestinformations table
I tried the following: $requestgenerals = Requestgeneral::without('requestinformation')->get(); but I still get all the rows from the requestgenerals table instead of just two.Please assist
You should use this:
$requestgenerals = Requestgeneral::doesntHave('requestinformations')->get();
if you want to get all records that don't have related record in second table. Take a look at Eloquent documentation.
Use doesntHave for get data doestnt have requestinformations
$requestgenerals = Requestgeneral::doesntHave('requestinformations')->get();
For refernce refer this link
Example

add relation that depends on the content of a table

So i have the following tables: 'order', 'comments', 'personnel' and 'contactperson'
What i want is to get all the orders with their comments and thats comment's author
so i do this to get the comments:
Order::with('comments')->get();
No problem here. But how can i get the author of those comments.
The Order table has the following fields:
id_author -> int
fk_author -> enum('personnel','contactperson')
Now depending on the fk_author field, the author should be fetched from the personnel table or the contactperson table.
How can i achieve this?
Look into polymorphic relations. On your orders table, you will want to rename your columns to fit what Eloquent expects author_id, author_type instead of id_author and fk_author

Soft delete on a intermediate table for many-to-many relationship

How do I set soft delete on an intermediate table which is connecting two different types of entities? I've added deleted_at column, but the docs say that I need to put this into the model:
protected $softDelete = true;
Of course, I don't have a model for an intermediate table.
Any idea?
You can put a constraint on the Eager Load:
public function groups()
{
return $this
->belongsToMany('Group')
->whereNull('group_user.deleted_at') // Table `group_user` has column `deleted_at`
->withTimestamps(); // Table `group_user` has columns: `created_at`, `updated_at`
}
Instead of HARD deleting the relationship using:
User::find(1)->groups()->detach();
You should use something like this to SOFT delete instead:
DB::table('group_user')
->where('user_id', $user_id)
->where('group_id', $group_id)
->update(array('deleted_at' => DB::raw('NOW()')));
You could also use Laravel's Eloquent BelongsToMany method updateExistingPivot.
$model->relation->updateExistingPivot($relatedId, ['deleted_at' => Carbon\Carbon::now()]);
So to use #RonaldHulshof examples you have a User model with a groups relationship which is a belongsToMany relationship.
public function groups() {
return $this->belongsToMany(Group::class)->whereNull('groups_users.deleted_at')->withTimestamps();
}
Then in order to soft delete the pivot table entry you would do the following.
$user->groups()->updateExistingPivot($groupId, ['deleted_at' => Carbon\Carbon::now()]);
As far as I understand it; an intermediate table is simply a length of string attaching one tables record to a record in another table and as such it does not require a soft delete method.
To explain, imagine you have a Users table and a Groups table, each user can have more than one Group and each Group can belong to more than one User. Your pivot table may be User_Group or something like that and it simply contains two columns user_id and group_id.
Your User table and Group table should have a deleted_at column for soft deletes, so when you "delete" say a Group, that group association will not appear in $User->Groups() while the pivot table row has remained unaffected. If you then restore that deleted Group, it will once again appear in $User->Groups().
The pivot table row should only be affected if that group record is hard deleted, in which case the pivot rows should also be hard deleted.
Now I have explained why I do not believe you need to add soft delete to a pivot table; is there still a reason why you need this behavior?

Resources