Got a resourceful controller with a __contruct
function to declare $project
public function __construct(Project $project){
$this->project = $project;
}
Then I have an update
and a destroy
function which gives an Call to a member function delete() on a non-object
(or update()
error)
public function edit($id)
{
$project = $this->project->find($id);
return view('project.edit', ['project' => $project, 'id' => 'edit']);
}
public function update(CreateProjectRequest $request, $project)
{
$project->fill($request->input())->save();
return redirect('project/index');
}
public function destroy($project)
{
$project->delete();
return redirect('project');
}
What am I doing wrong?
$project
in this case is just the id of the project. You have to load it first:
public function destroy($projectId)
{
$project = $this->project->findOrFail($projectId);
$project->delete();
return redirect('project');
}
Or just use the destroy
method which takes the key as parameter:
public function destroy($projectId)
{
$this->project->destroy($projectId);
return redirect('project');
}
You might also want to look into Route Model Binding to fetch the model automatically from the DB.