-1

I'm trying to do follow system without any laravel libray. I got this error when I submit form. How can I fix it? I think error is about my user model and my follow model relationship but I couldn't solve.

My error is:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'muzik.follows' doesn't exist (SQL: insert into follows (following_id, follower_id, updated_at, created_at) values (12, 30, 2021-04-02 22:32:50, 2021-04-02 22:32:50))

My User model contains the following relationship:

public function follows(){
        return $this->hasMany('App\Models\Follow');
    }

My User model contains the following relationship:

public function user(){
        return $this->belongsTo('App\Models\User');
    }

My controller is:

public function follow(Request $request){
        $request->validate([
            'follower_id'=>['required'],
            'following_id'=>['required'],
        ]);

        $follower_id = $request->follower_id;
        $following_id = $request->following_id;
        

        $save = Follow::create([
            'following_id' => Auth::user()->id,
            'follower_id' => $follower_id,
        ]);

        if($save){
            return back();
        }else{
            return back();
        }
    }
vega9231w
  • 31
  • 1
  • 9
  • 1
    Does this answer your question? [Base table or view not found: 1146 Table Laravel 5](https://stackoverflow.com/questions/30159257/base-table-or-view-not-found-1146-table-laravel-5) – Martin Zeitler Apr 02 '21 at 22:51
  • yes but I'm beginner in Laravel. i don't know how can I fix this – vega9231w Apr 02 '21 at 22:51

3 Answers3

0

check your "Follow" model, you may needs specify table name with: protected $table='tableWhereYouSaveFollows';

NEOhitokiri
  • 411
  • 5
  • 9
  • i have one more relationship like this follow system in this project and it is working but this model with the relationships not working. I don't know where is the problem – vega9231w Apr 02 '21 at 23:09
0

Did you check your migration I think you did not create the Follow table if you did try to Just specify your table in the model as such:

class follows extends Model{
    public $table = "follow";
Basharmal
  • 1,313
  • 10
  • 30
0

I think your follow table does not exist, first create your follow table and then add this to your follow model:

use Illuminate\Database\Eloquent\Model;

class Follow extends Model
{
    protected $table="follows";
   
    //---Guarded
    protected $guarded = [];

    //---User Function
    public function user(){
        return $this->belongsTo('App\Models\User');
    }
}
Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38