Category : relation

I got a problem when writing a single relation method with laravel project. I have 2 table: residents with columns: id, building_id, phone. building_id and phone have an unique constraint services with columns: id, building_id, phone. building_id and phone don’t have an unique constraint I have a method get resident from service model: public function ..

Read more

#ask How do you call the relation and its sub-relations in the model? I made an AdditionalInformation model in which there is a relationship with telephone and additional data, and I want to call the AdditionalInformation model and its relationship in the ApplicationCloseGarden model I want relation model AdditionalInformation with its relation public function additionalInfo() ..

Read more

I have 3 tables User (id,name,mail,mobile) Contest (id,contest_name,contest_description) Contest_user (id,user_id,contest_id) I want to write a has many contest user method in the contest model also I need the user details from the method. Kindly tell me how can I get the desired result. Model Screenshot <?php namespace AppModelcontest; use AppModelProjectContestUsers; use IlluminateDatabaseEloquentModel; class ProjectRequestGroupMaster extends ..

Read more

I have a relation that has a where condition based on one of the properties of the main model. // Product model public function optionValues() { $relation = $this->hasMany(ProductOptionValue::class); if ($this->stock_status_id == Product::CustomStock) { return $relation->where(‘status’, 1); } return $relation ->where(‘price’, ‘>’, 0) ->where(‘quantity’, ‘>’, 0) ->where(‘status’, 1); } I try to find a product ..

Read more

$this is namespace AppModelsProduct; protected $fillable = [ ‘name’, ‘slug’, ‘company_id’, ‘original_price’, ‘discount_price’, ‘code’, ‘barcode’, ‘quantity’, ‘parent_id’, ‘position’, ‘status’, ‘short_description’, ‘long_description’, ‘approved’, ‘auto_approve’ ]; enter code here public function childProducts() { return $this->hasMany(Product::class, ‘parent_id’, ‘id’); } my scope public function scopeActive($query) { return $query->where(‘status’, 1); } Sourc..

Read more

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