memele memele

How to sort posts by latest commented

I have a One to Many relationship for a Post model and a Comments model. I want to sort posts by latest commented - if someone posts a new comment that post goes to the top...
sunedall sunedall

Querying model based by two different pivot tables

Hi folks. Been scratching my head with this one, since I'm not hardly skilled in Laravel and Eloquent yet. I've been searching in here (...
cristian9509 cristian9509

Slow queries with Eloquent, always over 1600ms

I have two Lumen apps in production (both AWS, EC2) and from time to time I see random queries that take exactly over 1600ms. I am using DB::listen to log the query includi...
Belhedi Belhedi

Retrieve data with eloquent relationships

Hi guys.. I have a model called (Departement) which have a one to many relationship with the (Teacher) model : class Departement extends Model { protecte...
thebigk thebigk

Grouping By Given Values in Single Column

My database table is as follows - conv_id | user_id 1 | 100 <-- exists 1 | 200 <-- 3 | 500 3 | 600 4 | 100 <-- 4 | 800 <--...
skoobi skoobi

Syntax error or access violation: 1075 Incorrect table definition

Hi. Im trying to setup a migration which on delete cascades but i keep getting the error:: SQLSTATE[42000]: Syntax error or access violation: 1075 Inc...
samuelchlui samuelchlui

Eloquent::find method call constructor 3 times?

Why User::find method call User constructor 3 times? In api.php Route::get('/abc', 'App\[email protected]'); In UserController.php...

I could use some assistance in formatting a query

I have a query that I'm struggling to get to bracket correctly, query appears at the end of this post I have three relations: User User->offices()...
Adcade Adcade

Eloquent OneToMany relationship with multiple possible columns

I have the following tables: categories: id, name products: sku, name, category1, category2, category3 Where category 1, 2 and 3 are the...
jijoel jijoel

belongsTo object from member of belongsToMany relationship?

I'm having a hard time figuring out how to get an object connected to a belongsToMany relationship, and hoping someone can help. Essentially, I have a many to many relation...
lambooni lambooni

Query Builder - Sum with groupBy on relationship

I am looking for a query builder solution for the following: Table: transaction_types | id | type_hash | description | category | |----|----------...
FareedR FareedR

Achieve available venue on that date.

i try to achieve "mini stadium" but collection still appear "stadium" and "mini stadium". should i use first() or get() ? or my query are wron...

How to use eloquent in view?

This doesn't work @foreach(User::all() as $key => $val) {{ $val->name}} @endforeach...
r_duong89 r_duong89

Eloquent Create Method Error When Testing Build A Forum

Hi all, I'm following the Laracast tutorial on Building A Forum with Laravel and when I do my testing, all of a sudden I'm getting errors when testing as seen here:...
Ap3twe Ap3twe

Updating Database using updateOrCreate method

Hey, am inserting images to a database. I tried updateOrCreate but It replaces every record in the database with the new data. I do not want to update just insert data to t...
thebigk thebigk

Migrations: Adding foreign_key when table is yet to be created?

I'm creating two tables that reference each other's columns- > create_conversations_table $table->bigIncrements('id'); ... ... $table->unsignedBigI...
MartinZeltin MartinZeltin

Query takes 10 seconds if I add orderBy and paginate

I have this query but it is taking 10 seconds to execute. If I remove orderByRaw nad paginate then it only takes 15 ms. But I need to order it and paginate it. Is there ano...
nghiepit nghiepit

Laravel 5.1: Eloquent relationship hasMany, Limit records

Hi all, I have a problem with Laravel 5.1: Eloquent relationship hasMany, Limit records. I have 2 tables: feeds, comments. The request is...
packy packy

Scope filter relationship and where clause

So I am doing a scope filter on a model Account. An account has many users. I am trying to get all the accounts that user has and then filter down by the name....
erozas erozas

Database design for recurring events

Hey what's up. I'm having trouble wrapping my head around a database design issue. The thing is that I have to design an application in which the user can create events bas...
jaheller jaheller

eloquent update latest

I'm using this code inside my model to make sure a "primary" wallet is selected. self::updated(function($model){ //check if there is s...
jorisvanandel jorisvanandel

Call to a member function addEagerConstraints() on integer with scope function

One of my models has a relationship with another model and I want to put the sum of one of it's values in a scope so I can call it conditionally. Right now I have th...
esmaill23 esmaill23

eloquent query that sum of two column greater than a number

i want to build an eloquent query sum of two column greater than a number like this $query = Transaction::query(); $query = $query->where('plan_price - fi...
ludo1960 ludo1960

Insert predefined values into mongodb

Hi guys, I can insert a Boolean as true or false like so: 'color' =>$faker->Boolean, But what if I want to make predefined valu...
twoarmtom twoarmtom

Inserting the information for a range of dates

I'm trying to find examples of properly looping through a range of dates to insert information. Currently, I am creating a schedule row with the user, location etc.,...

Getting Model's morph class without an instance

When using a custom morphMap, you can get the morph class for a model using the getMorphClass() instance method. The inverse operation can be found in a static...
Laraveldeep Laraveldeep

List of Unique Users with Certain Role From Many To Many Relationship

Hi, I have something like this users table --------------- id, <other fields> roles table ------------------ id, rolename, permission role_use...
eleven0 eleven0

Members(Persons and Companies) > Teams > Projects Members ->Project

Hi there, can someone share some opinions about a membership structure I am trying to establish? I already have; Projects have many teams (But team can have one proj...
hanzanati hanzanati

Eager Loading Null Nested Relationship Causes Memory Error when Null

I am eager loading a nested relationship and it works without issue. However, when I eager load a nested relationship, where the 2nd relationship contains a null val...
Maks0815 Maks0815

Select only specific attributes with eager loading

Hey is it possible to get specific attributes with eager loading, i want to select only one attribute from the role table for example. Thanks in advance:) $u...
mikkelbfriis mikkelbfriis

Laravel collection merge removes an item?

Hiya, casts! I've recently been trying to figure out why my eloquent->merge removes an item? $value['city'] = 'KĂžbenhavn'; $advertisements = App\Adverti...
timgavin timgavin

Trouble with defining relationships

I'm having a hard time figuring out the relationships between my tags and posts. I created a tagging system that would store tags in one table (tags) an...
joshblevins joshblevins

Using array in search filters

I have followed this tutorial to build a filter on one of my pages.......
jorisvanandel jorisvanandel

Only getting accessor values on specific queries

Hi, I got a few accessors defined in my Model that take some time (they sum certain values of a relationship). It's not that bad but I only want these accessors call...
Create a New Discussion Button