Skip to content
Advertisement

Resource Controller Concept

I am unable to find the issue. It is showing 404|Not Found

update.blade.php

@extends('main')
@section('content')
<h1>Update Post</h1>
<form method="POST" action="{{route('posts.update', $post) }}"  >
    @method('PUT')
    @csrf
    <input type="text" name="title"><br><br>
    <input type="text" name="body"><br><br>
    <button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection

PostController.php (a resource controller)

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;
use Appposts;
use Sessions; 

class PostController extends Controller
{
    public function index()
    {
        $post = posts::all();

        return view('post.index', compact('post');      
    }
    public function create(Request $req)
    {
        return view('posts.create');
    }
    public function store(Request $request)
    {
        $post = new posts;
        $post->title = $request->input('title');
        $post->body = $request->input('body');
        $post->save();
        return redirect('/');
    }

    public function show($data)
    {
        $post = posts::findOrFail($data);
        return view('posts.read', compact('post','$post'));
    }

    public function edit(posts $post)
    {
        return view('posts.edit', compact('post'));
    }

 
    public function update(Request $request, $id)
    {
        $request->validate([
            'title'=>'required',
            'body'=>'required'
        ]);
        $post = posts::find($id);
        $post->title =  $request->get('title');
        $post->body = $request->get('body');
        $post->save();

        return redirect('/');
    }

   
}

route:

Route::resource('posts', 'PostController');

please tell me what is the issue in this. one of the advice I got is to change the name of view file i.e update.blade.php to edit.blade.php. I don’t know how does it help

Advertisement

Answer

First you should change edit.blade.php instead of update.blade.php

Second you should not call model like this use App/posts; It is wrong. It must be use AppPost; in your PostController

Third you should change edit() in your controller

public function edit($id)
    {
        $post = Post::find($id);
        return view('posts.edit', compact('post'));
    }

You should use $post->id instead of $postin your form action

@extends('main')
@section('content')
<h1>Update Post</h1>
<form method="POST" action="{{route('posts.update', $post->id) }}"  >
    @method('PUT')
    @csrf
    <input type="text" name="title"><br><br>
    <input type="text" name="body"><br><br>
    <button type="submit" class="btn btn-primary">Update</button>
</form>
@endsection

Then check

public function update(Request $request, $id)
    {
        dd($id);//check id before update

        $request->validate([
            'title'=>'required',
            'body'=>'required'
        ]);
        $post = posts::find($id);
        $post->title =  $request->get('title');
        $post->body = $request->get('body');
        $post->save();

        return redirect('/');
    }
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement