Laravel 5.2 exists validation cannot find column - laravel-5

I have wierd trouble during validating request in controller. I use exists validation rule. User is supposed to submit integer. This integer should exist as id in a single row of user_courses table. This row also should have column user_id equal to 158 - docs.
My validation rule:
'course_id' => 'integer|exists: user_courses, id, user_id, 158'
What I got instead:
PDOException in Connection.php line 333: SQLSTATE[42S22]: Column not
found: 1054 Unknown column ' id' in 'where clause'
My Table:
id, user_id, created_at, updated_at

Problem was in the spaces:
'course_id' => 'integer|exists: user_courses, id, user_id, 158' // incorrect
'course_id' => 'integer|exists:user_courses,id,user_id,158' // correct
Original code would confuse laravel to search for column names like this:
' user_courses'

Related

Laravel where in a relational database

I have two tables
Thread and Participants
Thread has many participants and I'm able to access it like so:
Thread::with('participants')->get();
The participants table has a column last_read
I would like to get all participants from thread WHERE last_read is NULL. I've tried this but it returns an error.
Thread::with('participants')
->where('last_read', NULL)
->get();
I am getting this error:
ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'last_read' in 'where clause' (SQL: select * from threads where last_read is null and threads.deleted_at is null)
Any idea how I can do this?
Since you want a query on relation table use whereHas
Thread::with('participants')
->whereHas('participants', function($q){
$q->where('last_read', NULL);
})->get();

Laravel Find Not working with custom primary key

I am stuck with a situation where i coudn't update my records using Eloquent in Laravel 5.5 using custom primary key.
Here is what i tried :-
In my Model.php i have,
protected $primarykey = 'custom_primary_key';
my migrations looks like :-
$table->increments('custom_primary_key');
in my controller,i have :-
Model::find(custom_primary_key);
but when i tries to find a record using find() in my controller, it gives
Column not found: 1054 Unknown column 'table.id' in 'where clause' (SQL: select * from `table` where `table`.`id` = 1 and `table`.`deleted_at` is null limit 1)"
table also contains a column with custom primary key and there is no column named id.
It doesnt recognize the cutom primary key. Where can i be missing something ?
Set $primarykey into $primaryKey. Check the documentation from Laravel here:
https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions

sql 42S22 error in Laravel (choosing a column I didn't mention)

I am doing a basic project in Laravel, when trying to delete an entry, it generates this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `employees` where `id` = 6 limit 1)
and it is true I don't have a column named 'id', instead I have employee_id, but why is it choosing id instead of employee_id?
Please explain from where did it bring this id column?
In your Employee model (Employee.php), add
protected $primaryKey = 'employee_id';
This will tell Laravel to use employee_id as the primary key for Empolyee objects.

Use JWT with uuid lumen

I am using jwt in luman and use primary key UUID hexadecimal.
And firstly i got issues to Authenticate token by passing in header the issue was -
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.id' in 'where clause' (SQL: select * from user` where users.id = 0 limit 1) "
but i solved it using " protected $primaryKey = 'uuid'; " put in model it fine.
But after i got another issue in 'jwt->attemp' it always give my UUID '30' in numeric number when i login but when i remove the code form model it's working fine. Please help me about this issues.
In your config/jwt.php change:
'identifier' => 'id'
To
'identifier' => 'uuid'

How to resolve "Unknown column 'description' in 'field list'" in CodeIgniter?

I am trying to add single field from many fields to a table:
Error Number: 1054
Unknown column 'description' in 'field list'
Code:
INSERT INTO `eys_questions` (`question`, `description`, `answer1`,
`checkbox1`, `answer2`, `answer3`, `answer4`, `answer5`, `answer6`,
`answer7`, `answer8`, `answer9`, `answer10`, `image`)
VALUES ('This is the question ', 'This is the description', 'This is the answer', '1',
'', '', '', '', '', '', '', '', '', '')
Filename: C:/xampp/htdocs/eys/system/database/DB_driver.php
Line Number: 691
It looks like your db table don't have any column with name description. Check out the spelling of the column name description.
You need to verify the columns on the eys_questions table. The error is pretty clear: that table doesn't have a description column. You can check the columns on a table with the desc (describe) sql command.
In your case desc eys_questions. This will show you table information such as column names and types.
Once you've verified column names, you can modify your insert statement accordingly.

Resources