Questions tagged [laravel-relations]
165 questions
0
votes
1 answer
Laravel Morph filter
// table: rates
+ -- + ------------ + -------------- + ----- + ----------- +
| id | ratable_id | rateble_type | score | create_at |userid
+ -- + ------------ + -------------- + ----- + ----------- +
| 1 | 1 | Events | 4 |…

catch mela
- 11
- 3
0
votes
0 answers
Laravel relationship similar to belongsToThrough()
I'm using Laravel 8. I would like to access data from Table C to Table A, through Table B with only 1 query.
Let's say I have the following tables:
A) Pages
B) Posts
C) Comments
How can I get the Page of a Comment if I only have the Post ID in the…

Radical_Activity
- 2,618
- 10
- 38
- 70
0
votes
1 answer
Laravel HasManyThrough Relation with Where Condition on related table
I want to get count of Verified Doctors of a city based on hospital (where doctor works). I have created hasManythrough relation in City modal and when I'm using this relationship in blade file it gives all doctors (verified and unverified). I only…

Waqas Altaf
- 392
- 3
- 16
0
votes
1 answer
Foreign key constraint is incorrectly formed in Laravel-7 migration
When you are applying a foreign key using laravel migration it through this type of error
"Foreign key constraint is incorrectly formed"
The default structure of migration
User Table
---------
Schema::create('users', function (Blueprint $table) {
…

Hadayat Niazi
- 1,991
- 3
- 16
- 28
0
votes
2 answers
Laravel Migrations are migrating successfully but they are not creating foreign key relationship in tables
My Laravel App is creating all tables in migrations successfully but it's failing to create a foreign keys relationships in the table or even enforce cascade when I delete the primary record.
Here is the migration.
Schema::create('articles',…
0
votes
0 answers
How to create using relationship in Eloquent model?
I have two tables having a relationship:
Package Table:
slug
Package_Banner_Image
Package_Type
Package_Name
Package_Price
Package_Duration
Package_Level
Package_Short_Description
Package_Description
Featured
Package_Location
Programs…

Nutan Panta
- 59
- 1
- 16
0
votes
2 answers
Get records from two relations (one is pass through accessor) with paginate
Laravel 7.x
I need to get posts from two relations. Look:
User has Posts;
User has Friends (accessor);
Friends has Posts;
How can I get all own (User) posts and all posts by each Friend, and paginated?
Which the best way to do that?
Only to…

morphinduction
- 101
- 1
0
votes
1 answer
Laravel getting latest field in Join table with Eager loading
Laravel version:7.0
I am going to get list from Guest model.
guests table: id, name, status, created_at, updated_at
$guests = Guest::orderby("status")->get();
Here is messages table.
$messages table: id, message, from_id, from_type, to_id,…

LoveCoding
- 1,121
- 2
- 12
- 33
0
votes
1 answer
NESTED EAGER LOADING WITH CONSTRAINTS: Getting products all products from the parent category. Category are defined under three different table
I have defined categories in three level. Primary, Secondary, and Product Category.
I have Two categories under primary_categories table which is Women's Fashion and Men's Fashion.
Under secondary_categories I have categories like traditional…

Gaurav
- 149
- 12
0
votes
2 answers
GROUP BY the relationship data in laravel
I am fetching products along with its relationship. I have done this:
$data = $this->products->with('images')->with('colors')->where('slug', $slug)->first();
And in the Product model, I have written:
public function images(){
return…

Alisha Lamichhane
- 506
- 8
- 18
0
votes
0 answers
How to verify if an eloquent parent model exists in another connection in laravel?
I'm using the eloquent method has() to retrieve only rows where the parent exists and is not deleted and it's working well.
But this doesn't work when am passing "user" as a parameter because the user is coming from a different connection.
I have…

Etta
- 339
- 4
- 10
0
votes
1 answer
Write common relation at one place in laravel and extend that class in model where it is required
How can we use common relations in laravel to write in a single file and extend that class in a model where we required that relations.
Please have a look at model "Court":
use Illuminate\Database\Eloquent\Model;
use…

nayan verma
- 103
- 1
- 11
0
votes
1 answer
Laravel Model Relations fetch automatically all other model relations too
I'm hoping someone can help me.
I have 3 models like User, Task and Subtask.
These are already linked via hasOne or hasMany.
Everything works fine.
Now I call the data via Task::where(..)->with(['user','subtask'])... and get the corresponding…

tola
- 23
- 2
0
votes
2 answers
Fetch news from specific category along with each news writer in laravel
I have following function in Category model:
public function news(){
return $this->belongsToMany(News::class,'news_categories','category_id','news_id');
}
public function writer(){
return…

Alisha Lamichhane
- 506
- 8
- 18
0
votes
1 answer
Laravel how to get sum price for domain prices table
I am using Laravel 7.0.
My domain_prices table has price, duration, addPrice columns.
I created DomainPrice model.
Domain price for specific duration is price + addPrice.
But for 5 years duration, I need to sum all prices and all addPrices for 1, 2,…

LoveCoding
- 1,121
- 2
- 12
- 33