
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
- How to get column names from table in Laravel
- How to upload image in laravel 8
- Rendering HTML from database table to view in Laravel
- How to pass two variables in HREF in laravel
- How to insert multiple rows in mysql using loop in laravel?
- Session Doesn't Work on Redirect
- Call to a member function update() on null
- The use statement with non-compound name 'Auth' has no effect
- How to create belongstomany relation using custom name on custom pivot table
- Extract only time from datetime in laravel
- Cannot end a section without first starting one
- Ignore Records where a field has NULL value in Laravel
- Rename Pivot Table in Laravel
- Display data in table using foreach in Laravel
- How to prevent host header attack in Laravel
- Order by multiple columns in Laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Remove public from url in laravel project
- How to get all route list
- Delete all related comments on deleting a post in Laravel
- Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null, object given
- Laravel append URI in route
- Route not defined in Laravel
- Get only 10 records from table in laravel
- 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