Questions tagged [eloquent--relationship]

1

votes
2

answer
70

Views

Sort by relationship first in laravel

I have two tables: admins and log_doctor_infos. admins table has relationship hasOne with log_doctor_infos throught doctor_id like this. In model Admin: public function logDoctorInfo() { return $this->hasOne(LogDoctorInfo::class, 'doctor_id', 'id'); // Model LogDoctorInfo is log_doctor_infos table...
Ngoc TT
1

votes
1

answer
22

Views

Laravel - How to access related object other relashionship

I have 3 Models 'Category', 'Post', and 'User'. A Category has a hasMany relationship with Post. And a Post has a belongsTo relationship with User. I have a Category object $cat1 and i can access its posts (and the user_id) in my view, but i can't access more user data (line name) @foreach ($cat1->p...
Badr
1

votes
1

answer
143

Views
1

votes
2

answer
31

Views

HasManyThrough Relations includes a Pivot

I have a Module, Question and Category model. Module hasMany Questions. (1 to Many) Question belongsToMany Categories. (Many to Many) For a given Module, I would like to access only the Questions where category_id = X . I'm not sure what the most efficient way of doing this is. Can I do it through a...
Adnan
1

votes
2

answer
145

Views

Eloquent: How to 'WhereNotNull' from a ::with( ) result

I'm running the following query that has a with() relation. $logbook_objectives = self::whereIn('lobjective_id', $logbook_objectives_ids) ->with(['objective' => function($q) use ($course_objective_ids){ $q->select(['objective_id', 'objective_code', 'objective_name']) ->whereIn('objective_id', $cour...
Joab Mendes
1

votes
0

answer
67

Views

Laravel: How to define belongsTo in a MorphPivot?

In my project there is a Many-to-Many polymorphic relationship (Many instances of a model called Package (morphedByMany) can contain many different content types (each MorphedToMany). I've created a pivot table containing some additional fields that I'll need to access and query by, and so I've deci...
kiwibg
1

votes
5

answer
61

Views

Eloquent Relationship Between Tables in Laravel

I have three tables: collections which has id, name genre_collection which has id, genre_id, collection_id genres which has id, name I want to retrieve data from collections with generes. Collections Model class Collections extends Model{ public function genres(){ return $this->hasMany('App\Models\G...
Alaksandar Jesus Gene
1

votes
0

answer
24

Views

two Eloquent model with multiple pivot table

I have two models 'article' and 'user' model, and I want to show more than one M2M relation between them: such as bookmarks, votes, views, ... I mean I want to have more than one M2M relation for article and user model. I'm pretty stuck as to how to implement this.
Kamran_dev
1

votes
1

answer
43

Views

Laravel Eloquent relationship fetching third table/relation value

I have 3 tables in a database Transaction {'id','bill_id','remark'} Bills {'id','third_party','amount'} ThirdParty {'id','company_name',remark} The 'transaction' table has column bill_id coming from 'Bills' and Bills table has 'third_party' column which connected to ThirdParty table column -> 'id' S...
Khagesh
0

votes
0

answer
2

Views

How to insert into a table based on an Eloquent relationship an array of foreign keys

I have two models TeamMember and ProjectRequest. A TeamMember can have one ProjectRequest, that is why I created the following Eloquent relationship on TeamMember: class TeamMember extends Model { // protected $table = 'team_members'; protected $fillable = ['project_request_id']; // Relations public...
Code Worm
1

votes
2

answer
0

Views

How to get data from table related through pivot table?

I have 4 tables: countries, activities, country_activities and packages. countries and activities are related through pivot table country_activity, and packages is related to country_activity. Now, How do I eager load all packages related to each activity in a country? class Country extends Model {...
priosshrsth
1

votes
1

answer
0

Views

Change order of main query according to Eloquent WhereHas Relationship

$collection = USER::whereHas('userLocations', function ($query) use ($filterId) { $query->select(DB::raw('*, ( 6367 * acos( cos( radians('.$filterId['lat'].') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$filterId['lng'].') ) + sin( radians('.$filterId['lat'].') ) * sin( ra...
sushant
1

votes
1

answer
31

Views

Laravels syncWithoutDetaching and additional data

I have Googled my fingers sore, and I can't see anyone discussing this, but I have a suspicion that Laravels syncWithoutDetaching() method doesn't take any parameters for extra data like save(), sync() and attach() does? Does anybody know this? In the API documentation the method has the following p...
1

votes
0

answer
58

Views

Laravel5.5 Eloquent Relationships with() no effect

I have 3 model, and I already set Relation. Model A: namespace App\Model; use Illuminate\Database\Eloquent\Model; class ComponentA extends Model { protected $table = 'ComponentA'; protected $primaryKey = 'ComponentAId'; public function ComponentB() { return $this->hasOne( 'App\Model\ComponentB', 'Co...
Ignacio
1

votes
1

answer
84

Views

how to return object instead of object in array

I am trying to return a response of an object which came from a collection array due to a relation of hasMany. I have tried to do a return $block->where('date','=',$today)->first(); error said: Call to undefined method App\BlockDate::addEagerConstraints() public function block_dates() { return $th...
NewProgrammer
1

votes
3

answer
50

Views

How to join 3 tables with Laravel's Eloquent relationships with Eager Loading?

So, an order has a foreign_key offer_id. And an offer has a foreign_key item_id. An item may be in multiple offers. But every offer has one item. An offer may be in multiple orders. But every order has one offer. When I do this: $orders = Auth::user()->orders() ->with('offer') ->get(); I get this:...
padawanTony
1

votes
2

answer
14

Views

How to join 3 tables with Laravel's Eloquent relationships?

So, an order has a foreign_key offer_id. And an offer has a foreign_key item_id. An item may be in multiple offers. But every offer has one item. An offer may be in multiple orders. But every order has one offer. When I do this: $orders = Auth::user()->orders() ->with('offer') ->get(); I get this:...
padawanTony
2

votes
2

answer
187

Views

Nested relationships with Laravel faker - laravel seeder

I wanted to make a post seeder with users and comments as mentioned in the relationship section in the laravel documentation https://laravel.com/docs/5.5/database-testing $users = factory(App\User::class, 3) ->create() ->each(function ($u) { $u->posts()->save(factory(App\Post::class) ->create() ->e...
upeksha1996
0

votes
1

answer
7

Views

Laravel Eloquent for hasMany relation cannot find records

I can not find why it does not work. In my Course model I have defined relation: class Course extends Model { public function courseDates() { return $this->hasMany(CourseDate::class, 'course_id'); } } And in my CourseDate model this: class CourseDate extends Model { public function course() { return...
Michal Palus