How to save data to pivot table in laravel , I have two tables has many-to-many relationship , so i create pivot table , I tried the sync()
method to insert but it keep gives me this exception:
"message": "Call to a member function sync() on null", "exception": "Symfony\Component\Debug\Exception\FatalThrowableError",
here is Contract Model :
class Contract extends Model
{
public function skills(){
$this->belongsToMany(RequiredSkills::class , 'contract_skills')->withTimestamps();
}
}
and here is RequiredSkills Model :
class RequiredSkills extends Model
{
public function contract(){
$this->belongsToMany(Contract::class , 'contract_skills');
}
}
and here is the controller :
class RequiredSkillsController extends Controller
{
public function store(Request $request){
$contract = new Contract();
$data = $request->all();
$requiredSkills = RequiredSkills::create([
'contract_id'=>$data['contract_id'],
'skills_tag'=>$data['skills_tag'],
'user_id'=>auth()->guard('api')->user()->id
]);
$contract->skills()->sync($requiredSkills->id);
return $requiredSkills;
}
}
how to solve the exception ?
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2DJcEvE
via IFTTT
Aucun commentaire:
Enregistrer un commentaire