Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kordix's avatar

How To EDIT Posts / Items / Threads

Im scared that I can't find anywhere proper way to have possibility to EDIT posts.

EDITING is not mentioned in Laravel from Scratch series.

In forum series there is update() method

public function update($channel, Thread $thread) { $this->authorize('update', $thread);

    $thread->update(request()->validate([
        'title' => 'required',
        'body' => 'required'
    ]));

    return $thread;
}

But this does not tell me much. There is no edit() method and no edit.blade.php

I tried this tutorial http://itsolutionstuff.com/post/crud-create-read-update-delete-example-in-laravel-52-from-scratchexample.html

Here is an error when I go to edit form Trying to get property of non-object (View: C:\xampp\TODOLISTA\resources\views\tasks\edit.blade.php)

Here is my routing: Route::get('tasks/edit/{id}', 'TaskController@edit')->name('edit'); Route::patch('tasks/edit/{id}', 'TaskController@update')->name('update');

Methods in TaskController:

public function edit($id) { $task = Task::find($id); return view('tasks.edit', compact('task')); }

public function update(Request $request, $id)
{
    Task::find($id)->update([
    'title' => request('title'),
    'channel_id' => 2,
    'description' => request('description')
]);
}

edit.blade.php

{!! Form::model($task, ['method' => 'PATCH','route' => ['update', $task->id]]) !!} {{ csrf_field() }} Title Description Submit {!! Form::close() !!}

0 likes
2 replies
Corban's avatar
Corban
Best Answer
Level 6

Editing a post/task/anything is just like creating a new one but with a patch request and a update method instead of post and create.

Edit.blade.php:

<form action="/editpost/{{$post->id}}" method="post">
    {{  csrf_field() }}
    //method field is used to tell the browser that this "post" is actually a patch request
    {{ method_field('PATCH') }}
    // Here goes the edit form that can be exactly like your create post but with the values of the post you are editing
</form>

web.php:

Route::patch('/editpost/{id}', 'PostController@update');

PostController:

public function update($id){

    //validate as usual
    request()->validate([
         'title' => 'required',
         'description' => 'required'
        ]);

    //Now instead of just creating a new one we are going to update the one we want
    Post::find($id)->update([
        'title' => request('title'),
            'channel_id' => 2,
            'description' => request('description')
    ]);

    //some fancy feedback message       

    return back()
}

And yep, that's all you need. If you are able to create post you shouldn't have more problem editing them. Just remember that you need to find the post you want to update using the id, that you need to submit a patch request (or make it with a post if you want but using get+post+patch+delete let's you make everything with one single route.

The Trying to get property of non-object means that you are passing a null value either through a relationship or to a value you are using in some logic (for example a null date that you are formatting).

1 like
kordix's avatar

Damn, that's how things should be explained in documentation. I couldn't find any good answer.

I used dd() function and I realized i made stupid mistake - there was no task with id 1.

I would figure it out anyway but if I read this answer before I would save hours.

Big thanks.

Please or to participate in this conversation.