我尝试使用 post 方法将数据插入数据库,但它没有插入,而是什么都不做,并且 token 出现在我的网络浏览器的地址栏上。
这就是显示的内容:
这是我的表单:
<li class="list-group-item">
<form action="/booth_personnel/create" method="post">
{{ csrf_field() }}
<div class="input-group">
<select class="form-control" name="personnel_id">
@foreach($personnel as $person)
<option value="{{$person->id}}">{{$person->full_name}}</option>
@endforeach
</select>
<input type="hidden" value="{{$service->id}}" name="services_booth_id">
<span class="input-group-btn">
<button class="btn btn-default" type="submit" tabindex="-1"><span aria-hidden="true"></span>Add Personnel</button>
</span>
</div>
</form>
</li>
这是我的路线:
Route::post('/booth_personnel/create', 'BoothPersonnelController@store');
我的 Controller :
public function store(Request $request)
{
$this->validate($request,[
'personnel_id' => 'required',
'services_booth_id' => 'required',
]);
$result = BoothPersonnel::addNew($request);
if($result === true) {
$message = "Successfully added to database.";
return redirect()->back()->with('formSuccess', $message);
} else {
return back()->withErrors($result);
}
}
我的模型:
public static function addNew($request){
$result = array();
DB::beginTransaction();
try {
$current_time = Carbon::now('Asia/Manila');
DB::insert("INSERT INTO `tbl_booth_personnels`
(
`personnel_id`, `services_booth_id`, `created_at`
)
VALUES
(?, ?, ?)",
[
$request->input('personnel_id'),
$request->input('services_booth_id'),
$current_time
]
);
DB::commit();
$result = true;
} catch (\Exception $e) {
DB::rollback();
$result = $e->getMessage();
}
return $result;
}
注意:我正在循环,我的 View 中有一个多重表单,它位于 foreach 方法中。它只发生在第一个循环上,下一个循环插入就好了。
最佳答案
事实证明,我在循环之外还有另一个表单标签。 我删除了它,现在一切正常。
关于php - LARAVEL Post 方法在地址栏上显示 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48919496/