How do you combine the following three tables with eloquent in laravel? - laravel

How to combine 3 Many to Many tables with eloquent in the laravel
Hi Friends, please help me,
How do you combine the following three tables with eloquent in laravel?
The following table structure and figures:
(https://i.stack.imgur.com/BfISA.jpg)
Member table structure
{member_id: primary key, name, created_at, updated_at}
List table structure
{list_id: primary key, member_id: foregn key, price_id: foreign key, created_at, updated_at}
Price table structure
{price_id: primary key, name_price, created_at, updated_at}
Can you give me sample source code for view.blade, controller and model.
Thank you, your answer is very meaningful

in your member model add relationship function list(). Then in your list model add relationship function price then use below code
app\Member.php
public function list(){
return $this->hasMany('App\List', 'member_id');
}
app\List.php
public function price(){
return $this->hasMany('App\Price', 'price_id');
}
in your controller
$result = Member::with(['list.price'])->get();
it will give you a member list with list and price table data.

Related

Laravel - Eloquent relation - many-to-many - get intermediate table columns

I have a question about Laravel Eloquent Relations.
I have the following table situation:
Table guests:
id
name
...
Table landing_pages:
id
name
...
A guest can have several landing pages. And a landing page will have multiple guests.
So I thought, I can use a many-to-many relationship there and I created a new table
Table guests_landing_pages:
id
guest_id
landing_page_id
...
I would be able to use this the following way:
In guest Model I can do this, to create the many-to-many relationship:
public function landing_pages()
{
return $this->belongsToMany(\App\Models\LandingPage\LandingPage::class, 'guests_landing_pages','landing_page_id','guest_id');
}
... and the other way round in landing_page model.
But actually the table guests_landing_pages contains more data than just the guest_id and landing_page_id relation.
There are several other fields in it:
Table guests_landing_pages:
id
guest_id
landing_page_id
identifier_token
max_persons
answer
...
My question is now, how can I realize this best. If I would use the many-to-many relation as described above, I will not be able to access the fields in the intermediate table, won't I?
Or will the best solution to this to create a GuestLandingPages model with all the fields and create hasMany relations to/in both?
Laravel Eloquent has features for retrieving intermediate columns.
public function landing_pages()
{
return $this->belongsToMany(\App\Models\LandingPage\LandingPage::class, 'guests_landing_pages','landing_page_id','guest_id')
->withPivot('max_persons', 'answers');
}

Laravel Relationships with 3 tables problem

I don't understand how to connect three tables in Laravel.
users> id, ...
groups> id, ...
group_user> id_group, id_user
I want to return all groups to me auth()->user()->groups.
Thanks in advance.
According to your explanation, group_user table holds many-to-many relationship between Group and User model. In case of a many-to-many relationship, you actually don't need a GroupMember model. This kind of table which holds many-to-many relationship is called pivot table. Read more from the Official Documentation
The first argument is the Model you are relating to, the second one is the name of the pivot table, and 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
User.php
class User
{
public function groups()
{
return $this->belongsToMany(Group::class,'group_user','id_user','id_group');
}
}

Laravel 5.5 many to many relationship with an intermediate table

Please help,
I have these relationship in laravel between ITEMS, SUPPLIERS and PURCHASE_ORDERS
As ITEMS and SUPPLIERS has many to many relationship, I created an intermediate table between them, called ITEM_SUPPLIER.
Now I need to make a many to many relationship between PURCHASE_ORDERS and the intermediate table ITEM_SUPPLIER.
How do I make the relationship?
Should I create an intermediate table between them, what's the best way to name it?
Add a model for pivot table ItemSupplier whth extra column id which extends Pivot
use Illuminate\Database\Eloquent\Relations\Pivot;
class ItemSupplier extends Pivot
{
public function purchaseOrder()
{
return $this->belongsToMany(PURCHASE_ORDERS::class, item_supplier_purchase_order);
}
}
As you see you need to create a new item_supplier_purchase_order pivot table and add the relationships
class PURCHASE_ORDERS extends Model
{
public function purchaseOrder()
{
return $this->belongsToMany(ItemSupplier::class, item_supplier_purchase_order);
}
}
Add id column to the item_supplier and create ItemSupplier model for the pivot table. Then create a new item_supplier_purchase_order pivot table and two belongsToMany() relationships between ItemSupplier and PurchaseOrder models.
Also, don't forget to use cascade deleting to delete records from new pivot table when a related record is deleted from item_supplier pivot table.

Laravel relationship between two tables

I'd like your input on this.
I have a Customer_table with a field name. I have another table called Reservation_table with a Customer_name field.
How can I relate them in such a way that I'd see all the bookings by the specific customer?
In Reservation_table you should have a field(foreign key) userid in order ta have a user for each reservation.
You can using primary key - foreign key relationship to relate/join those two tables. Also, instead of having a 'Customer_name' field as your FK referring to 'name' field in 'Customer_table' table, it is better to have an id (unique) generated for each customer; This way you can have an efficient way of uniquely identifying and relating customer across tables; can save space on Database side as well. Hope this helps!
If you want to use eloquent you must first define a relationship.
One reservation belongs to a user. Here is how to define the relationships:
Inside the Reservation model:
public function user()
{
return $this->belongsTo('App/User'); //User model
}
To define the inverse you do the following:
Inside User model:
public function reservations()
{
return $this->hasMany('App/Reservation'); // Reservation Model
}
Now you can do the following in your controller:
$reservations = Auth::user()->reservations;
Now you have all reservations by the currently logged in user.
I am not sure if I got the question right so ask away.

Use a different column on a many-to-many relationship in Laravel 4

I've got a situation in a project where I need to form a relationship between a primary key on one table and an indexed column (not the primary key) on another. Here's a sample of the database layout:
courses table
id
level
resources table
id
courses_resources table
course_level
resource_id
In my CourseResource model I have the following:
public function courses(){
return $this->belongsToMany('Course', 'courses_resources', 'resource_id', 'course_level');
}
Which works fine.
Then in my Course model I have:
public function resources(){
return $this->belongsToMany('CourseResource', 'course_resources', 'course_level', 'resource_id');
}
Which doesn't work. When I look at the last query performed on the database, it appears Laravel is searching the course_level column using the course's ID. That makes sense, but is there any way to use the level column for this relationship?
Eloquent BelongsToMany depends on PKs, so there is no way to do that with its methods.
You need custom relation object for this, that will check for given field, instead of primary key.
A quick and hacky solution would be this:
// Course model
public function getKey()
{
$relation = array_get(debug_backtrace(1, 2), '1.object', null);
if (method_exists($relation, 'getForeignKey')
&& $relation->getForeignKey() == 'courses_resources.course_level')
{
return $this->getAttribute('level');
}
return parent::getKey();
}
However if you would like to use it in production, do some extensive testing first.

Resources