Third Table lookup on Eloquent - laravel

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.

Related

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

Save on different tables using Eloquent Laravel Relationships

Hi I have 3 tables "temp_quotes", "quotes" and "quote_items"
Every quote generated goes to the "temp_quotes" table at first.
At the end of my multi-step form I need the following:
1- Receive all the quotes from the "temp_quotes" table, send it to the "quote_items" table and delete it from the "temp_quotes" table
2- Generate a new entry in the "quotes" table referencing the "users" table and
the "quote_items" table
Here is all the data and how needs to be save/removed on the tables:
temp_quotes Table
ID
BRAND
MODEL
YEAR
quotes Table:
ID
USER_ID
QUOTE_ID
quote_items Table:
ID
QUOTE_ID
BRAND
MODEL
YEAR
Any help will be appreciated.

Laravel / Eloquent -- Lookup Table Issue

I have 3 tables that I am trying to work through and am having a hard time connecting them via Eloquent joins.
Character Table
profileID (PK)
Character Gear Table
profileId (PK)
qualityId (FK)
Quality Lookup Table
id (PK)
name
I am able to access the Character Gear Lookup with the following in my Character Model:
public function gear()
{
return $this->hasMany('App\Models\CharacterGear', 'profileId')->where('gearSet', '=', '0');
}
How do I get the lookup to work so that I can get the quality name from the Quality Lookup table to tie in to the gear() shown above?
Please let me know if you need any further information!
Figured it out. Eloquent has Nested Relationships and I didn't know that.

How to create a one-to-many relationship in Eloquent to a table without creating a model for that table?

I have a model which holds a property with zero to many values. Those values are strings (e-mail addresses). But I don't want to create an extra model for such values since they only appear in this property.
As far as I read the docs I need to have a model for my e-mail addresses to gain full power of Eloquent.
Am I missing something out or is there no clean way to spare a model for a database table for relationships?
A short example of my database tables in question:
Table A:
- id [serial]
- name [string]
- someProperty [string]
- mailAddresses [unsigned int; reference to id of Table B]
Table B:
- id [serial]
- mail [string]
I've got a model for Table A:
class ModelA extends Eloquent {
protected $table = 'Table A';
public function mailAddresses() {
return $this->hasMany('<what to put here?>');
}
}
Bascially, you have 1 Model for 1 Database Table (except the Table is an intermediate table (pivot table)).
So shortly, yes. If you want Eloquent Models to work properly, you need to create a Model for your E-Mail Table.

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

Resources