Laravel 5.3 update request with unique rule with exception - php

I have request for updating page. When adding new page request is like:
'title'=>'required|max:70|unique:pages',
but when I'm updating page title must be unique but need to check all other titles except one already entered.
I searched Google for all possible solutions but nothing works.
I tried:
'title'=>"required|max:70|unique:pages, title,{$this->id}",
'title'=>'required|max:70|unique:pages, title,'.$this->id,
'title'=>'required|max:70|unique:pages, title,'.$this->input('id'),
rule is inside rules method
public function rules()
{
return [
'title'=>'required|max:70|unique:pages, title,'.$this->id,
...
]
I'm getting this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column ' title' in 'where clause' (SQL: select count(*) as aggregate from `pages` where ` title` = test and `id` <> )
In my mysql I have id (lowercase) column that is primary key and title column (also lowercase).

You have a bug here:
public function rules()
{ This space is causing trouble
|
return [
'title'=>'required|max:70|unique:pages, title,'.$this->id,
...
]
Should be
public function rules()
{ Space removed
|
return [
'title'=>'required|max:70|unique:pages,title,'.$this->id,
...
]

SQLSTATE[42S22]: Column not found: 1054 Unknown column ' title'
It seems that it's searching for a column named " title", it may be stupid but double check that the column name is sent without a space at the beggining.

Related

Count in update query doesn't work in Laravel

public function increment($id)
{
$this->model->where("id",'=', $id)->update(['rating'=> DB::raw('count+1')]);
}
I am getting the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'count' in
'field list' (SQL: update news set rating = count+1, updated_at
= 2019-04-13 08:12:51 where id = 5)
I also tried
->update(['rating'=>'count+1']);
You are not telling the query builder on which table you are performing the query, so DB::raw('count+1') makes no sense.
You can try to use the eloquent increment method like this:
$this->model->where("id", $id)->increment('rating');
Thanks #Tharaka removed the extra call to save().

Column not found error while trying to mass assign values inside controller

I'm getting this error when I'm trying to assign a value to all the columns in a table.
Controller
public function updateallcompany(Request $request, $id)
{
$active = $request->input('active');
AccessCode::where('company_id', $id)->update([$active, 'active']);
return view('pages.accesscode.showallaccesscodecompany')
->with('success', "AccessCodes Updated");
}
Error
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field
list' (SQL: update access_codes set 0 = Yes, 1 = active, updated_at = 2019-04-11 11:10:03 where company_id = 2)
You must pass the $active value to the field active.
You can see Mass Updates example on laravel documentation:
https://laravel.com/docs/5.8/eloquent
Try:
AccessCode::where('company_id', $id)->update(['active' => $active]);

How to change id to uuid?

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from shops where username = csesumonpro and id 5d4cb970-197d-41db-90e1-26988d543935)
how to change id to uuid when execute query? i am using uuid in my model replace of id i already defined
protected $primaryKey = 'uuid'; // in my model
Show above error when use unique email validation, unique email validation execute a query query autonomic capture id but i need uuid .otherwise everything is ok.
'email' => 'unique:users,email,'.$user->id // validation code
You need to specify the uuid column name as you are not using numerical IDs.
So it should read:
'email' => 'unique:users,email,'.$user->uuid.',uuid'

SQL column not found although it exists

I'm using the Laravel framework to create a prototype and need to output an individual article from a database based on it's ID. Seems straightforward but I keep getting the following error when I query the database:
QueryException in Connection.php line 647:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'articles.id' in 'where clause' (SQL: select * from `articles` where `articles`.`id` = 1 limit 1)
I'm trying to retrieve a specific article based on the database column article_id via the articles table in my Article model. I am using a resource route thus the method name 'show' - so I can access via article/{$article_id} in the URL.
Here is my controller:
public function show($article_id) {
$article = Article::find($article_id);
$article->toArray();
return view('articles.view', array('article' => $article));
}
There is no articles.id. It should be articles_id. Am I missing something obvious here?
Thanks in advance, any pointers much appreciated.
The find command looks for whatever is set as the primary key, which by default is id. You can change the primary key in your model:
class Article {
protected $table = 'articles';
protected $primaryKey = 'articles_id';
}
find() searched by primary key, so change the query to:
$article = Article::where('article_id', $article_id)->first();

using unique rule of laravel 4 on updating database records

i have this table weapons that have the columns weaponID, weaponName, wStock, wDesc
in my update function inside the controller, i have this set of rules
public function c_updateSystemUser($id)
{
$rules = array(
'weaponName' => 'required|min:2|max:50|regex:/^[a-zA-Z0-9\-\s]+$/|unique:weaponTbl,weaponName,' . $id,
'wStock' => 'required|numeric',
'wDesc' => 'required|min:1|max:100|regex:/^[a-zA-Z0-9\-\s]+$/'
);
//other stuff
}
when i updated and only changed the wStock it is having problems because it is saying that the weaponName already existed. so i used the unique rule
unique:weaponTbl,weaponName,' . $id,
because i wanted to except the record whose im currently working on but when i tested it i'am having these errors
Column not found: 1054 Unknown column 'id' in 'where clause'
(SQL: select count(*) as aggregate from `weaponTbl` where `weaponName` = unicorn and `id` <> 1)
why is that it using id only whereas i don't have any 'id' in my table but only weaponID? is there a way to work around with this?
Inside your weapon class you need to change the primary key:
class Weapon extends Eloquent {
protected $primaryKey = 'weaponID';
}
And then change the unique rule to:
unique:weaponTbl,weaponName,' . $id . ',weaponID'

Categories