
Call to undefined relationship [user] on model [App\Models\Post]
Call to undefined relationship [user] on model [AppModelsPost]
Good day, guys. In this post, we’ll look at how to solve the "Call to undefined relationship [user] on model [AppModelsPost]" programming puzzle.
Sometimes we got error Call to undefined relationship [user] on model [App\Models\Post] while calling relationship method using with() on model without defining the method in model class.-
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
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Define variable and use in Laravel controller method
- How to insert multiple rows in mysql using loop in laravel?
- Laravel insert query not working
- Trying to get property 'title' of non-object
- Wheredate in laravel not working
- Calculate age from date of birth in Laravel
- Run artisan command to generate key in laravel
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- Show old value while editing the form in Laravel
- How to get date from created_at field in laravel
- How to create pivot table in laravel using migration
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- Return redirect laravel not working
- How to add is_activated column in database using laravel migration
- How to add active class to menu item in laravel
- How to get last month records in Laravel
- Skip first n record and display rest records in laravel view
- Get count of filter data, while return a small set of records
- How to add class to tr in table using foreach in laravel
- Laravel delete all rows older than 30 days
- Cannot end a section without first starting one
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- Update existing pivot table data in laravel
- Get all users except the followings users in overtrue laravel-follow