Questions tagged [eloquent-relationship]

Official documentation: https://laravel.com/docs/7.x/eloquent-relationships

944 questions
1
vote
3 answers

How to create a relationship between two eloquent models

I have two Models: Users and Appointments class Appointments extends Model { use HasFactory; protected $fillable = [ 'business_id', 'user_id', 'subject', 'description', 'status', …
cdBreak
  • 63
  • 6
1
vote
1 answer

Laravel 9 - Eloquent group results of query

I am looking for a way to take one query in Eloquent to get all search results, and then take those results, group them by type, and output the different types. I currently have this setup to get all my search results: $listings =…
1
vote
2 answers

laravel relationship not loading

I've got a Product and a Category models: class Product extends BaseModel { use Uuid; protected $fillable = [ 'barcode', 'name', 'sku', 'description', 'type', 'category_id', …
MrCujo
  • 1,218
  • 3
  • 31
  • 56
1
vote
2 answers

how to GROUP BY data in multi relational table (laravel eloquent)

I'm stuck in a problem that how to GROUP BY data in multi relational table. Here is my code: $invoices = Invoice::select('id', 'date', 'type') ->with('invoice_items:invoice_id,item_id,amount', 'invoice_items.item_title:id,name') …
mahbub
  • 103
  • 1
  • 3
  • 13
1
vote
1 answer

How to apply hasMany relation from Laravel Model in inertia ReactJs

I am trying to display comments that are related to individual posts from the db. Displaying the comments with that post inside my React component (inertia), using eloquent relations in Laravel. This is my PostController: public function show() …
Miss Def
  • 13
  • 2
1
vote
0 answers

Laravel creating a Relationship doesn't return the ID into parent

Whenever i create a relationship entry into the parent model it doesn't seem to set the foreign_key. Example: $vehicle = Vehicle::create([ 'vin' => $data['VIN'], 'engine' => $data['ENGINE'], …
morxw
  • 13
  • 3
1
vote
0 answers

Count on GroupBy on Pivot data in Laravel

I've got a many-to-many relationship between a user and a match, and I have a availability column on the pivot table which is an enum: 'available', 'not-available', 'tbc'
n8udd
  • 657
  • 1
  • 9
  • 30
1
vote
1 answer

filter result of a 'subquery' in laravel 9, with eloquent

my model: a post belongs to a category. So I want to get the category and the posts it has, with filters. I've already tried: $category = Category::select('id', 'name') ->with('post:id,title') ->get(); But this returns the following…
1
vote
0 answers

Laravel Eloquent Inner join where multiple data from another table return in property as an array

Suppose, I have two tables; tableA and tableB. For tableA single row's id there are multiple rows in tableB. Now I want all tableA data including tableB, that's matches by id (tableA->id === tableB->blood_request_id) The return will be [ …
1
vote
1 answer

Laravel Eloquent - setup both-ways relationships after loading?

Let's consider the following example: a thread has posts, and the posts also have a "thread" relation. The title of each post must include the title of the parent thread. class Thread extends Model { public function posts() { return…
Stalinko
  • 3,319
  • 28
  • 31
1
vote
1 answer

Laravel Relationships | Joins | Eloquents - Looking for a better way to write this Query -

Lets suppose, I have two tables which are customers and leads. Car table has these columns id, name, phone, source while leads table has id, customer_id (FK) and other columns. I want to fetch count of leads against each source. Here is my query and…
AKT
  • 56
  • 5
1
vote
1 answer

Why does hasOne and BelongTo relationships load only ids but not the models

I've got a problem with relations in Laravel 9.14 Eloquent. I have two models File and Project with coresponding tables in MySql database files and projects. Project migration Schema::create('projects', static function (Blueprint $table) { …
Lazzytchik
  • 13
  • 3
1
vote
1 answer

How to sync with 2 main items in Laravel syncWithoutDetaching

The Cart_product table has 4 columns: id | cart_id | product_id | size_id | quantity with relationship of belongsToMany for Cart, Product and Size. User can add a product with deferent sizes but not same size, so product->id = 1 can have size->id =…
Mehdi Yaghoubi
  • 561
  • 3
  • 8
  • 24
1
vote
1 answer

Laravel eloquent join with multiple wheres

I have 2 Models: Project Task A Project has multiple tasks and a task has only 1 project. a task also has a start week, start year, end week and end year, what i want is Select all the projects and join the tasks where task startWeek = $startWeek…
w3_
  • 64
  • 1
  • 1
  • 14
1
vote
1 answer

Laravel Query with Relation and Where/orWhere for Search field

there are 2 models. Customers & Locations. In a location single page, it should now be possible to display all customers from the location and filter them according to customer attributes. The problem here is that when you enter something in the…
Daniel H
  • 21
  • 3