mercredi 19 août 2020

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=""  >
    @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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire