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
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- Wheredate in laravel not working
- Array to string conversion laravel blade
- Count all and get 10 records after where condition in laravel
- How to check data inserted or deleted in pivot after toggle method
- Use of undefined constant laravel
- Update if exist else insert new record in laravel
- Trying to access array offset on value of type null error in laravel
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- Rendering HTML from database table to view in Laravel
- Sample .htaccess file and index.php file under public directory in laravel
- How to validate website url in laravel using validaiton
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- How to get path from current URL in Laravel
- Laravel 9 route group with controller
- Insert Comma Separated Values in laravel
- Class 'App\Rules\Hash' not found in Laravel
- Get ids in array from users table
- Target class [HomeController] does not exist
- How to create controller in laravel
- How to check email is valid or not in Laravel
- How to insert ckeditor data into database in Laravel?
- Laravel create default admin user
- How to check duplicate entry in laravel
- How to include header file in laravel