4etrfhgvnb
4etrfhgvnb

Reputation: 39

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 App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\posts;
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

Upvotes: 0

Views: 70

Answers (2)

Sobir
Sobir

Reputation: 177

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 App\Post; 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('/');
    }

Upvotes: 0

Aless55
Aless55

Reputation: 2709

The problem is that you are returning the view: view('post.edit'). But you say that the file is called update.blade.php.

So you will have to rename the file to edit.blade.php or you need to change your edit function as follows, so that it returns the update blade file:

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

Upvotes: 1

Related Questions