Did you run php artisan:migrate ?
If yes, then show us the code so we know more.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i want to dispaly the name of the author in the post page but it say SQLSTATE[42S02]: Base table or view not found: what is wrong with it ?
Did you run php artisan:migrate ?
If yes, then show us the code so we know more.
ok in the post migration table here is what i am adding
$table->integer('user_id');
and in the post table create seeder
$author1= User::create([
'name'=>'habto',
'email'=>'[email protected]',
'password'=>Hash::make('password')
]);
$post3=post::create([
'title'=>'Best practices for minimalist design with example',
'description'=>'lbljdsbflzskbfk;asbfasgflbasdljfhgaslfglasjgflas',
'content'=>'ckjvblzghvljzshbvljzhbvljzxhvj,zxcvlzsjhvflsdvfulsdgvf',
'catagory_id'=>$catagory3->id,
'image'=>'posts/3.jpg',
'user_id'=>$author1->id
]);
and in the show
<p><span class="opacity-70 mr-1">By</span> <a class="text-white" href="#">{{$post->user->name}}</a></p>
@habte When do you get this error ?
When you seed the database ? or when you try to get it from the database ?
@habte Did you create Post Model and migration ?
@furqanDev when I try to get it from the database
@furqanDev yes I already have post model and i migrate it
@habte Can you show me how are you getting the values ?
@furqanDev which value what you want to see?
@habte Where you get the records from DB
You have a lower case p in when you call the Post::create method.
Since it says that it can't find the table, make sure that you have ran
php artisan migrate --seed
Also make sure that you have the correct table names in your migrations.
Show us the PostController and your Post model.
Are you using a custom users table or do you use the one that is provided with Laravel?
If you are using the one provided you need to change the datatype on your foreign key to unsignedBigInt(''user_id) or use the foreignId('user_id')
@Tray2 i have lower case p model that why i use lower case and here is the post controller
{
$image=$request->image->store('posts');
$post = post::create([
'title'=>$request->title,
'description'=>$request->description,
'content'=>$request->content,
'image'=>$request->image,
'published_at'=>$request->published_at,
'catagory_id'=>$request->catagory,
'user_id'=>auth()->user()->id
]);
if ($request->tags) {
$post->tags()->attach($request->tags);
}
//flashing the message
session()->flash('success','Post created successfully');
return redirect(route('posts.index'));
}
@Tray2 i am using the provided one .....so you are saying the 'user_id ' in the post migration table should be unsignedBigInt type ?
@furqanDev here is the records that i create in post table seeder
$author1= User::create([
'name'=>'habto',
'email'=>'[email protected]',
'password'=>Hash::make('password')
]);
$author2= User::create([
'name'=>'habtewold',
'email'=>'[email protected]',
'password'=>Hash::make('password')
]);
$post1=post::create([
'title'=>'We relocated our office to a new designed garage',
'description'=>'lbljdsbflzskbfk;asbfasgflbasdljfhgaslfglasjgflas',
'content'=>'ckjvblzghvljzshbvljzhbvljzxhvj,zxcvlzsjhvflsdvfulsdgvf',
'catagory_id'=>$catagory1->id,
'image'=>'posts/1.jpg',
'user_id'=>$author1->id
]);
$post2=post::create([
'title'=>'Top 5 brilliant content marketing strategies',
'description'=>'lbljdsbflzskbfk;asbfasgflbasdljfhgaslfglasjgflas',
'content'=>'ckjvblzghvljzshbvljzhbvljzxhvj,zxcvlzsjhvflsdvfulsdgvf',
'catagory_id'=>$catagory2->id,
'image'=>'posts/2.jpg',
'user_id'=>$author2->id
]);
$post3=post::create([
'title'=>'Best practices for minimalist design with example',
'description'=>'lbljdsbflzskbfk;asbfasgflbasdljfhgaslfglasjgflas',
'content'=>'ckjvblzghvljzshbvljzhbvljzxhvj,zxcvlzsjhvflsdvfulsdgvf',
'catagory_id'=>$catagory3->id,
'image'=>'posts/3.jpg',
'user_id'=>$author1->id
]);
$post4=post::create([
'title'=>'Congratulate and thank to Maryam for joining our team',
'description'=>'lbljdsbflzskbfk;asbfasgflbasdljfhgaslfglasjgflas',
'content'=>'ckjvblzghvljzshbvljzhbvljzxhvj,zxcvlzsjhvflsdvfulsdgvf',
'catagory_id'=>$catagory2->id,
'image'=>'posts/4.jpg',
'user_id'=>$author1->id
]);
@habte Yes it must be the same as the id in the users table.
@habte You told me that you get this error when you try to get the data from database. If the data is saving correctly then it must be something wrong with the way you fetch it from DB.
@habte Change that and follow the Laravel conventions.
All classes should start with a capital letter, the file name and the class name should match.
You should name them Post and Post.php.
Double-check while your creating that you have set your fillable properties incase its not creating the post?
protected $fillable = ['title', 'description', 'content', 'image', 'published_at', 'catagory_id','user_id'];
Might not be the problem....
@Elliot_putt ya i checked it
protected $fillable = [
'title','description','content','image','published_at','catagory_id','user_id'
];
open php artisan tinker and do $posts = App\Models\post::all();
@Elliot_putt yes here it is ``` App\post {#4261 id: 1, title: "We relocated our office to a new designed garage", description: "lbljdsbflzskbfk;asbfasgflbasdljfhgaslfglasjgflas", content: "ckjvblzghvljzshbvljzhbvljzxhvj,zxcvlzsjhvflsdvfulsdgvf", image: "posts/1.jpg", catagory_id: 1, user_id: 2, published_at: null, created_at: "2021-12-03 11:49:47", updated_at: "2021-12-03 11:49:47", deleted_at: null, }, App\post {#4262 id: 2, title: "Top 5 brilliant content marketing strategies", description: "lbljdsbflzskbfk;asbfasgflbasdljfhgaslfglasjgflas", content: "ckjvblzghvljzshbvljzhbvljzxhvj,zxcvlzsjhvflsdvfulsdgvf", image: "posts/2.jpg", catagory_id: 2, user_id: 3, published_at: null, created_at: "2021-12-03 11:49:47", updated_at: "2021-12-03 11:49:47", deleted_at: null, }, App\post {#4263 id: 3, title: "Best practices for minimalist design with example", description: "lbljdsbflzskbfk;asbfasgflbasdljfhgaslfglasjgflas", content: "ckjvblzghvljzshbvljzhbvljzxhvj,zxcvlzsjhvflsdvfulsdgvf", image: "posts/3.jpg", catagory_id: 3, user_id: 2, published_at: null, created_at: "2021-12-03 11:49:47", updated_at: "2021-12-03 11:49:47", deleted_at: null, }, App\post {#4264 id: 4, title: "Congratulate and thank to Maryam for joining our team", description: "lbljdsbflzskbfk;asbfasgflbasdljfhgaslfglasjgflas", content: "ckjvblzghvljzshbvljzhbvljzxhvj,zxcvlzsjhvflsdvfulsdgvf", image: "posts/4.jpg", catagory_id: 2, user_id: 2, published_at: null, created_at: "2021-12-03 11:49:47", updated_at: "2021-12-03 11:49:47", deleted_at: null, }, ], }
it means it works correctly in the tinker
@habte It wont find an author if there aren't these things:
a relationship has you defined this in your models?
a valid user_id which relates to I'm assuming your user-id?
in your migration make your user_id is a foreign key?
@habte Also try in tinker Get the first post instead then find it by author or however you have defined the relationship
public function user()
{
return $this->belongsToMany(User::class);
}
public function author()
{
return $this->belongsTo(User::class, "user_id");
}
It should be like this instead because you want to call it by the author
Also if its readable like so "A Post Belongs To Many Users" Which doesn't make sense if you get me ? it should be "A Post Belongs to A Single-user"
@habte Again.
@Elliot_putt tank you so much it works when i change it in to "author"!!
@habte No Problem ! I had this issue before Jefferys laravel 8 from scratch might help you out if your stuck with anything to do with posts and comments authors etc
Please or to participate in this conversation.