Category : relation

How to filter relationships? I found models from related data. How do I select only those related models that match the search term? Basic query $toDolists = ToDoList::select([‘*’]); foreach ($tags as $tag) { $toDolists->whereHas( ‘item.tag’, function ($query) use ($tag) { $query->where(‘tags.name’, $tag); } ); } I want to select related models (item) that match the ..

Read more

I am running into this issue where I can’t retrieve the softdeleted data of the relation tables. My approach was to use the withTrashed method, but it gave me no luck whatsoever. This is the line of code: return response()->json($user->orders()->withTrashed()->with(‘order_details’, ‘order_details.product’, ‘address’)->get()); It like ignores what’s inside the with clause. How do I fix this ..

Read more

I have tables, ec_products : id | brand_id (id from ec_brands) | store_id (id from mp_stores) ec_brands: id mp_stores: id I am calculating total products belong to each brand and store using relations and withCount of Laravel, Like, Brand model, public function products() { return $this->hasMany(Product::class, ‘brand_id’)->where(‘is_variation’, 0); } Stores model public function products() { ..

Read more

When I use User model to return the data I need it gave me list of objects with all the relations. User Model class User extends Model implements AuthenticatableContract, AuthorizableContract { use SoftDeletes, Authenticatable, Authorizable, HasFactory, Notifiable; public function getNameAttribute() { return $this->last_name.’ ‘.$this->first_name; } public function service(){ return $this->BelongsTo(Service::class); } public function group(){ return ..

Read more

I’m trying this code $query = Parent::where(‘state’, 1) ->with(array(‘child’=> function ($q) use ($end_date) { $q->where(‘start_date’, ‘<=’, $end_date); })); $query->whereHas("child", function ($query) use ($filter) { if (isset($filter["id"]) && $filter["id"] != "") { $query->where("id", ‘=’, $filter["id"]); } }); then in Parent Models i have this code public function child() { return $this->hasOne(‘AppModelsChild’, ‘code’, ‘code’); } I want ..

Read more

I have user image saved on different table and I want have the following in User model public function Image() { return $this->hasOne(UserImages::class, ‘user_id’, ‘id’)->latest(); } The above relation returns the following. "image": { "id": 3, "user_id": 1, "image": "http://live.test/uploads/user/User-Oss8MewXVzHZCehHoOUgkdYoo3N1K0gYI9jY69ZsnyiHnqHsHv.png", "is_primary": 1, "created_at": "2021-04-12T08:01:47.000000Z", "updated_at": "2021-04-12T08:01:47.000000Z" }, I want to receive only image, how can ..

Read more

I am trying this code but in whith where query does not work any One can solve this problem? Quotation::with([‘QuoParts’, ‘client’=>function($cq){ $cq->orWhere(‘first_name’, ‘LIKE’, ‘%Muhammad%’); }, ‘user’])->orWhere(function($q) use ($s){ $q->orWhere(‘sku’, ‘LIKE’, ‘%’.$s.’%’); $q->orWhere(‘issue_date’, ‘LIKE’, ‘%’.$s.’%’); })->take($length)->skip($r->start)->get(); Sourc..

Read more

how to select data in this relation count by qrcode and sum price if not null? $wajibRetribusis = WajibRetribusi::with([‘subDistrict’=> function($q) use($month,$year){$q->select(‘name’,’id’);},’payments’=> function($q) use($month,$year){ $q->whereMonth(‘payments.last_paid’,$month); $q->whereYear(‘payments.last_paid’,$year); $q->select(‘id’,’price’,’qr_code’,’category_id’,’last_paid’); },’category:id,price’])->select(‘sub_district_id’,’qr_code’,’category_id’)->get(); Sourc..

Read more

i have two model Admin Model -> i use to save Admins inside it and i make Guard admin and his own routes in admin.php User Model (Default in laravel)-> i use to save Normal users inside it and and his own routes in web.php Comments Model : $table->id(); $table->string(‘comment’); $table->boolean(‘approved’)->default(0); $table->unsignedBigInteger(‘user_id’); $table->foreign(‘user_id’)->references(‘id’)->on(‘users’)->onDelete(‘cascade’); $table->unsignedBigInteger(‘post_id’); $table->foreign(‘post_id’)->references(‘id’)->on(‘posts’)->onDelete(‘cascade’); ..

Read more

hi I have 3 table that is Resort, Booking, Expense, these tables are join with relation. the code is given below, $resorts = Resort::where(‘status’,1)->with(‘bookings’)->withSum(‘bookings’, ‘amount’) ->with(‘expenses’)->withSum(‘expenses’, ‘amount’)->get(); I want to sort this table using the date field. how could I use the wherebetween in this query for bookings and expense Sourc..

Read more

I have the following query that should return relations but is not. The models involved are Person and Role. The Person relation: public function roles() { return $this->belongsToMany(Role::class, ‘person_role’, ‘user_id’, ‘role_id’) ->where(‘person_role.org_id’, $this->defaultOrgID); } The roles relation: public function people() { return $this->belongsToMany(Person::class, ‘person_role’,’role_id’, ‘user_id’); } Troublesome Query: $persons = Person::with(‘roles’) //->selectRaw(‘person.personID, person.lastName, person.firstName, person.login, ..

Read more

This is table relation $result = Event::select(‘events.*’, ‘events.at as datatime’, ‘events.end_time as endtime’,’event_types.name as event_type_name’) ->from(‘events’) ->leftJoin(‘event_types’, ‘event_types.id’, ‘events.type_id’) ->where(‘events.id’, $id) ->where(‘events.user_id’, 350) ->get() ->toArray(); return $result; i want to change it to eloquent relation. for example, i think we can use with() method instead of select method of query. Sourc..

Read more

I am going to make eloquent model as belongsTo, hasManythorugh.. from some tables. i can make it when there are 3 tables but am not sure if there are 3+ tables. this is code. ->from(array(‘cases’,’c’)) ->join(array(‘case_contact’,’cc’), ‘LEFT’)->on(‘cc.case_id’, ‘=’, ‘c.id’) ->join(array(‘case_statuses’,’cs’), ‘LEFT’)->on(‘cs.case_id’, ‘=’, ‘c.id’) ->join(array(‘statuses’,’s’), ‘LEFT’)->on(‘s.id’, ‘=’, ‘cs.status_id’) ->join(array(‘milestones’,’m’), ‘LEFT’)->on(‘m.id’, ‘=’, ‘s.milestone_id’) ->join(array(‘companies’,’com’), ‘LEFT’)->on(‘com.id’, ‘=’, ‘c.company_id’) ..

Read more