1,538 questions
1
vote
1
answer
53
views
retrieve data using where condition and sort in descending order
I am trying to retrieve category (id, category_name, status) table data using where condition and sort in descending order for id. I am writing query like::
public function category()
{
$...
1
vote
1
answer
80
views
Eloquent query for filter with multiple params from url (Some parameters may not be specified)
I am new for Eloquent Query Builder and I have some questions:
Goal: Create a query that will get me recipes that match the specified parameters from selectors:
Dish Category
Dish Subcategory
Cuisine
...
0
votes
2
answers
84
views
Laravel Query Builder: joining tables by from / where
Joining multiple tables in Query Builder is so simple:
DB::table('projects')
->select('tasks.*')
->join('tasks', 'tasks.project_id', 'projects.id')
But for now customer asks me to avoid join ...
0
votes
2
answers
45
views
Parameterized query in Laravel Query Builder
I use parameterized query in Laravel:
$objectsSQL = <<<'EOS'
SELECT
o.id AS oid,
o.name AS name,
o.fields AS fields
FROM projects p
JOIN tasks AS t ON t.project_id = p.id
LEFT ...
-1
votes
1
answer
48
views
Laravel 10 Eloquent. Morph fields type error
I`m using postgres 17.2
I have transactions table with morph columns entity_type and entity_id. When i use whereHasMorph i got a type error with entity_id because users table id is bigint
Example:
$...
0
votes
0
answers
16
views
Validation request fails on the second entry and starts to work on the third entry
I run a validation rule on a combination of 3 fields (f_name, s_name, l_name) to make sure the combination is unique in an institution, but the rule fails and don't apply until the third entry after ...
1
vote
1
answer
44
views
model the inverse of hasManyThrough relationship using a pivot model
I have the following models/tables:
branch:
id - integer
department:
id - integer
teacher:
id - integer
branch_department_id - integer
branch_department:
id - integer
...
0
votes
2
answers
105
views
$model->save() saving but with different value [closed]
When updating my cart model, I run:
$cart->transaction_id= $result->salePaymentRequestResult->token;
$cart->save();
This does update my cart. But it with different value. Why is my model ...
0
votes
1
answer
135
views
Get one to many relationship in the pivot table of another many to many relationship in Laravel 11
I have a Place, User models with a many to many relationship as the pivot table / model named Reservation, I needed to make a new relationship between the pivot table and another table called ...
2
votes
4
answers
338
views
Laravel - Error with union and cursorPaginate
I am trying to do union with DB query builder because with models is not working the union with paginate/cursorPaginate.
I have this but give an SQL error because Laravel is not building the right ...
1
vote
0
answers
94
views
How to find the average of two columns from different tables with a one to many relationship
I want to get the average time from one a patient came to when they had their first vital signs checked. It means that I have to check the time the visit was initiated (created_at) against the ...
0
votes
1
answer
51
views
Laravel relation based on column from main table
So this is the query I have constructed so far
$data['requested_units'] = RequestedUnit::with([
'user',
'agent_warrants' => function ($q) {
$q->where('type', RequestedUnit::...
0
votes
1
answer
86
views
Laravel temp attributes inside 'with' and 'each' together
I'm trying to use Laravel each method inside a with in my query builder but the attribute I'm adding inside my each method does not persist to the collection. My attribute does show when I dd($task); ...
0
votes
1
answer
24
views
Using Laravel query builder with Between and GETDATE
I'm trying to understand how I can create a query with query builder and have a where value something like
where item=1
and GETDATE() BETWEEN date1 AND date2
Thanks.
1
vote
1
answer
170
views
Joining subquery from different database in Laravel
I'm attempting to join a subquery into a main query using the Laravel Query Builder in a context similar to the example below. I assumed that the main query would recognize this by specifying the ...
0
votes
0
answers
155
views
Laravel Socialite GitHub Authentication: Redirect Issue after Successful Authentication
I'm encountering an issue with GitHub authentication in my Laravel app using Laravel Socialite. Despite setting up everything correctly and being able to retrieve user data from GitHub, users aren't ...
0
votes
1
answer
46
views
Use whereNot with elemMatch in Laravel with mongo
I have a query I use to filter results according to user profile. I have a list of countries applied to model, and I show only those where user.country matches with this array. This part works fine to ...
0
votes
1
answer
75
views
Laravel whereMorph query not working in laravel-10
$table->unsignedBigInteger('creater_id')->nullable();
$table->string('creater_type')->nullable();
$table->unsignedBigInteger('updater_id')->nullable();
$table->string('...
0
votes
1
answer
89
views
Laravel query builder or Eloquent both return null result
I am trying to return a single id from a query result. When I dump the query using the \DB::getQueryLog(), it shows the correct query. And when I plug the query into my DB manager, it returns the ...
0
votes
1
answer
286
views
Optimizing Eloquent Queries for Complex Reporting System in Laravel
I have multiple tables with millions of records, and I'm facing performance challenges with my current Eloquent queries.
I have three tables = 'sales', 'expenses', and 'profits'
Each table has ...
0
votes
0
answers
264
views
Convert Laravel query eloquent to query builder when eloquent using with('relation')
I am using Laravel 5.7 and have a query written using Eloquent. I am attempting to convert it into an equivalent Query Builder query to enhance performance. However, I am facing difficulties when ...
0
votes
0
answers
92
views
Searching for nested data in JSON (Laravel)
"I want to create a filter where the data is taken from nested JSON."
Previously, I had a table named history_product with the column transaction_details. I want to create a filter based on ...
0
votes
2
answers
86
views
Laravel : Integrity constraint violation: Column cannot be null
i'm trying to update event name through postman with the following json object:
PUT {{base_url}}events/1
{
"name":"this Event Name is Edited"
}
in my controller i'm using ...
-1
votes
1
answer
57
views
How to eager load "conditional" has-one relationship with the condition referencing to itself
Imagine I had 4 models as follows:
Year
Position
PositionAllowance : This is a pivot table of Position and Year, with the addition of "allowance" i.e. it defines the amount of allowance for ...
0
votes
1
answer
93
views
Laravel Eloquent & SQL: Grouping and Selecting Rows with Maximum Date
I'm facing an issue with grouping and selecting rows based on the maximum date in Laravel using Eloquent. I have a table named AlertsLog with columns id, price, and created_at. The goal is to group ...
0
votes
0
answers
37
views
How to get value from query using select as. It returns NULL instead of value
I have query:
$query = Task::query()->select(['tasks.*', 'rootTask.is_public as rootPublic']);
$query->leftJoin('tasks as rootTask', function ($join) {
$join->on('tasks._lft', ...
0
votes
0
answers
116
views
prevent query builder delete() method of laravel
I have a big laravel project that contains a lot of deletion in that project, in all of those deletion didn't put alert before deleting, now I can't modify the project one by one and adding alert or ...
-1
votes
2
answers
74
views
How to filter rows in query properly?
I want to filter records with relation which has another relation with conditions and with count > 0
Code:
$classClass = ClassClass::query()
->with('parent')
->with('...
1
vote
1
answer
118
views
Mysql query to Query Builder
I want to take this SQL query to a query in Laravel Query Builder. It is important that the result be of type Query Builder. Please if you can help me.
SELECT MAX(sub.total) as total, sub.vendedor
...
-1
votes
1
answer
135
views
how to handle morph relation with doctrine
i have an Entity that contains morph relation to represent relation with different Entities.
for example
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
*/
class BinderContent
{
/**
* @...
0
votes
0
answers
52
views
Query builder not populating query parameters correctly
The query builder is producing the following query. Everything is correct except for how the parameters are populated:
select
`citations`.*,
(
select
sum(`settlements`.`amount_dollars`)
...
0
votes
3
answers
584
views
How to access grammar from query builder?
I'm trying to convert a json column name with path 'foo->bar' to be compatible with mysql/postgresql using Laravel's wrapJsonSelector() method.
How can I access this method from the query builder ...
0
votes
1
answer
63
views
Laravel mass update a date column by adding a value from other column of same row
I have a following MySQL table:
id
days_remaining
final_date
1
4
2023-10-01
2
10
2023-10-01
I'm trying to mass update the final_date column. The resulting value of this column is a fixed date value eg:...
0
votes
1
answer
112
views
Laravel merge 2 queries into one
I am trying to merge query into one, is there's anyway to avoid if statement and merge the query into one query?
Each organization has different specialties, which works great in the first query, i ...
0
votes
0
answers
52
views
how to get selected options belongs to product
Thank you all in advance ,
i have peoject for ecommerce app
please check simplified DB schema for better unsrstand
products
- id
- name
- some other columns ...
product_items
- id
- product_id
- some ...
0
votes
0
answers
27
views
I am using laravel one to one relation and getting the null value exception on the data from other table
I can access my holder class data but i can't seems to access data that is coming from devices table. i have used postman to check my api and it return the data ok and also when i print_r the data on ...
0
votes
1
answer
41
views
Issue with Eloquent: Retrieving Entries Based on Multiple Conditions
I'm facing an issue while trying to retrieve entries using Eloquent that meet specific criteria involving filtering on two related models. The goal is to first select reservations with a "Checked-...
0
votes
0
answers
55
views
How to filter whole dashboard data, based on the authenticated user region
i'm facing this problem where i must show only data related to the region that the current user belongs to, but if you're let's say the global manager of the solution you could see all of the data.
i'...
0
votes
1
answer
112
views
Get Only the value of field mongodb using laravel
My question is similar with NodeJS get only value of MongoDB result.
But in this case, i'm using laravel.
This is my controller :
public function validation(Request $request)
{
$email= $...
-1
votes
2
answers
35
views
How to select records from DB in Laravel that are within the range of days of each month
I have table with records. In table i have column 'payment_date', and it's integer with unix timestamp. I need to select records from the database that are in the time interval of two days for each ...
0
votes
1
answer
174
views
How to get Employee wise tasks count in laravel along
I want to get each employees task count in a month and integrate into bar chart,i managed to get total count of tasks based in a month i am confused how to do that user based,let's say i have 10 ...
2
votes
0
answers
334
views
Laravel Union and pagination
I am trying to achieve a result using Laravel Union.
[
{
warehouse_id: 12,
warehouse_name: "new",
wi_id: 19,
wi_type: "storage",
wi_name: "1",
wr_id: 20,
...
0
votes
2
answers
203
views
Updating multiple records in a single query from existing data using the Laravel query builder
I have an inventory system with bins, lots, and transactions.
Bin:
has many lots
Lot:
belongs to a bin
belongs to many transactions
transaction_lot (pivot)
- quantity
Transaction:
belongs ...
0
votes
2
answers
759
views
Apply eloquent eager limit query using SQL ROW_NUMBER in Laravel
As @JonasStaudenmeir answered on laravel eager loading with limit, which query looks like:
User::select('id')
->with([
'posts' => fn($query) => $query->select(['id', 'user_id'])...
2
votes
1
answer
1k
views
Bind array param in raw query with WHERE IN/WHERE NOT IN
How can I properly bind an array for a raw query's NOT IN / IN?
The following throws an array to string conversion error:
$user_list_to_exclude = [1, 2, 3];
$data = DB::select("...
1
vote
1
answer
372
views
How to split Laravel query result at the end after doing some common query at beginning?
I want to split a Laravel query result at the end of the query after doing some common query in the beginning. Scenario, code smple and my try is given below.
Code sample
$bankSlipIds = [1,2,3];
$...
0
votes
1
answer
104
views
Not able to use orderBy in Laravel query with distinct
Hi I am using Laravel 10 .
My issue is not able to use orderby() with distinct().
I am using PostgreSQL as database.
When I tried Group_by , I am getting repeated results. I have tried many solutions ...
0
votes
1
answer
43
views
Laravel query optimazing
A project has multiple task. A task is belongsTo a project
A task is belongsTo a user, and a user has many tasks
A user have an avatar
So I can get the users thats belongs to the project through the ...
0
votes
1
answer
97
views
Laravel OrWhere orWhereHas slowing down the application
The following code checks the whether subscriber can access the property or not.
If property subscriber_id == current subscriber id, can access it.
If property subscriber_id within linked offices ...
0
votes
2
answers
229
views
How can I join Two Tables with and intermediate Table and gather data?
I am working on a project and I needed to retrieve data from an Table A to Table C with a Table B as an intermediate using the HasManyThrough from Laravel Doc Has Many Through.
I have three Tables ...