Laravel3: Eloquent/Fluent fails during UPDATE statement - laravel

$account = Account::where('account_id', '=', $account_id)->first();
$account->username = 'New_Username';
$account->password = 'Password';
$account->save();
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'
Why is this happening?

When you update an Eloquent model it will use the model's primary key. The default primary key is id, you can change this by adding the following in your class:
public static $key = 'account_id';
Be warned that there are some hard-coded references to id in Laravel, so the best advise is still to use id as a primary key when designing your databases for Eloquent.
Reference: laravel/database/eloquent/model.php

Related

How to delete record with composite primary keys using Eloquent?

I'm trying to delete a record with two primary keys, using Eloquent - Laravel.
This is my model
class Like extends Model
{
//protected $primaryKey = ['crdid', 'usrid'];
public $timestamps = false;
use HasFactory;
}
Controller
try{
$dellike = Like::where('crdid', '=', $like->crdid, 'and')
->where('usrid', '=', $like->usrid)->first();
$dellike->delete();
}
catch(Exception $e){
return $e->getMessage();
}
Table definition
Schema::create('likes', function (Blueprint $table) {
$table->biginteger('crdid');
$table->biginteger('usrid');
$keys = array('crdid', 'usrid');
$table->primary($keys);
});
However, it gives me the below error;
Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: delete from `likes` where `id` is null)
I believe the 'id' is the default primary key used by Eloquent, but I'm not using it.
How should I define that, I'm not using the default primary key? or what is the correct way to delete the record with composite primary keys? Any help would be highly appreciated.
As Laravel Eloquent Docs says:
Eloquent requires each model to have at least one uniquely identifying "ID" that can serve as its primary key. "Composite" primary keys are not supported by Eloquent models. However, you are free to add additional multi-column, unique indexes to your database tables in addition to the table's uniquely identifying primary key.
So you can't use some methods of eloquent.
But seeing your code, looks like you are trying to create a model for a relationship pivot table (many to many between user and crd (what is crd?! no reason to abreviatte here.)
Try defining a many to many relationship on your user and crd model.
Also laravel naming convention for key columns is: model_name_id for a model named ModelName
But, just to delete, you can skip the first in your query:
Like::where('crdid', '=', $like->crdid, 'and')
->where('usrid', '=', $like->usrid)->delete();
This happens because when you delete a model (your attempt) laravel uses the primary key field (defaults to id) as where condition. But manually querying, you are defining the where clauses of the delete.
Just create additionnal column named id in the table as unique with auto-increment option. Eloquent will use it to delete each row with delete function.
Example with Laravel 5.5 :
$logement = Logement::findOrFail($id);
$commodites = Commodites_logement::all()->where('logement_id','=',$logement->id);
foreach ($commodites as $commodite) {
$commodite->delete();
}

custom primary key in Laravel not working

I've set a custom primary key in my Task.php model.
class Task extends Model
{
//
protected $primaryKey = 'taskn';
public $incrementing = false;
}
I've also set taskn as my primary key in the migration:
$table->string('taskn');
$table->primary('taskn');
But I still get the error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `Task` where `id` = 1 limit 1)
For some reason Laravel still tries to query id.
I am trying to retrieve the data with the following call:
$tasks = DB::table('tasks')->find($taskn);
What is wrong here?
I'm doing $tasks = DB::table('tasks')->find($taskn);
Here's your problem.
DB:: calls don't use Eloquent - you're completely bypassing it. If you do Task::find($taskn) it'll work, but DB:: calls have no idea about your $primaryKey settings.

Column not found laravel 5.4

I get the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'books.id' in
'where clause' (SQL: select * from books where books.id =
98745632564 limit 1)
when I pass id value as id. I have column name bookID in my database but in the above error it is comparing books.id = 98745632564. I could not understand where book.id is coming from.
public function showBook($id){
$book = Book::findOrFail($id);
return $book;
}
The code works perfectly fine when I pass id value with the query as follows
public function showBook($id){
$book = Book::where('bookID', $id)->find();
return $book;
}
You should set:
protected $primaryKey = 'bookID';
in your Book model to make:
$book = Book::findOrFail($id);
version work.
Methods find or findOrFail are using primary key and this is by default set to id, so if you have any custom primary key, you should set it in your Eloquent model.

Laravel 5.3 ignores $primaryKey

I have a table with primary key group_id.
I have this in the model: protected $primaryKey = 'group_id';
In my update method I have:
'group_name' => 'required|string|unique:groups,group_name,' .$id,
The query executed is looking column id in the table instead of group_id.
SQLSTATE[42S22]: Column not found: 1054 Champ 'id' inconnu dans where clause (SQL: select count(*) as aggregate from groups where group_name = Clarens Community Forum and id <> 1)
Why is that?
Found the solution with the help in laravel.io chat room.
The validation should be like this:
'group_name' => 'required|string|unique:groups,group_name,' . $group->group_id .',group_id',

Simple find command won't work. Error shows wrong foreign key

I'm trying to edit the content of a Message that already exists. So when I click:
{{ HTML::linkAction('MessageController#edit', 'edit', array($message->PK_message)) }}
My 'MessageController'
public function edit($id)
{
$message = Message::find($id);
dd($message);
// return Redirect::back();
}
I get this error message:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tblMessages.id' in 'where clause' (SQL: select * from `tblMessages` where `tblMessages`.`id` = {messages} limit 1)
What attracts my attention is that 'tblMessages.id' is not my Foreign key.
So I think there's the problem?
Can I pass a custom Foreign key or is there another problem?
When you use a different Primary Key other than id then you should explicitly declare the $primaryKey property like this:
class Message extends Model {
// This is required now
protected $primaryKey = 'PK_message';
}
By default, Laravel assumes (when not declared explicitly) id as primary key.

Resources