first things first I have repositories and services in my laravel app.
Main Problem
My main problem is laravel is not compatible with sub-queries and not support them well!
-
I use below versions:
- laravel v10.16.1
- php 8.1
-
I have
deleted_at
,updated_at
andcreated_at
columns
Problem with deleted_at
If I use soft delete trait and want deleted_at
to handle automatically, when I use sub queries like below, it’ll show me error:
class UserRepository {
...
public function getWhere(array $columns, Builder $where)
{
$query = $this->model
->newQuery()
->when($where, function (Builder $query) use ($where) {
$query->fromSub($where, 'sub');
});
return $query->get($columns);
}
...
}
ERROR: `'deleted_at' column does not exists`
Problematic query Example
SELECT * FROM (SELECT * FROM `users` WHERE `name`='john') AS `sub` WHERE `users`.`deleted_at` IS NULL
In above query, deleted_at
is not exist but it exists in database and this is because of sub-query alias.
I search about it and it seems to be laravel issue some how and finally I removed automatic soft deletes and do it manually like below:
class UserRepository {
...
public function getWhere(array $columns, Builder $where)
{
$prefix = $this->model->getTable();
$query = $this->model
->newQuery()
->when($where, function (Builder $query) use ($where, &$prefix) {
$prefix = 'sub';
$query->fromSub($where, $prefix);
});
$query->whereNull($prefix . '.deleted_at');
return $query->get($columns);
}
...
}
What Should I do?
(I hope my explanation is understandable)
Thanks in advanced.
EDIT
I changed my example because I was doing wrong for update
operation and updated_at
is not problematic in my case but sub-query and set deleted_at
has problem.
2
Answers
I don’t quite understand what you are trying to do, but I think I can help you
you can just try this
to make this
If you are trying to bind a foreign key to a primary in laravel try this
add this to "users" model
and add this to other model "posts"
then you can get any things you whant from 2 table like this
Okay, usually when you create a Schema with
$table->softDeletes();
it will automatically create thedeleted_at
column in the table. So I’m not sure why you getdeleted_at
doesn’t exist.An example of a schema is like this.
As well, when use use
use SoftDeletes;
it will automatically add global scope to all queries, like wheredeleted_at
is null.To fix your issue in an alternative way, you can add
withoutTrashed()
to your query.