0

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?

2
  • why are you doing this return $project; in constructor? Commented May 15, 2015 at 8:21
  • I actually just noticed I had a return $project there, I deleted it now but it doesn't fix the problem Commented May 15, 2015 at 8:22

1 Answer 1

4

$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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.