Laravel 5.3 ignores $primaryKey - laravel-5

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

Related

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.

How to update field in table Laravel or create?

I try to update field rate in table by primary key user_id.
So, I need to increment of decrement rate field. If row is not exist I need to create it.
I tried:
$this->point = -1; // Or $this->point = 1;
Rating::updateOrCreate(["user_id", $id], ["rate" => $this->point]);
But it does not work:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `rating` where (`0` = user_id and `1` = 5) limit 1)
This is because your passing the column and the value as two separate values.
Change:
["user_id", $id]
to:
["user_id" => $id]
Hope this helps!
Laravel (eloquent) provides a fluent way to update related models.
Assuming the following model structure:
Rating.php
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function ratings()
{
return $this->hasMany('App\Rating');
}
You can easily update the relation via associate()
$user = User::findOrFail($userId);
$rating = Rating::findOrFail($ratingId);
$rating->user()->associate($user)->save();
Read more about Relationships in Eloquent

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.

Laravel3: Eloquent/Fluent fails during UPDATE statement

$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

Resources