
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
Error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause found in laravel because you did not define the right relationship method in model.
-
Get user/author detail using belongsTo relation on post model
//app\Models\Post.php class Post extends Model { //Add user method in existing post model public function user(){ return $this->belongsTo('App\Models\User'); } } //Code snippet to get the user/author details with post. $data = Post::with('user')->first(); dd($data); //Import post model after the namespace in controller or anywhere you want to use the above code use App\Models\Post;
You have to create a user method with belongsTo relationship in the post model. It helps you to get the user or author information using the user_id (foreign_id) of the posts table and id (primary key) column of users table.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Input file with max size validation in laravel
- Get comma separated email from input array
- How to pass data to partial view file in laravel
- How to check duplicate entry in laravel
- Laravel 7 login error message not showing
- Get domain name in laravel
- Json encode method in laravel
- How to restore multiple records after soft-deletes in Laravel
- Post model with title and body in laravel 8
- How to validate form input data in laravel
- How to add active class to menu item in laravel
- Laravel insert query not working
- Laravel order by date not working
- How to create static page in Laravel
- How to get all route list
- How to remove P tag from CkEditor in Laravel?
- Update existing pivot table data in laravel
- Delete file from amazon s3 bucket using Laravel
- Multiple Level eager loading in Laravel
- How to restore deleted records in laravel
- Page loader in laravel
- On delete set foreign id column value null using migration in laravel 8
- How to get CSRF token in laravel controller
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- How to validate URL with https using regex in laravel