#### Category : orm

i have Relations like User -> hasMany GamesAsHomePlayer -> belongsTo Game, what i need is count Game->where(‘result’, 1) per User, how should i do that? I am fighting with this like half day and nothin works. Sourc..

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 ..

For example, let’s take a look to this model in a laravel project: <?php //… class Post extends Model { //… public function user() { return $this->belongsTo(User::class); // User model is previously defined } } Now I can get the user (author) info of a specific post: Post::find(1)->user()->username; Of course laravel has to run some .. 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 ..

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 i want to set relationship between this three entities CARGO – ITEM – ASSET each ITEM should be has one unique CARGO each ASSET should be has one unique CARGO each CARGO has many ITEM each CARGO has many ASSET how can i design something like this in laravel? in laravel one to many morph .. Read more To provide context to my question, consider this example. We have three tables (these are fictitious) animal -> [id, name] animal_breed -> [id, name, animal, userId] animal_registration_table -> [id, userId, breedId] Now i have seen two kinds of devs, One who does not validate if the breedId being inserted actually belongs to the user. They .. Read more I have a courses table and every course has many sessions, So I need to sort courses by the first session of the course date for Laravel Project. Sourc.. Read more I’m contacting you because I absolutely can’t find the solution after having spent a lot of time on it… Here is my problem: I naturally created my tables on Laravel 5.3 (resumption of a project) for the arrival of a new feature. After setting up everything, Model/ Controller/CRUD I noticed a problem with the ORM. .. 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 is there any way to get the foreign keys from a laravel model? I’ve found that you can get the primary key by using the method getKeyName(); but havn’t found any solution to get the foreign keys. For example class Grades has the following relations: public function student() { return$this->belongsTo(Student::class, ‘student_id’, ‘id’); } public ..

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 ..

I’m using uuid as a primary key of account table and it has many to many relations with role table. I store uuid in database as binary(16) thanks to a function i created called UuidToBin in mariadb convert a uuid string to binary(16) and when i want get uuid i use another function name is ..

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..

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’ve been trying to get a variation of the Laravel Eloquent ‘has many through’ relation working. I have 3 models: Invoice, InvoiceLine and Order; I would like to retrieve a distinct set of all Orders of a given Invoice through its InvoiceLines. Invoice has 1 or more InvoiceLines; InvoiceLine has exactly 1 Invoice; InvoiceLine has .. Read more I have two migration tables and two models 1)User. 2)Subscription. in subscriptions Model i write one method called user() which has hasMany relationship and in Users model it will contain subscriptions() method which have belongs() relationship. in user table id acts as an primary key and in subscription table sub_id acts as a foriegn key,for .. 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 ..

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 ..