Task.php
public function projects()
{
return $this->hasMany(Project::class);
}
Project.php
public function tasks()
{
return $this->belongsTo(Task::class);
}
My Controller
public function store(Request $request,$id)
{
$request->validate([
'member_name' => 'required',
'module' => 'required_without:file',
'file' => 'required_without:module'
]);
$project=Project::find($id);
$task = new Task();
if ($request->hasfile('file'))
{
$ext = $request->file->getClientOriginalName();
$filename=$request->module.".".$ext;
$request->file->move(public_path() . '/members/', $filename);
$task->file = $filename;
}
if($request->module)
{
$task->module=$request->module;
}
$task->member_name=$request->member_name;
$project->tasks()->save($task);
return redirect('tasks');
}
Now when i call save method it gives me the following error Call to undefined method Illuminate\Database\Eloquent\Relations\BelongsTo::save()
I cant understand why this is happen. Please help me to solve this. I will be greatefull to you
Change this
$project->tasks()->save($task);
to this
$task->save();
Additionally project has many-to-one relation with tasks so the function name should be singular as many projects could be belong to only one task
public function task()
{
return $this->belongsTo(Task::class);
}
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.