Updating records in model - php

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

Related

laravel orm update document [duplicate]

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

Update user settings table - Laravel

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';

Laravel 5.4 - Change the id use in login

During authentification it use the wrong id so I have this error :
SQLSTATE[42S22]: Column not found: 1054 Field 'id' unknown in where clause (SQL: select * from "acteur" where "id" = 22 limit 1
The right id is : id_biodiv_acteur. And the model for the table acteur is User.php
So I made these change :
In Authenticable, User, LoginController I did
protected $primaryKey = 'id_biodiv_acteur';
In GenericUser I did this :
public function getAuthIdentifierName()
{
return 'id_biodiv_acteur';
}
But it's not working.
If in the table of my database I put id instead of id_biodiv_acteur it work, but I want to keep the right id.
Ask me if you need to see some code.
Thanks for your help !

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

Laravel Eloquent still throwing error with updated_at column even I ignored it

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

Categories