我正在尝试使用Laravel Framework使用Laravel的集体形式来更新帖子。但是,单击更新按钮后,出现错误“ MethodNotAllovedHttpEkception没有消息”。另外,当我单击子显示按钮以更新帖子时,URL上会出现问号符号,例如:Localhost:8000/edit?5
。有人知道这里出什么问题吗?
错误图片
表格示例
mainController
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Http\Request;
use App\Post;
//if we want to use sql syntax for queries
use DB;
use Image;
class MainController extends Controller
{
function index()
{
return view('login');
}
function checklogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'password' => 'required|alphaNum|min:3'
]);
$user_data = array(
'email' => $request->get('email'),
'password' => $request->get('password')
);
if(Auth::attempt($user_data))
{
return redirect('');
}
else
{
return back()->with('error', 'Wrong Login Details');
}
}
function successlogin()
{
$posts= Post::orderby('created_at', 'desc')->paginate(3);
return view('main_page')-> with('posts', $posts);
}
function logout()
{
Auth::logout();
return redirect('');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this -> validate($request,[
'title' => 'required',
'content' => 'required'
]);
// Handle File Upload
if($request->hasFile('image')){
// Get filename with the extension
$filenameWithExt = $request->file('image')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('image')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('image')->storeAs('public/upload', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
//create new post
$post= new Post;
$post -> title = $request -> input('title');
$post -> content = $request -> input('content');
$post->file_name = $fileNameToStore;
$post -> save();
// Check if file is present
if( $request->hasFile('post_thumbnail') ) {
$post_thumbnail = $request->file('post_thumbnail');
$filename = time() . '.' . $post_thumbnail->getClientOriginalExtension();
Image::make($post_thumbnail)->resize(329.33, 199.33)->save( public_path('uploads/' . $filename ) );
// Set post-thumbnail url
$post->post_thumbnail = $filename;
}
$post->save();
return redirect('/') ->with('success', 'Post Created');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//list post while we enter through link
$post= Post::find($id);
return view('show')-> with('post', $post);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$post= Post::find($id);
return view('edit')-> with('post', $post);
}
public function update(Request $request)
{
$this -> validate($request,[
'title' => 'required',
'content' => 'required'
]);
//create new post
$post= Post::find($id);
$post -> title = $request -> input('title');
$post -> content = $request -> input('content');
$post -> save();
// Check if file is present
if( $request->hasFile('post_thumbnail') ) {
$post_thumbnail = $request->file('post_thumbnail');
$filename = time() . '.' . $post_thumbnail->getClientOriginalExtension();
Image::make($post_thumbnail)->resize(329.33, 199.33)->save( public_path('/uploads/' . $filename ) );
// Set post-thumbnail url
$post->post_thumbnail = $filename;
}
return redirect('/') ->with('success', 'Post Updated');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post= Post::find($id);
$post -> delete();
return redirect('/') ->with('success', 'Post Removed');
}
}
?>
edit.blade.php
@extends('layout')
@section('content')
<div class="container2">
<div class="container">
<div class="card card-container">
<!-- <img class="profile-img-card" src="//lh3.googleusercontent.com/-6V8xOA6M7BA/AAAAAAAAAAI/AAAAAAAAAAA/rzlHcD0KYwo/photo.jpg?sz=120" alt="" /> -->
<h1 style="text-align: center;">Napiši Vijest</h1>
{!! Form::open(['action' => ['MainController@update', $post -> id], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('title', 'Title')}}
{{Form::text('title', $post-> title, ['class' => 'form-control', 'placeholder' => 'title'])}}
</div>
<div class="form-group">
{{Form::label('content', 'Content')}}
{{Form::textarea('content', $post-> content, ['class' => 'form-control', 'placeholder' => 'content', 'rows' => 5, 'cols' => 5])}}
</div>
<div class="form-group">
{{ 'Select the file to upload.' }}
{{ Form::file('image') }}
</div>
<div class="form-group">
{{Form::label('post_thumbnail', 'Dodaj Sliku')}}
{{Form::file('post_thumbnail')}}
</div>
<!--Hidden form is used because we want to develope route for edit form whcih is PUT-->
{{Form::hidden('_method', 'PUT') }}
{{Form::submit('Prihvati', ['class' => 'btn btn-primary']) }}
<a href="{{ route('main') }}" class="btn btn-success" style="margin-right: 3px;">Nazad</a>
{!! Form::close() !!}
</div><!-- /card-container -->
</div><!-- /container -->
</div><!-- /container -->
@endsection
路线
<?php
Route::resource('path', 'MainController');
//login routes
Route::get('/', 'MainController@successlogin')->name('main');
Route::get('/admin', 'MainController@index');
Route::post('/main/checklogin', 'MainController@checklogin');
Route::get('main/logout', 'MainController@logout');
//posts routes
Route::get('/createPost', 'MainController@create')->name('post.create');
Route::post('/createPost', 'MainController@store')->name('post.create');
Route::get('/edit/{id}', 'MainController@edit')->name('post.edit');
Route::post('/edit/', 'MainController@update')->name('post.update');
Route::get('/delete/{id}', 'MainController@destroy')->name('post.delete');
Route::get('/posts/{id}', 'MainController@show')->name('post.shpw');
答案 0 :(得分:-1)
我没有使用Laravel Form Collective,但是在您的刀片中,我发现了几个错误:
您的路线也有错误。更新功能需要$id
才能找到特定的帖子($id
)。
因此,如下更改该路线:
Route::post('/edit/{$id}', 'MainController@update')->name('post.update');
如果修复后仍存在相同问题,请告知:)