php - 无法在 Form POST Laravel 应用程序上保存数据?
问题描述
我正在使用 PHP Laravel 框架。我正在尝试在提交后保存表单。奇怪的是,第一次不是储蓄,但后来,它是储蓄。在第一次发布请求时,流程甚至没有function save_application
在下面输入代码。
我的控制器:
class ApplicationController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth',['except' => ['store_application']]);
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function save_application(Request $request){
$user = Auth::user();
if(isset($request->application)){
$application = Application::where("user_id",$user->id)->first();
if($application){
$application->update($request->application);
}else{
$application = new Application;
$application = Application::create($request->application);
$application->user_id = $user->id;
$application->save();
}
}
return $this->store_application($request);
}
public function store_application(Request $request){
if(isset($request->application)){
if($request->session()->has('application')){
$request->session()->forget('application');
}
$application_data = [];
$application = new Application;
$application->attributes = $request->application;
$application_data["application"] = $application;
$request->session()->put("application" , $application_data);
}
//
return Redirect::to("/application")->withErrors(array("success"=>"Thank you for submitting the application"));
}
}
我的路由器
Route::post('/application', 'ApplicationController@save_application')->name('application');
Route::post('/application_store', 'ApplicationController@store_application')->name('application');
我的html
<form method="POST" action="/application" enctype="multipart/form-data" id='application_form'>
<input type="hidden" name="_token" value="xtQapusSjgf5XVUxjCOudedeH93a8hEqyfaNh8ChEaKt">
<input type='checkbox'>
<label>I've read and accept the terms and conditions</label>
<p class='font-size-16 font-bold text-uppercase text-black'>
Your information
</p>
<hr class='hr1'>
<div class='row form-group'>
<div class='col-lg-6'>
<label class='control-label'>First name*</label>
<input type='text' class='form-control' name='application[first_name]' value="">
</div>
<div class='col-lg-6'>
<label class='control-label' >Last name*</label>
<input type='text' class='form-control' name='application[last_name]' value="">
</div>
</div>
<div class='form-group'>
<label class='control-label' >Middle name</label>
<input type='text' class='form-control' name='application[middle_name]' value="">
</div>
<div class='form-group'>
<label class='control-label'>ID*</label>
<input type='text' class='form-control' name='application[]' value="">
</div>
<button class="btn btn-primary text-uppercase">Submit <i class='fa fa-check text-white'></i></button>
</form>
解决方案
我认为您使事情过于复杂,您可以简单地使用updateOrCreate()
它来使其更清洁。
首先,确保$fillable
或$guarded
在您的应用程序模型中使用。(应用程序.php)
protected $fillable = ['each', 'field', 'as', 'string'];
// or
protected $guarded = [];
您的方法的一些改进:
public function save_application(Request $request){
// 1. Do a proper check
$request->validate([
'application.first_name' => 'required',
'application.middle_name' => 'required',
'application.last_name' => 'required'
]);
// 2. Update or Create
$application->updateOrCreate(
[ 'user_id' => $user->id ],
$request->application // I suppose this is an array that you want
);
// 3. Handle the redirect the right way so you can eliminate the other `store_applcation()` method entirely
return redirect()->back()->with([
'application' => $application
'success' => "Your Message"
]);
}
store_application()
此外,您的控制器或其路由中不需要方法,因为您的 html 表单正在发布/application
路由。
这就是你想要的,对吧?
推荐阅读
- javascript - JS:如何使用正则表达式从 HTML 字符串中删除样式标签及其内容?
- javascript - 在 Angular 6 的弹出模式中显示来自服务器的响应消息的问题
- vb.net - 如何确定自动登录是否适用于 WebBrowser 控件?
- python - 在 Python 交互式会话中运行 help() 后继续
- jsf-2.2 - 如何在 JSF 2.2.8 中覆盖 DefaultFaceletFactory-
- laravel - Laravel Valet 共享 - 无法连接到 127.0.0.1 端口 4040:连接被拒绝
- sql - VarChar 到 Int 限制值 MS 访问
- python - 我的包的选项卡完成中提供的其他包
- vba - VBA 和 MSWord:在查找/执行例程中使用查找参数的多个值
- angular - Angular 5 - Jasmine 测试:模拟对实际 HTTP 请求的错误响应错误