Questions tagged [laravel-relations]
165 questions
2
votes
1 answer
Fetch products by category id using pivot table
I have a function where I am passing the category id and based on that I want to fetch all the products .
Here is a structure of my db
Category db:
category_name
Product db:
product_name;
category_product:
category_id;
product_id;
Below are the…

SyedKhizer
- 241
- 1
- 5
- 14
2
votes
0 answers
Accessing parent table laravel model with relation where parent column comparison
I have been having this problem for few days, and there were no documentation on Laravel web or any issue related on google search. So I was wondering if it is possible or if I could do it any other way except loading all data from database to a…

Niyobuhungiro Yves
- 61
- 7
2
votes
2 answers
Query relationship inside the relationship in Laravel Eloquent
I am trying to get the news of a writer according to date passed. I have written function so far:
public static function getNewsByAuthorByDate($id, $year, $limit = 1){
return User::where('id', $id)->has('news')
->with(['news' =>…

Gaurav
- 149
- 12
1
vote
1 answer
Laravel Eloquent Multiple whereHas with Where condition
I have models Term, Concept, and Expression. I have pivot tables concept_expression and concept_term.
Schema
terms table
id
term,
concepts table
id
expressions table
id
expression_plural
Relationships
Term model
concepts():…

Jeroen Steen
- 531
- 8
- 22
1
vote
2 answers
How to load a relationship in Laravel without foreign key?
I have this model:
App\Controllers\ImageController.php
.....
$image = new Image;
$image->id = "pre_" . (string) Str::ulid()->toBase58();
$image->filename = $filename
$image->post_id= $post_id;
$image->save();
…

DeveloperX
- 517
- 5
- 23
1
vote
2 answers
How to access a Pivot table from an API Resource in Laravel 9
I am building an API using Resources. I have a Product (Chocolate Cake) that can be linked to a Property (Allergies) that has Properties_property (Glutten) that need to be shown in different orders for every product.
product <---[many-to-many]…

St. Jan
- 284
- 3
- 17
1
vote
1 answer
Retrieving data based on One To Many Relationship
I have two tabled called categories and resources table.
Basically each resource has a category and the category id is saved on a column called resource_category_id in resources table.
So in order to setup One To Many relationship between the…

Pouya
- 114
- 1
- 8
- 36
1
vote
0 answers
What if you make Model for pivot table in Many To Many relationship
I have made a separate Model and defined the required relationships between all my Many To Many Models:
class AttributeProduct extends Model
{
use HasFactory;
protected $table = 'attribute_product';
protected $guarded = [];
public…

japose7523
- 29
- 2
- 15
1
vote
1 answer
Laravel One to many trough belongsto and many to many (with pivot in between)
I've been looking around for a fix but couldn't find it.
The situation:
Resources are connected to an Event, multiple resources can be connected to multiple events (many to Many).
Many Interventions are connected to only 1 single event at a time…

NoahNxT
- 156
- 12
1
vote
1 answer
How to resolve Not unique table/alias error in laravel?
I am very new to the laravel,i am using l5-repository package for orderBy and sortedBy to sort columns ,while hitting API i am getting following error please help me to resolve the issue
my API URL…

usersuser
- 167
- 12
1
vote
1 answer
Laravel user, role, permission relationship implemention problem
I have three Models User, Role and Permission. There is complicated but well-known relation between these Models and their tables.
Tables and their columns…

Artin GH
- 546
- 2
- 10
- 21
1
vote
1 answer
Laravel get records of distant relations via an intermediate relation
There are 3 tables:
users
personal_access_tokens
tokenable_type
tokenable_id
personal_access_token_cards
personal_access_token_id
Users has many(morph) personal access tokens.
Each personal access token has one personal access token…

hijack
- 45
- 1
- 4
1
vote
1 answer
How to get all results of a table with Many To Many relationship
I'm working on an Online Store project and I wanted to create Add To Favourite system for users to add products that they like to their favourite list.
So I have created a table named favourite_products which goes like this:
So the usr_id stands…

yetep93258
- 33
- 1
- 13
1
vote
1 answer
Null Relation in Laravel Model
These are my Migrations
Type Migration
Schema::create('digital_types', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
Content…

CC7052
- 563
- 5
- 16
1
vote
0 answers
Change model And database table name in Laravel
I want to change my model name and its database table name in a Laravel project, but I've connected other models with relationships. So now I want to know if there is any way or artisan command to change it, so it does not make problems for its…

Niadev
- 11
- 3