I can't figure out how to set a field to null on certain condition. It should be an easy query but I'n not able to set the right value for NULL on a nullable field.
Product::where('id', $product_list)->update(['family_id' => null]);
nor
Product::where('id', $product_list)->update(['family_id' => DB::raw(null)]);
did the trick.
Both cases compiled fine but the generated query seems to be wrong because I get a SQL error. In the irst case the error is :
SQLSTATE[HY093]: Invalid parameter number (SQL: update `products` set `family_id` = ?, `products`.`updated_at` = 2019-11-12 08:41:07 where `id` = ?)
and the error for the second one is :
QLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ', `products`.`updated_at` = ? where `id` = ?' at line 1 (SQL: update `products` set `family_id` = , `products`.`updated_at` = 2019-11-12 08:21:50 where `id` = ?)
In the first case it seems that NULL is not being added to the parameters to the prepared query, and in the secon the NULL value is being inserted as nothing in the query. There is any simple way to set the value to NULL?
You should consider the following things first.
1) family_id column is nullable in your table. if not then set nullable.
2) Make sure you have in $fillable property of Product model column family_id.
Make sure $product_list returns id of that particular product.
if $product_list is collection/array of product data then use like:
Product::where('id', $product_list->id)->update(['family_id' => null]);
Try this
Product::where('id', $product_list)->update(['family_id' => 'NULL']);
Related
I have this UPDATE in MonetDB (version 11.41.11):
with t as (select distinct cod_atc_nl_1 , desc_atc_nl_1 from uruguay.staging_rm_dims where desc_atc_nl_1 is not null order by 1)
update uruguay.staging_rm_dims
set
desc_atc_nl_1 = t.desc_atc_nl_1,
desc_atc_nl_2 = t.desc_atc_nl_1,
desc_atc_nl_3 = t.desc_atc_nl_1,
desc_atc_nl_4 = t.desc_atc_nl_1
where
desc_atc_nl_1 is null
and desc_atc_nl_2 is null
and desc_atc_nl_3 is null
and desc_atc_nl_4 is null
and cod_atc_nl_4 is not null
and cod_atc_nl_1 = t.cod_atc_nl_1;
When I executing this sentence, I get the following error:
SQL Error [42S22]: SELECT: no such column 't.cod_atc_nl_1'
Is sintax incorrect?
Thank you.
This was an issue fixed at the version 11.41.1. The with relation must be specified on the from clause.
I have a customer class and a pet class which has a many to many relationship. There is a pivot table customer_pet. Both the customers table and the pets table have a team_id.
When I write the following code:
$results = auth()->user()->team->customers();
$results->leftJoin('customer_pet', 'customer_pet.customer_id', '=', 'customers.id');
$results->leftJoin('pets', 'pets.id', '=', 'customer_pet.pet_id');
return $results->get()->load('pets');
I get this error:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'team_id' in where clause is ambiguous (SQL: select * from `customers` left join `customer_pet` on `customer_pet`.`customer_id` = `customers`.`id` left join `pets` on `pets`.`id` = `customer_pet`.`pet_id` where `customers`.`team_id` = 2 and `customers`.`team_id` is not null and `customers`.`deleted_at` is null and `team_id` = 2
I tried making the relation in the Customer model look like this:
public function team()
{
return $this->belongsTo(Team::class, 'customers.team_id', 'teams.id');
}
but I get the same result. I am missing something obvious but I am not sure what?
Any help would be appreciated. Thanks.
select * from `customers` left join `customer_pet` on `customer_pet`.`customer_id` = `customers`.`id` left join `pets` on `pets`.`id` = `customer_pet`.`pet_id` where `customers`.`team_id` = 2 and `customers`.`team_id` is not null and `customers`.`deleted_at` is null and `team_id` = 2
The query in the exception shows that there is a team_id = 2 at the end. It seems that you're adding a $query->where('team_id', 2) without qualification somewhere in your code.
You can use $query->qualifyColumn($column) to prepend the table name automatically.
$old_records = DB::connection("mysql_old_s")->table('leads')
->select(DB::raw('count(*) as source_count, source'))
->groupBy('source')
->get();
dd($old_records); // works right
This query works right.
I want to add one more column like id or created_at to select
I couldn't do it as following:
$old_records = DB::connection("mysql_old_s")->table('leads')
->select('id',DB::raw('count(*) as source_count, source'))
->groupBy('source')
->get();
dd($old_records); // it gives an error
It gives me an error that is:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP
BY clause and contains nonaggregated column 'crmclini_crmv2.leads.id' which is not functionally
dependent on columns in GROUP BY
clause; this is incompatible with sql_mode=only_full_group_by (SQL: select `id`, count(*) as
source_count, source , id from `leads` group by `source`)
you should include the columns you select within your group by clause to group by them:
$old_records = DB::connection("mysql_old_s")->table('leads')
->select('id',DB::raw('count(*) as source_count, source'))
->groupBy(['id','source'])
->get();
the second option (witch I do not recommend), you can disable only_full_group_by option in config/database.php:
'connections' => [
...
'mysql' => [
...
'strict' => false,
...
],
]
unless you have special condition, make the query group by the columns you select
I try to do a query on a view from DB, with Laravel but I get an error, and I am not sure why.
ViewData::query()->where('page', '=', '918');
"SQLSTATE[42601]: Syntax error: 7 ERROR: zero-length delimited
identifier at or near """"↵LINE 1: ...data" where "page" = $1 order by
"view_full_data"."" asc lim...↵
^ (SQL: select * from "view_full_data" where "page" = 918 order by
"view_full_data"."" asc limit 1000 offset 0)"
From Postgres, a working one.
Select * from view_full_data where "page = 918;
I found the problem.
From Maatwebsite\Excel I get an order_by that is not ok (Maybe because I don't have an ID column).
I had to add orderBy manually.
ViewData::query()->where("page", '=', "918")->orderBy('page', 'asc');
i have this raw query and i want to use it in eloquent query builder
but seems i cant use date method in eloquent and gave me this error im new in eloquent. what is the problem:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'date(prizes.created_at), user_id' in 'group statement' (SQL: select user_id,COUNT(user_id), date(created_at) from `prizes` group by `date(prizes`.`created_at), user_id` having `user_id` = 1 order by `date(created_at)` desc)
raw SQL:
SELECT
user_id,COUNT(user_id), DATE(created_at) FROM prizes
GROUP BY DATE(prizes.created_at), user_id
HAVING user_id = 1
ORDER BY DATE(created_at) DESC
limit 2
Eloquent:
$points = \App\Prize::selectRaw('user_id,COUNT(user_id), date(created_at)')
->groupBy("date(prizes.created_at), user_id")
->orderBy("date(created_at)","DESC")
->having("user_id","=",1)
->get();
what is the the cleanest and best form??
By default, Laravel tries to parse all strings as tables. This means it will add the string between `.
To avoid this, you can put the string in a DB:raw() function to let Laravel know not to parse this string and send it as-is to the database.
->orderBy(\DB::raw("date(created_at)"), "DESC")
Or use the raw method for ordering:
->orderByRaw('date(created_at) desc')