首页 > 解决方案 > 无法在 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'>&nbsp;
       <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>

标签: phplaravel

解决方案


我认为您使事情过于复杂,您可以简单地使用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路由。

这就是你想要的,对吧?


推荐阅读