Reputation: 39
As I am learning laravel I am unable to understand that when I am trying to access my edit.blade.php page by typing posts/edit in URL(that file is in resource/views/posts) it is calling the method show and printing "show" on that page and if I type posts/posts/edit the edit.blade.php(mentioned below) is showing up. please guide me what I am doing wrong here
edit.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 create()
{
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('posts/read');
}
public function show($data)
{
echo "show";
}
public function edit($id)
{
return view('posts.edit');
}
public function update(Request $req, $id)
{
echo posts::where('title' , $req->title)
->update(['body'=>$req->body]);
return redirect('/');
}
public function destroy($id)
{
$post = posts::find($id);
$post->delete();
return redirect('/');
}
}
route:
Route::resource('posts', 'PostController');
Upvotes: 1
Views: 1326
Reputation: 177
First of all you change your controller like this
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Post;
use Sessions;
class PostController extends Controller
{
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('posts/read');
}
public function show($id)
{
$post = Post::find($id);
return view(posts.show, ['post' => $post]);
}
public function edit($id)
{
$post = Post::find($id);
return view(posts.edit, ['post' => $post]);
}
public function update(Request $request, $id)
{
dd($request->all()); //check before update
}
public function destroy($id)
{
$post = Post::find($id);
$post->delete();
return redirect('/');
}
}
Add $post->id
to form route
in your edit.blade.php
@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 you create show.blade.php
in view/posts
folder and put this code to show.blade.php
@extends('main')
@section('content')
<h1>Show Post</h1>
<h1>{{ $post->title }}</h1>
<p>{{ $post->body }}</p>
@endsection
Upvotes: 0
Reputation: 50561
Your routes are like so:
GET posts/{post}/edit EDIT
GET posts/{post} SHOW
So the URI posts/edit
is matching the SHOW route:
posts/edit posts/{post}
posts/edit
The URI posts/posts/edit
is matching the EDIT route:
posts/posts/edit posts/{post}/edit
posts/posts /edit
This is expected and how the routes are setup.
Laravel 7.x Docs - Controllers - Resource Conrollers
Upvotes: 1