Laravel - using different id for unique validation - php

I'm using itemId instead of id and when I validate as
'barcode' => 'required|unique:item,barcode,' .$this->get('itemId'),
I get
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from item where barcode = 5391510260790 and id <> 10)
How do I tell Laravel to use the itemId instead of id in the unique validation in the request class at the authorize() function?

You can specify the column to check using the fourth parameter to the rule:
'barcode' => 'required|unique:item,barcode,'.$this->get('itemId').',itemId',

If you see the documentation it said, you must follow the following structure to tell which one is the id column.
unique:table,column,except,idColumn
You have to replace the idColumn with the name of the id column of yours. So the final code will be like this:
'barcode' => 'required|unique:item,barcode,' .$this->get('itemId').',itemId',

Related

Laravel 5.8: SQLSTATE[42S22]: Column not found: 1054 Unknown column

I have a table called user_wallet like this:
Then at the Controller, I tried this:
try {
if ($request->pay_wallet == '1') {
$us_id = Auth()->user()->usr_id;
$user_wallet = UserWallet::find('user_id', $us_id);
dd($user_wallet);
}
}catch (\Exception $e) {
dd($e);
}
But I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '2' in 'field list' (SQL: select 2 from user_wallet where user_wallet.id = user_id limit 1)
However as you can see in the picture, there are two wallets with the user_id of 2.
So what's going wrong here? How can I solve this issue?
the find method only works on id so it's better to use where clause
$user_wallet = UserWallet::where('user_id', $us_id);
I think you are using find() wrong, this method accepts only one paramater, it is the value of the primary key you want to find. Because you are in a pivot table and there is no id present,
you will have to use firstWhere('user_id', $us_id) for the first occurrence or you will have to rewrite the find() method for the pivot table.
But please be aware that a user could have multiple wallets, so it might be better to ask them which wallet to use.

laravel 5.4 validate array with custom rule

I'm having trouble validating an array of fields that requires a custom rule. I have the following validator:
$validator = Validator::make($request->all(), [
'order' => 'required',
'service_id.*' => Rule::unique('order_services')->where('order_id', $request->order),
'due_date.*' => 'required|date',
'vendor' => 'required|integer',
'instructions' => 'string|nullable',
'lock_box' => 'string|nullable',
]);
The due date array validates just fine, but the service id returns the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'service_id.0' in 'where clause' (SQL: select count(*) as aggregate from `order_services` where `service_id`.`0` = 10 and `order_id` = 100f)
The service id rule prevents duplicate records with the same order number and service id
The rule works as expected when validating a single service ID, I'm just not sure how to validate multiple service IDs at the same time.
Thanks in advance
By default Laravel uses key for unique role and because it's an array, keys are 0, 1, 2 and so on. The solution is adding column name for unique rule like this:
Rule::unique('order_services', 'id')->where('order_id', $request->order),
Obviously in your case you might need other column name than id (it depends on your application)

how to create static field in mysql db select query using codeigniter active records?

In MySQLquery .I can write this
SELECT 500 as myValue FROM `myTable` WHERE 1 ;
so how to write this in codeigniter active records.
what i have tried
$this->db->select("$valas as myValue ");
but this gives
Unknown column '500' in 'field list'
Need solution apart from this
$this->db->query("$valas as myValue ");
thanks in advance.
try passing false as second parameter in select
$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or
table names with backticks.
$this->db->select("500 as myValue", false);

Cakephp 3: use of "$this->alias()" in "where" conditions

To avoid the Column 'xxx' in where clause is ambiguous error when making queries involving multiple tables sharing the same name for a given column, I now systematically include $this->alias() in where conditions, like this :
$table->find()->where([$table->alias() . '.field' => 'value']);
Is this considered a good practice?
For some reasons I ignore, doing so leads to an error when using updateAll instead of find for tables that contains an underscore. Example:
$table = TableRegistry::get('BlogPosts');
$table->updateAll(
[$table->alias() . '.title' => 'new title'],
[$table->alias() . '.id' => 1]
);
will throw the error:
Column not found: 1054 Unknown column 'BlogPosts.id' in 'where clause' for the prepared query UPDATE blog_posts SET BlogPosts.title = :c0 WHERE BlogPosts.id = :c1
Is this a cakephp 3 bug? Or is it me who is misusing $table->alias()?
You should not alias the conditions for updateAll. The reason is that only one table is involved in that method, so the alias is not required

Unknown column in 'where clause?

I Have the following code of php
$query = sprintf("SELECT to_go.to_location FROM to_go
INNER JOIN to_location ON to_go.to_location_id = to_location.id
WHERE match(to_location ) against(%s)", mysql_real_escape_string($location));
i tried every thing but it keep output me that following error "Unknown column in 'where clause ?" i tried to change the names of the columns and still the same problem
match(to_location ) against needs to be provided a field, not a table:
match(to_location.id) against(something)
I guess you may need to replace
WHERE match(to_location )
with
WHERE match(to_go.to_location)
Since you have a column name the same name as a table name, MySql is probably confusing them and thinking match(to_location) refers to the table. Try using the fully-qualified column name, i.e., table_name.column_name.

Categories