SQL column not found although it exists - php

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

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

Updating records in model

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

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 !

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'

Delete a record in database cakephp

I have the config 'prefix' => 'hq_',
and I created a table : hq_products(pd_id, pd_price, pd_name,pd_date).
In controller, I want to delete a product. I used:
$productId = (int) $this->params['url']['id'];
$this->Product->deleteAll(array('Product.pd_id' => $productId));
and received the error :
Warning (512): SQL Error: 1054: Unknown column 'Product.id' in 'field list' [CORE\cake\libs\model\datasources\dbo_source.php, line 684]
Query:
SELECT `Product`.`id` FROM `hq_products` AS `Product` WHERE `Product`.`pd_id` = 1
I also used : $this->Product->delete($productId); same error.
Please help me.
In your Product model, you need to configure the $primaryKey model attribute:
class Product extends AppModel {
var $primaryKey = 'pd_id';
}
CakePHP expects the primary key in your hq_products table to be id, but you have named it pd_id. You need to inform CakePHP whenever you deviate from the conventions.

Categories