How to change id to uuid? - php

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'

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().

Change the name of the column to be taken from the database

How can I change the questioned to the database which is automatically generated.I have in blade template this
{{\Illuminate\Support\Facades\Auth::user()->friends()->get()}}
and it generates
Unknown column 'friends.user_id' in 'where clause' (SQL: select * from `friends` where `friends`.`user_id` = 1 and `friends`.`user_id` is not null
because my column name is user_id1 insted user_id.
and I do not know why the query to the database takes the user_id value instead of user_id1. How can I change it ?
Define the custom foreign key in the friends relationship:
public function friends()
{
return $this->hasMany(Friend::class, 'user_id1');
}

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'

Multiple aliases of a single table column with Eloquent ORM

Is it possible to have multiple Aliases from a single table column. If I do the following in MySQL I get the result I want:
SELECT name, name AS label, name AS value FROM `tags` WHERE 1
But, if I try to do:
$query->select('name, name AS label, name AS value');
..it tells me SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name,' in 'field list' (SQL: selectname,asASfromtagswheretags.deleted_atis null group bynameorder bynameasc)
If I try to do
$query->select('name AS name, name AS label, name AS value');
..it doesn't throw an error, but instead only gives me one alias (name). How can I achieve this using Eloquent ORM? Thanks
When you try select laravel do this:
public function select($columns = array('*'))
{
$this->columns = is_array($columns) ? $columns : func_get_args();
return $this;
}
And when you pass the string, laravel thinks you passed few args, so you can do:
$query->select(['name AS name', 'name AS label', 'name AS value']);
Or
$query->selectRaw('name AS name, name AS label, name AS value'):
Or
$query->select(DB::raw('name AS name, name AS label, name AS value'));
Or like said #Jarek Tkaczyk
$query->select('name as name', 'name as label', 'name as value');
Try this:
$query->select(DB::raw('name AS name, name AS label, name AS value'));
Link of Laravel Doc

Categories