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();
}
}
check your "Follow" model, you may needs specify table name with: protected $table='tableWhereYouSaveFollows';
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";
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');
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.