Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luk's avatar
Level 1

SQLSTATE[42S02]: Base table or view not found:

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 ?

0 likes
29 replies
furqanDev's avatar

Did you run php artisan:migrate ?

If yes, then show us the code so we know more.

1 like
luk's avatar
Level 1

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>
furqanDev's avatar

@habte When do you get this error ?

When you seed the database ? or when you try to get it from the database ?

1 like
Tray2's avatar

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.

  • users
  • posts

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')

1 like
luk's avatar
Level 1

@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'));
    }
furqanDev's avatar

@habte If the records are saving correctly in the database then check everything that @tray2 mentioned and if it still doesn't work then show the code where you are fetching the records from database.

luk's avatar
Level 1

@Tray2 i am using the provided one .....so you are saying the 'user_id ' in the post migration table should be unsignedBigInt type ?

luk's avatar
Level 1

@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
            
        ]);
Tray2's avatar

@habte Yes it must be the same as the id in the users table.

furqanDev's avatar

@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.

Tray2's avatar

@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.

1 like
Elliot_putt's avatar

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....

luk's avatar
Level 1

@Elliot_putt ya i checked it

 protected $fillable = [
        'title','description','content','image','published_at','catagory_id','user_id'
    ];
1 like
Elliot_putt's avatar

open php artisan tinker and do $posts = App\Models\post::all();

luk's avatar
Level 1

@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, }, ], }

luk's avatar
Level 1

it means it works correctly in the tinker

Elliot_putt's avatar

@habte It wont find an author if there aren't these things:

  1. a relationship has you defined this in your models?

  2. a valid user_id which relates to I'm assuming your user-id?

  3. in your migration make your user_id is a foreign key?

Elliot_putt's avatar

@habte Also try in tinker Get the first post instead then find it by author or however you have defined the relationship

luk's avatar
Level 1

@Elliot_putt

  1. here is the r/n that i have created in the post model
public function user()
    {
        return $this->belongsToMany(User::class);
    }
Elliot_putt's avatar

@habte

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"

Tray2's avatar

@habte Again.

Show us the PostController and your Post model.

luk's avatar
Level 1

@Elliot_putt tank you so much it works when i change it in to "author"!!

1 like
Elliot_putt's avatar

@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.