Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
2
votes
1 answer
How to create category tags for posts? I am using laravel 8
I am trying to allow users to create and add category tags to a post when creating that post in the form.
I would further want those tags to appear in the profile view and function as filter buttons to show posts according to the tag names they…

Eden Gaitor
- 75
- 6
2
votes
1 answer
How to get the default price of the product with different prices from different date periods from the period closest to today?
my sqlfiddle eample
Hello there,
according to the above sqlfiddle example;
I have a table A where the products are listed and a table B with different prices for different periods associated with these products.
Here I show these prices according to…

Ersin
- 39
- 6
2
votes
1 answer
Laravel relationships use Where
This relationship is in my Maintenance.php
public function contactedContractor()
{
return $this->hasMany(ContactedContractor::class, 'maintenance_id');
}
I want to use the relationship in a query in my…

user3714932
- 1,253
- 2
- 16
- 29
2
votes
0 answers
Left Join with Multiple on clause Eloquent Laravel
I have the following mysql query
select * from operatories o
left join apts ap on o.location = ap.Location and o.operatory = ap.Operatory;
I also have two models
class Operatories extends Model
{
public function operatories()
{
…
user14289833
2
votes
1 answer
Laravel pivot model relationship with another table
Database Explanation:
Item has many tiers
Tier has one type (Size, Option OR Addon)
type has many-to-many relationship with option
optionType has many sizes and price
The Problem:
In eloquent query I am able to fetch data until option_type, I…

Wcan
- 840
- 1
- 10
- 31
2
votes
1 answer
Laravel Relationship: A division has many ranks, a rank has 1 division, and a user has only 1 rank
For instance:
Divisions:
Division A
Division B
Division C
etc...
Ranks in Divisions
Division A
-- Rank 1, Rank 2, Rank 3, ...
Division B
-- Rank a, Rank b, Rank c, ...
etc..
And a user would only be assigned a rank. For instance, they would be…

Brennan
- 35
- 1
- 6
2
votes
1 answer
Where clause in polymorphic relationship
I have the tables
threads
- id
replies
- id
- repliable_id
- repliable_type
I want to add another column to the Thread, which is the id of the most recent reply.
Thread::where('id',1)->withRecentReply()->get()
And the following query scope
public…

Orestis uRic
- 347
- 1
- 6
- 11
2
votes
1 answer
Get relationship data from eloquent builder laravel 7
I wanna filter my table data. I have a table for states, one for cities and one for students.
states(id, name)
cities(id, name, state_id)
students(id, first_name, last_name, city_id)
When I wanna filter data with eloquent builder how can I access…

Majid
- 97
- 9
2
votes
2 answers
How to get items are not in a collection in Laravel Eloquent?
In my Laravel 6.x project I have Product model, Warehouse and WarehouseProduct models.
In the Product I store the base information of my products. In the WarehouseProduct I store the stock amount informations about products in warehouse. Of course I…

netdjw
- 5,419
- 21
- 88
- 162
2
votes
2 answers
Laravel Many-to-Many (on the same users table/Model): Query scopes to include related for the specified user
Users can block each other. One user can block many (other) users, and one user can be blocked by many (other) users.
In User model I have these many-to-many relationships:
/**
* Get the users that are blocked by $this user.
*
* @return…

PeraMika
- 3,539
- 9
- 38
- 63
2
votes
1 answer
I need to know the Laravel Eloquent syntax for querying the nested relationship and returning the results
I have the following models
Company
Contact
Ticket
Job
User
Job BelongsTo Ticket, Ticket BelongsTo Contact or User (Polymorphic), Contact Belongs to Company. I can retrieve all jobs for a particular company…

Shane Poppleton
- 35
- 5
2
votes
1 answer
Access nested related objects with where clause in Laravel using Eloquent
I'm trying to get a list of tasks that are associated with a specific client, but there is no direct relation between a task and a client. I also need to filter those tasks based on task status and project status.
Here are my models:
class Client…

codescribblr
- 1,146
- 2
- 11
- 29
2
votes
1 answer
Problem when getting records from database with relationship table
I'm having trouble relating photos to tags using an intermediate table.
In the example below, how can I select all photos that belong to tag 1 with an Eloquent relationship method in Laravel?
I have these tables:
-Photos Table
| id | name …

SSPerformance
- 21
- 1
2
votes
1 answer
How to determine pivot-table name in belongToMany relationship in Laravel?
I am having the issue of simply trying to determine the correct name to give to the intermediary table used for many to many relationships in Laravel.
I received the following error upon attempting to access an eloquent…

Jack
- 135
- 2
- 12
2
votes
1 answer
Update Data From Dynamic Form Using Laravel 5.8
I have some problem, but I'm not understand why its not working.
I have a Quotation.php model, like :
class Quotation extends Model
{
protected $table = 'quotation';
protected $fillable = [
'no_quotation',
'subject',
…

Yudhistira
- 43
- 7