Category : eloquent–relationship

I have three Models. University, Professor and Student. They have some relations. University Model : <?php namespace AppModels; use IlluminateDatabaseEloquentModel; use AppModelsUser; class University extends Model { protected $fillable = [‘name’, ‘user_id’]; /** * Get the user that owns the university. */ public function owner() { return $this->belongsTo(User::class); } } Professor Model : <?php namespace ..

Read more

I have a requirement. My DB has tables like the following. The tables have OneToMany (1-n) parent-child relation. Table School (id, school_name) Table Class (id, school_id, class_name) Table Section (id, class_id, section, no_of_seats) Table Student (id, section_id, student_name, ….) When Some Student is registered, data is uploaded to the Student table. Now, I want to ..

Read more

I have a requirement. My DB has tables like the following. The tables have OneToMany (1-n) parent-child relation. Table School (id, school_name) Table Class (id, school_id, class_name) Table Section (id, class_id, section, no_of_seats) Table Student (id, section_id, student_name, ….) When Some Student is registered, data is uploaded to the Student table. Now, I want to ..

Read more

I need some specific columns from two relations. In my questions model I have two relations public function ans_options() { return $this->hasMany(‘AppModelsAnswerChoices’, ‘ac_quest_id’, ‘q_id’); } public function question_category() { return $this->hasOne("AppModelsCategories", ‘cat_id’, ‘q_category’); } I tried Questions::with(array( ‘questionCategory’ => function($query) {$query->select(‘cat_id’,’cat_value’);}, ‘ans_options’ => function($query1) {$query1->select(‘ac_id’,’ac_choiceTxt’);} ))->get(); am getting only the columns of question_category not in ..

Read more

I am facing an issue please if someone can help I want to get sorted inventory by apply orderBy clause on eager loaded table column blow is my code and you can check laravel doc for the reference https://laravel.com/docs/8.x/eloquent-relationships#constraining-eager-loads my code $inventoryFilterQuery = Inventory::with([‘inventoryPrice’ => function ($query) use ($request) { return $query->orderBy(‘inventory_price’, ‘desc’); }])->get(); but ..

Read more

Let’s say Here . I have an extra column on intermediate table ‘noOfUpdates’ then how can i access its previous value. I tried this but it didn’t work. $user = User::find(1); $user->roles()->updateExistingPivot($roleId, [ ‘noOfUpdates’ => noOfUpdates + 1, ]); In raw SQL i can to do that. n_enroll column sorry for my english 🙁 PLEASE ..

Read more

Phone Model(Child) class Phone extends Model protected $guarded = []; public function user(){ return $this->belongsTo(AppModelsUser::class); } } User Model(Parent) class User extends Model{ protected $guarded = []; public function phone(){ return $this->hasOne(AppModelsPhone::class); } } I know we can do something like this: USE AppModelsUser; USE AppModelsPhone; $user = new User(); $user->name = ‘John Doe’; $user->email ..

Read more

I have the following Models: Account: class Account extends Model { protected $attributes = [ ‘id’ => false, … ‘model_id’ => null, ‘model_type’ => null ]; public function model() { return $this->morphTo(); } } Driver: class Driver extends Model { protected $attributes = [ ‘id’ => false, ‘name’ => false, … ]; public function accounts() ..

Read more

How to Convert the following query into Eloquent specially the sub query in query and COALESCE and Group By in Eloquent select sub.activity_id,sub.district, COALESCE(sum(general_reports.quantity),0) as quantity from (SELECT activities.id as activity_id, project___districts.district FROM project___districts INNER JOIN activities ON project___districts.project_id = activities.project_id ) as sub LEFT JOIN general_reports on general_reports.activity_id=sub.activity_id and sub.district=general_reports.district WHERE sub.activity_id=’$id’ GROUP BY ..

Read more

i wanted access the value of content_type_id .i tried like $courseChapters[‘chapter_content’][‘content_type_id’] , $courseChapters[‘content_type_id’] , $courseChapters[0][‘content_type_id’] & $courseChapters[‘chapterContent’][‘content_type_id’].All these shows the error ErrorException: Undefined index: content_type_id . I also tried everything in the foreach loop i’ve commented below. Nothing works. Can someone tell me how to fix this? $courseChapters = courseChapter::with([‘chapterContent’ => function ($q) use ($id) ..

Read more

i wanted to display id,FileName & FilePath from Files table and id & name from User table along with the course table columns – id,courseDisplayName & aboutCourse. But it is returning null from both files & user relations.How can i fix this? $course=Course::with([‘files:id,FileName,FilePath’,’user:id,name’]) ->select(‘id’,’courseDisplayName’,’aboutCourse’) ->where(‘userId’,$request->tutorId) ->get(); Course Model public function files() { return $this->belongsTo(Files::class, ‘fileId’, ..

Read more

I’m trying to create a relationship between my ‘users’, ‘profiles’ and ‘countries’ table. Users table has the columns: id – name – email – password Profiles table has the columns: id – user_id – country_id Countries table has the columns: id – name – code The problem is that currently my code is matching a ..

Read more

i’ve an array like below for $response. i wanted to fetch the user field alone & files field lying inside the user from it . i tried like $response[‘user’] & $response[‘user’][‘files’] , it shows the error undefined index. How can i achieve this? [ { "id": 14, "userId": 1, "courseDisplayName": "yuu", "aboutCourse": "kljkl", "user": { ..

Read more