【发布时间】:2018-10-08 05:55:24
【问题描述】:
我正在尝试将表单发布到数据库,但出现以下错误
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 随消息抛出
表单预先填充了数据并以发布路线为目标。通过研究,我发现问题出在发布到获取路线上,但我定位的路线是帖子。
表格
<form action="/account/tenancy/{{$user->id}/" method="POST">
{{ csrf_field() }}
<div class="row">
<div class="col-md-6">
<label for="property_address">Property Address</label>
</div> <!-- ./col6 -->
</div> <!-- ./ row-6 -->
<div class="row">
<div class="col-md-10">
<select class="form-control" id="property_address" name="property_address">
<!--Gets all counties from DB -->
@foreach ($properties as $property)
<option value={{$property->id}}>{{$property->address . ', ' . $property->town . ', ' . $property->county}}</option>
@endforeach
</select>
</div> <!-- ./ col-6-->
</div> <!-- ./ row-5 -->
<div class="row mt-2">
<div class="col-md-6">
<label for="landlord-name">Landlord Name</label>
</div> <!-- ./col=6 -->
</div> <!-- ./ row-4-->
<div class="row">
<div class="col-md-6">
<select class="form-control" name="landlord-name">
<option value="{{Auth::user()->name}}">{{Auth::user()->name}}</option>
</select>
</div> <!-- ./ row 3-->
</div> <!-- ./col-3 -->
<div class="row mt-2">
<div class="col-md-6">
<label for="tenand-name">Tenant Name</label>
</div> <!-- ./col=6 -->
</div> <!-- ./ row-4-->
<div class="row">
<div class="col-md-6">
<select class="form-control" name="tenant-name">
<option value="{{$user->name}}">{{$user->name}}</option>
</select>
</div> <!-- ./ row 3-->
</div> <!-- ./col-3 -->
<button class="mt-2 btn btn-primary" type="submit">Create Tenancy</button>
</form> <!-- ./form -->
控制器方法
//Renders Form
public function create($id){
$user = User::where('id', $id)->first();
$properties = PropertyAdvert::where('user_id', Auth::id())->get();
return view('/pages/account/tenancy/create', compact('user', 'properties'));
}
//Stores data
public function store(Request $request){
$Tenancy = Tenancy::create([
'tenant_id' => $request->user_id,
'landlord_id' => Auth::id(),
'property_address' => $request->property_address
]);
return back();
}
租赁模式
class Tenancy extends Model
{
protected $fillable = ['tenant_id', 'landlord_id', 'property_address', 'accepted'];
public function user(){
return $this->belongsTo('App\User');
}
}
路线
【问题讨论】:
-
这个错误通常意味着你的路由定义有问题。你能说明如何在
web.php中声明你的路线吗? -
问题可能出在路由上。请粘贴您的路线定义并粘贴您从
php artisan route:list获得的输出。 -
添加为图片
-
在表单操作中将 {id} 替换为 {{id}}。希望能解决问题。
-
不,那是行不通的。嗯嗯
标签: laravel laravel-5 eloquent laravel-5.6