Trying to update a table that holds a user's settings. I have a hasOne usersetting relation setup in the user model. The usersetting table doesn't have a id column. But Im getting this error.
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where
clause' (SQL: update usersettings set user_name = u9ddf,
usersettings.updated_at = 2019-04-30 02:13:37 where id is null).
public function profileupdate(Request $request)
{
if($user = Auth::user())
{
$usersetting = $user->usersetting;
$usersetting->update([
'user_name' => $request->input('profile_name')
]);
return redirect('profilesettings');
}
}
I guess Im not querying correctly with eloquent?
Add the following in model usersetting:
protected $primaryKey = 'user_id';
I figured it out. Had to set user_id as a primary key in usersetting model.
public $primaryKey = 'user_id';
Related
This question already has answers here:
Laravel Unknown Column 'updated_at'
(6 answers)
Closed 2 years ago.
I have been trying to update a document using laravel ORM but getting exception stating a unknown column updated_at.
This column is not in database table, and neither have I mentioned in the query.
$foo = ProductMatchUnmatchHistory::where('product_match_unmatches_id', $pmu->id)
->update(['is_latest'=>0]);
ProductMatchUnmatchHistory model:
protected $fillable = [
'product_match_unmatches_id',
'human_verdict',
'ai_result',
'updated_on',
'updated_by',
'is_latest'
];
The table has the same columns as in $fillable array, with additional id primary key.
Here's the exception message:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: update `product_match_unmatch_histories` set `is_latest` = 0, `updated_at` = 2019-11-20 12:20:02 where `product_match_unmatches_id` = 1759)
I have no clue from where the updated_at column is added to the query.
If you don't want to use the updated_at and created_at fields from Laravel's Eloquent models, edit your ProductMatchUnmatchHistory.php model and set $timestamps = false.
class ProductMatchUnmatchHistory extends Model
{
//....
$timestamps = false;
//....
}
Additional Info
Laravel Models automatically set a created_at field when an entry is created and update the updated_at column when you do any changes to a row. This is handled through the $timestamps field on the model. If set to true, which is default, laravel will assume that your models have created_at and updated_at fields.
In most cases this is pretty helpful. You can easily add these fields in your migration by just adding $table->timestamps();.
See https://laravel.com/docs/5.8/eloquent#eloquent-model-conventions
(under Timestamps)
please change
you must define table name
protected $table = 'your table name';
protected $fillable = [
'product_match_unmatches_id',
'human_verdict',
'ai_result',
'updated_on',
'updated_by',
'is_latest'
];
$timestamps = false;
First please check in your table updated_at exist? because sql error say column not found
#Azima you should added the column in your table otherwise you will get this error, because eloquent updating that column and its not present in table or you can $timestamps=false in your Model
Considering this tables: person and employee to which connected to person by person_id. This person_id is also the Primary & Foreign key of employee table. Thus in my migrations, i have this:
Schema::create('employees', function (Blueprint $table) {
$table->bigIncrements('person_id');
$table->foreign('person_id')->references('id')->on('persons')->onDelete('cascade');
});
and my show method is like this one
public function show(Employee $employee){
dd($employee->person_id);
$employee = Employee::where('person_id', $employee->person_id)->orderBy('employee_number', 'asc')->join('persons', 'employees.person_id', '=', 'persons.id')->first();
return view('employee.show', compact('employee'));
}
But i am experiencing this issue:
Illuminate\Database\QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `employees` where `id` = 5 limit 1)
Does the query is not aware of the column i am using?
Change the primary key in the Employee model
class Employee extends Model
{
/**
* The primary key associated with the table.
*
* #var string
*/
protected $primaryKey = 'person_id';
public function person()
{
return $this->belongsTo('App\Employee', 'person_id', 'person_id');
}
}
Then query using find
Employee::find($employee->person_id)->with('person')->first();
orderBy and first are redundant on one result queries
And if you want to return the Employee with the Person, just access the relationship object (employee is returned from route model binding)
public function show(Employee $employee) {
$person = $employee->person;
return view('employee.show', compact('employee', 'person'));
}
Hope this helps
I try update model in database.
I have request with naw values and value.
'old_log'- old value to find row in database.
public function UserChange2(Request $request){
dump($request);
$data=$request->all();
$log = $request->input('old_log');
dump($log);
$user=userModel::select(['fam','im','ot','phone','log','pass'])->where('log',$log)->first();
$temp=$request->input('fam');
$user->fam=$temp;
$temp=$request->input('im');
$user->im=$temp;
$temp=$request->input('ot');
$user->ot=$temp;
$temp=$request->input('phone');
$user->phone=$temp;
$temp=$request->input('log2');
$user->log=$temp;
$temp=$request->input('pass');
$user->pass=$temp;
dump($user);
$user->save;
}
But the records in the table are not updated.
Model:
class userModel extends Model
{
public $timestamps = false;
protected $fillable=['fam','im','ot','phone','log','pass','reg','token','date','position'];
}
Now:
dump($request);
$data=$request->all();
$log = $request->input('old_log');
dump($log);
$user=userModel::select(['fam','im','ot','phone','log','pass'])->where('log',$log)->first();
$user->fill($request->all())->save();
$user->save;
And error:
Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update user_models set fam = y, phone = gggg, pass = tttyyyyyyyy where id is null)
Your userModel extends Model, which is an Eloquent Model and by default expects that the related table has a primary key called id.
The error
Column not found: 1054 Unknown column 'id' in 'where clause'
indicates that the column id does not exist.
Check your table definition. Is this a table in a DB that was created outside your Laravel project?
If not, check your database migration whether the primary key id is defined. Read here about database migrations.
If there is already a primary key that you want to use instead of id, you can tell Eloquent:
protected $primaryKey = 'your_primary_key';
See here.
You likely don't have fillables completed in your Model.
Go to the model which should be blank and add the fields that can be completed:
example:
protected $fillable = [
'phone', 'log2', 'pass',
];
You can find this in the Laravel manual here:
https://laravel.com/docs/5.5/eloquent#mass-assignment
remove select from your eloquent. try this..
$user=userModel::where('log',$log)->first();
$user::create($request->all());
edit
$user=userModel::where('log',$log)->first();
$request->request->add(['log' => $request->log2]);
$user::create($request->all());
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();
I am developing a website using Laravel 5.2. I am working with database. So I used Laravel eloquent model to make the code neat and clean. But one of my Eloquent is throwing unknown updated_at column exception even if I already ignored it.
I ignored or override the built in function like this in my Entity.
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
//
protected $fillable = ['name', 'mm_name'];
public function setUpdatedAt($value)
{
// Do nothing.
}
public function setCreatedAt($value)
{
// Do nothing.
}
}
I deleted updated_at column from categories table. So if I update the category, updated_at column should be ignored.
So updated like this
function updateCategory($bag)
{
$category = Category::find($bag->id);
if($category)
{
$category->name = $bag->name;
$category->mm_name = (empty($bag->mm_name))?$bag->name:$bag->mm_name;
$category->save();
}
}
But it still throwing this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: update `categories` set `name` = sdsd, `mm_name` = sdsd, `updated_at` = 2016-02-20 11:36:41 where `id` = 4)
I overrode the other tables and eloquent entity like that. That is all working, but this one is not. How can I fix that?
This is the screenshot of categories table:
If you're not using the created_at or updated_at fields, you need to set the $timestamps property on your model to false. Additionally, you don't need to override the setUpdatedAt() or setCreatedAt() methods.
class Category extends Model
{
protected $fillable = ['name', 'mm_name'];
public $timestamps = false;
}
If $timestamps is true, the Eloquent query builder will add in the updated_at field whenever you update a model.
Your mutator defination is wrong. it should be setUpdatedAtAttribute
and setCreatedAtAttribute
There was no need to delete the updated_at and deleted_at attributes from the database.
You have deleted the attributes from the database so there is no need for setUpdatedAtAttribute and setCreatedAtAttribute in your model. Laravel will include them as columns in your database.
Will advice you recreate the updated_at and created_at in the categories table and if you are using mysql 5.7 ensure that your timestamps are not 00-00-00 00:00:00 that is if you are manually created them(not recommended to manually create them).