Official documentation: https://laravel.com/docs/7.x/eloquent-relationships
Questions tagged [eloquent-relationship]
944 questions
-1
votes
1 answer
Laravel, whereHas checking latest record of a relationship only
I have 2 tables (posts, revisions), and there is a relationship between them
A post has many revisions (One to Many relationship)
Revisions table has a column called status (approved or rejected)
Now, I want to select all posts, if its latest…

user9500574
- 142
- 1
- 11
-2
votes
2 answers
i have a MLM system. where User hasmany Users. now i want to get all users of root/ node A user. where one user hasmany user (in deep) laravel
i have a MLM system. where User hasmany Users. now i want to get all users of root/ lavel-1 user and where one user hasmany user and each of the users can hasmany user (in deep) laravel.
Here Is relation in model:
// All Child Refer
…

Rajib Bin Alam
- 353
- 1
- 4
- 16
-2
votes
1 answer
ErrorException foreach() argument must be of type array|object, null given (View:) laravel
I've created one to many relationship and when I try to retrieve data I am getting this ErrorException
foreach() argument must be of type array|object, null given (View: )
i am still learning laravel so i don't know much about it. here's my blade…

alishba1998
- 1
- 3
-2
votes
2 answers
I want to check name of that particular company's shift exist or not?
I have many to many relationship and I'm trying to check this company's shift name already exist or not in database.
relation is below.
public function shifts()
{
return $this->belongsToMany('App\Models\Shift');
}
public function…

saroj baskota
- 17
- 2
-2
votes
1 answer
How to build a query on laravel correctly?
I am grateful in advance for any answer!
there are CATEGORIES
CATEGORIES (one) 1⟶n PRODUCTS (many)
PRODUCTS (many) m⟶n STORE (one)
STORES (one) 1⟶n ADDRESS_CITY (many)
CATEGORIES → PRODUCTS → STORES → ADDRESS_CITY
you must select all the…

Den
- 27
- 5
-2
votes
1 answer
Getting error message call to undefined function on Laravel relation
I am working on a query in Laravel 8 project for retrieving company details. Below is the structure and MySQL query.
# Getting company param from request.
SELECT c.company_name, p.position_name, s.salary
FROM tbl_company as c
JOIN tbl_company_type…

deepu sankar
- 4,335
- 3
- 26
- 37
-2
votes
1 answer
Laravel eloquent relation to json as string (only one attribute)
When I serialize a model (e.g. ModelA) that has a relation (e.g. ModelB), it looks something like this:
[{
"id": 1,
"name": "model a1 name",
"modelB": {
"id": 1,
"name": "model b1 name"
}
}, {
"id": 2,
"name":…

shaedrich
- 5,457
- 3
- 26
- 42
-2
votes
2 answers
Laravel / Eloquent : request that returns a count of id instances inside a table then stores it into a variable
I'm currently working on a project on laravel 8.
I have 3 tables : 1.customers 2.books 3.loans
I want to be able to make a request like $data=DB::table('emprunts')->where('customerid',$id->number)->first(); but where it would count the instances of…

YesItsMe
- 13
- 6
-2
votes
1 answer
Problem with Laravel Eloquent reltionships for tables without relations
I, ve got problem with eloquent relationships.
This is my DB
https://i.stack.imgur.com/2we4g.jpg
https://i.stack.imgur.com/20KeG.jpg
I've got Santander ID in santander column in partner table and want to use data from those two table like from…

Piotrexed
- 3
- 3
-2
votes
1 answer
Laravel how to make Eloquent relationship to find all data from child_category table with related category & subcategory name?
I have three table:
categories table fields id, category_name
subcategories table fields id, category_id, subcategory_name
child_categories table fields id, category_id, subcategory_id, child_category_name
I have three model Category,…

Nazmul Haque
- 720
- 8
- 13
-3
votes
1 answer
How to fetch data from two tables using ORM in LIKE search using Laravel
This is my controller code:
public function searchUser(Request $request)
{
$pram = $request->name;
$data['users'] = User::whereHas('userBasicInfo', function ($query) use ($pram) {
$query->where('first_name', 'like', '%' . $pram .…

syed1234
- 761
- 5
- 12
- 30
-4
votes
2 answers
How to connect 3 table?
In my case, I have three tables:
type
id
type
reason_id
reason
id
reason
permission
id
type_id
reason_id (not sure has or not)
I expect that:
{
"message": "Get all permissions with type and reason",
"data": [
{
…

Vireak Ron
- 1
- 1
-4
votes
2 answers
I want make array for eloquent relationship laravel
in Course model this relation are include
public function course_modules()
{
return $this->hasMany(CourseModule::class, 'course_id');
}
public function course_lessons()
{
return $this->hasMany(CourseLesson::class,…

Akash
- 1
- 1