add relation that depends on the content of a table - laravel

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

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.

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

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

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

Laravel 5 Fetching Relation Data from other table without using pivot

I have the following table structure.
Posts
id (int)
title (varchar)
comments (json field)
Comments
id
title
The comments field looks like following (It's a simple array of ids):
[6, 9, 48, 12, 49]
Now I want to query the Posts and replace all ids in comments with the values of the Comments table.
Can I do this without using a pivot table like in laravel relations (belongsToMany)?
Make your table structure like this:
Posts
id (int)
title (varchar)
Comments
id
title
post_id
This way you will have simple hasMany relation without any pivot tables.

Get list of joined rows with DAX

I have Supplier dimension table has 1:n relationship with InvoiceDetail fact table. I would like to get the list of active suppliers like below SQL, but in DAX language:
SELECT [Id]
,[Name]
,[Code]
,[CountryIso]
FROM [Supplier] s
WHERE EXISTS (SELECT 1 FROM [InvoiceDetail] id WHERE s.id = id.SupplierId)
I am not sure how I can do on Measure with DAX
Assuming that an active supplier means that the supplier has an invoice against them and that your data looks something like this..
Invoice Table
Supplier Table
Creating a relationship between the two tables will in effect, 'join' the two tables.
You can then use invoice number field from the invoice table and the name/code/countryiso from the supplier table.
Example being:
The value are only being drawn from the invoice table, so you'll only see active Invoices.
If being an active supplier means having a true bool value, join the tables and add a report/page wide filter on that bool value.

Resources