我正在尝试为我的网站制作评论系统,但每当我提交我的数据时,我都会收到此错误 RouteCollection.php第218行中的MethodNotAllowedHttpException:
这是我在web.php中的代码
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index');
Route::resource('doomxos','DoomxosController');
这是我模型中的代码: -
namespace App;
use Illuminate\Database\Eloquent\Model;
class Doomxo extends Model
{
//
public function userd(){
return $this->belongsTo('App\User');
}
}
这是我的刀片文件中的代码
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"> </script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<div class="row new-post">
<div class="col-md-6 col-md-offset-3">
<header><h3>comments</h3></header>
<form action="route('doomxos.store')" method="post">
{{csrf_field()}}
<div class="form-group">
<textarea class="form-control" name="body" id="new-post" rows="5" placeholder="Your comment "></textarea>
</div>
<button type="submit" class="btn btn-primary">Post comment</button>
</form>
</div>
</div>
@foreach($doomxos as $doomxo)
<link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css">
{{$doomxo->created_at}}
{{$doomxo->body }}
@endforeach
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
</body>
</html>
这是我在控制器中的代码: -
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Doomxo;
use Auth;
use App\User;
class DoomxosController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
$doomxos = doomxo::all();
return view('doomxos.create', ['doomxos' => $doomxos]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$doomxo= new Doomxo();
$doomxo->user_id=Auth::userd()->id;
$doomxo->body=$request->body;
$request->userd()->doomxos()->save($doomxo);
return back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
$doomxos=doomxo::all();
return view('doomxos',['doomxos'=>$doomxos]);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
这是我的user.php中的圆锥
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function comments() {
return $this->hasMany('App\Comment');
}
public function doomxos() {
return $this->hasMany('App\Doomxo');
}
}
提前致谢: - )
答案 0 :(得分:2)
您的表单操作不正确,您有:
<form action="route('doomxos.store')" method="post">
您需要使用花括号,因为这是一个需要运行的函数:
<form action="{{ route('doomxos.store') }}" method="post">