Category : orm

I am implementing a Laravel REST API backend and I have two tables, trip_announcements (user announcing their trip) and countries (self explanatory). Each announcement can have multiple countries. I ended up creating two functions: //ModelsTripAnnouncement public function countries(){ return $this->belongsToMany(Country::class); } and //ModelsCountry public function trips(){ return $this->belongsToMany(TripAnnouncement::class); } Then I created a new migration ..

Read more

How can I pass a list of dynamic months to laravel entities without using multiple ‘wheres’? For example: $goalsData = MyEntitie::whereMonth(‘created_at’, [’04’, ’12’])->get(); The example above "works", but it only recognizes the first item in the array. Why? Note: I need a way that does not use multiple wheres and accepts dynamic months, the list ..

Read more

I would like to get items where used_items < limit but i get also used_items >= limit. I tired to try use having but didn’t work either. public function getAllowedItems() { $items = Item::leftJoin(‘runners_items’, ‘runners_items.item_id’, ‘items.id’) ->leftJoin(‘subrace_items’, ‘subrace_items.item_id’, ‘items.id’) ->where(‘subrace_items.sub_race_id’, $this->id) ->select(‘items.*’, DB::raw(‘count(items.id)-1 as used_items’)) ->where(‘items.limit’, ‘>’, ‘used_items’) ->orWhere(‘items.limit’, 0) ->groupBy(‘items.id’)->get(); dd($items); } Sourc..

Read more

Here I have two model. User and Company. Inside User model: public function company() { return $this->hasOne(Company::class); } Inside Company model: public function user() { return $this->belongsTo(User::class); } And when I fetch users using query below: User::query() ->with(array(‘company’ => function($company) { $company->select(‘id’, ‘company_name’); }))->get()->map->only([ ‘id’, ’email’, ‘has_employees’, ‘created’, ‘status’, ‘last_login_ago_day’ ]); This code returns only ..

Read more

i need to make update some field on the document and remove others, is possible to make both operations on same action? i use https://github.com/jenssegers/laravel-mongodb for making operations on mongodb document for example is use for update but i not understand how i can try unset: Doc::where(‘_id’, $doc[‘_id’])->update($update); How i can add unset operation on ..

Read more

The following code works fine and gets CanBeModified as 1 if the subquery exists, and if not null, I want to return true or false instead, how can I customize it? $category->select(‘*’)->selectSub(function($query){ $query->whereExists(function ($query){ $query->from(‘category_resource’)->join(‘cars’, function ($join){ $join->on(‘category_resource.resource_id’, ‘cars.id’); $join->on(‘category_resource.resource_type’, DB::raw("’cars’")); })->where(‘category_resource.category_id’, DB::raw(‘categories.id’)); })->select(DB::raw(1)); }, ‘CanBeModified’); i suppose that case or IIf operator needs to ..

Read more

I want to export query results to pagination. And the pagination should have the following data results per page, column names, offset. I tried this code $customer = Customer::where("customer_id", $request->customer_id)->first(); $customer->paginate($request->limit, [‘*’], $request->offset)->toArray(); But this code returned me a full of customers, i dont want this. It ignores the where condition. As i said before ..

Read more

How is possible make this query convert to Laravel Eloquent query? I don’t want to use DB::query(""); select * from (select o.id, o.operation_id, kt.date, o.product_id, tr.nomi as product_name, o.birlik_id, br.nomi as birlik, o.money_id, v.nomi as money, o.store_id, ob.nomi as store_name, kt.user_id as user_id, kt.hamkor_id, u1.name as hamkor_name, tr.category_id as category_id, tr1.nomi as category_name, o.price as ..

Read more

I’ve a problem when sorts data from largest to smallest from table relations that do not necessarily have a relationship with PHP Laravel Eloquent. Table: items |—————————| | id | name | price | |—————————| | 1 | Samsung | 70.000 | | 2 | iPhone | 90.000 | | 3 | Nokia | 50.000 ..

Read more

I am trying to get the first transaction that was recorded close to a particular date using the Laravel 8 DB Facade. I have tried to use it in the following manner $lastest_transaction = DB::table($targetTable) ->where(‘date’, ‘<=’, date(‘F’)) ->where(‘id’, ‘=’, $client->id) ->where(‘branch’, ‘=’, session(‘branch’)) ->get(); but it’s not giving me the correct transaction. Sourc..

Read more

I have done much research about this but I can’t fetch the pivot table as I want. I want to just fetch the measurement value from the pivot table-like structure you can see. Please check my all details first then you know what I want to show. Model MeasurementPart.php public function customers(){ return $this->belongsToMany(Customer::class, ‘customer_measurements’, ..

Read more

I would like to preface this by saying that I’m new to Laravel and Eloquent ORM. I’m currently using Laravel 5.8. I have a Model ReturnRequestthat has a ReturnRequestCustomer, a ReturnRequestShippingAddress, and a ReturnRequestAddress. The relations look like so: ReturnRequest class ReturnRequest { public function customer() { return $this->hasOne(ReturnRequestCustomer::class); } public function shippingAddress() { return ..

Read more

in my Laravell Application, I need submodels of the base ORM model, for specific types of item in my DB which is specified in ‘type’ collum in DB in my base model, I use this override for function newFromBuilder //OVERIDES public function newFromBuilder($attributes = [], $connection = null) { $class = "AppModels" . ucfirst($attributes->type); if ..

Read more