Category : orm

Is there a way to use additional conditions when defining an eloquent relationship in a model? Let’s say I have: public function researchtopic() { return $this->belongsTo(‘AppResearchTopic’, ‘taggable_id’); } But i want to add an additional where condition to use a 2nd column in the relationship as well. How would I add a 2nd condition for: ..

Read more

My User.php Model function is this public function send_connection_requests() { return $this->hasMany(‘AppModelsConnectionRequest’,’from_user’); } public function received_connection_requests() { return $this->hasMany(‘AppModelsConnectionRequest’,’to_user’); } and my coonectionRequest realtionship is this-> public function send_connection_requests() { return $this->belongsto(‘AppUser’,’id’); }` public function received_connection_requests() { return $this->belongsto(‘AppUser’,’id’); }` ` When I am calling function @foreach (Auth::user()->send_connection_requests as $request) {{request->to_user}} @endforech I’m only getting ..

Read more

Having a hard time understanding how to order my Laravel model by a nested relationship. Here are the Models. User.php // Has many small_groups through a pivot table public function small_groups() { return $this->belongsToMany(‘AppModelsSmallGroup’)->withPivot(‘type’)->withTimestamps(); } SmallGroup.php // Has many SmallGroupLessons public function small_group_lessons() { return $this->hasMany(‘AppModelsSmallGroupLesson’); } SmallGroupLessons.php // Has many SmallGroupLessonComments public function small_group_lesson_comments() ..

Read more

I have these two models with one to one relationship. "products" id name minimum_required "product_data" id product_id price oh_hand I want to get the count of product_data where its on_hand is less than its related product’s minimum_required. I’ve tried subqueries and I still can’t figure it out. The query I want may looks something like ..

Read more

I have a query like this, to make count from my query, I want finding where id is with the minimum count, with result id in an array $id = Departemen::where(‘open’, 1)->where(‘name’, ‘LIKE’, ‘%LOKET%’)->pluck(‘id’)- >toArray(); $matchThese = [‘called’ => 0, ‘onserved’ => 0]; $smaller = Queues::select(‘department_id’,DB::raw(‘count(*) as total’)) ->where($matchThese)->whereIn(‘department_id’, $id)->groupBy(‘department_id’)->get(); Sourc..

Read more

I am trying to insert data in orders table and then save order details in order_details table. Following is the code that I’m trying $order = Order::create($request->all()); $order->order_detail()->insert($request->order_detail); //$request->order_detail is an array In my model I have provided relationships Order Model public function order_detail(){ return $this->hasMany(OrderDetail::class, ‘order_id’); } Order Detail Model public function order(){ return ..

Read more

I am trying to save multiple records in DB and have an array which looks like:- $insert_data = array( array(record 1), array(record 2), array(record 3) ) Now, I tried two options:- Model::create($insert_data) But it doesn’t create any entry in DB. Model::insert($insert_data) It’s creating entry in DB with null datestamps. I wanna insert multiple records with ..

Read more

I have the following code: $ids = $adGroupsOfUser->pluck(‘id’); $permittedTables = extensiontables_registry::whereHas(‘ad_groups’, function($q){ $q->whereIn(“ad_group_id”, $ids); })->pluck(‘extensiontable_name’)->toArray(); I get this error: local.ERROR: ErrorException: Undefined variable: ids in E:aether-backendappHttpHelpersmodelInteractions.php:51 Why is $ids not visble in the callback of whereHas()? What can I do to “get it inside”? Source: Laravel Sta..

Read more

I have a many-to-many relationship between extensiontables_registry and Ad_groups The pivot table is extensiontables_registryxad_groups Now I have this code: $permittedTables = extensiontables_registry::has(“ad_groups”)->get(); I want to see what this gets me so I do: log::info($permittedTables); And I get this error: [2020-05-11 07:32:23] local.ERROR: PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘extensiontables_registryxad_groups.extensiontables__registry_id’ in ‘where clause’ in ..

Read more

How to represent the query sentence in Laravel (Query builder and Eloquent) select c.image_path, c.image_x, c.image_y from banners b left outer join banner_images i on b.banner_id=i.banner_id left outer join images_links l on i.banner_image_id=l.object_id left outer join images c on l.image_id =c.image_id WHERE b.status LIKE ‘A’ AND b.type LIKE ‘G’ AND l.object_type LIKE ‘promo’; Source: Laravel ..

Read more

I have two Models (Store, Product) and Relationship hasMany public function products(){ return $this->hasMany(Product::class); } And I want to return response collection, in class StoresCollection extends ResourceCollection public function toArray($request) { return $this->collection->map(function ($item) { return [ ‘id’ => $item->id, ‘seller_id’ => $item->seller_id, ‘store_product’ => $item->products()->get(), ]; }); } But I don’t want to return ..

Read more

I cannot figure out one problem with Database notification in Laravel (https://laravel.com/docs/5.8/notifications#database-notifications). I wish showing user notification like Facebook, for example: “User X, Y and 2 other people commented your photo”. My databse structure like this: +————————————–+——————————+—————–+—————+———————————————————————+———+———————+———————+ | id | type | notifiable_type | notifiable_id | data | read_at | created_at | updated_at | +————————————–+——————————+—————–+—————+———————————————————————+———+———————+———————+ ..

Read more