簡體   English   中英

商店插入正確的數據和空行 Laravel 5.7

[英]Store inserts correct data and an empty row Laravel 5.7

每次我創建新對象時,Eloquents 都會保存它並創建另一個空行。 什么可能導致這種情況? 我已經檢查了我的模型、控制器和遷移,但無法弄清楚。

我的模型,根據需要列出了所有字段:

 protected $fillable =
 ['first_name','last_name','birth_date','mobile_phone','educational_level','cv','location','email','recommendation];

控制器,存儲方法:

public function store(Request $request)
{
    $candidate = new Candidate;

    Candidate::create([

    'first_name' => request('first_name'),
    'last_name' => request('last_name'),            
    'email' => request('email'),
    'birth_date' => request('birth_date'),
    'mobile_phone' => request('mobile_phone'),
    'education_level' => request('education_level'),
    'cv' =>  request('cv'),
    'location' => request('location')
    'recommendation' => request('recommendation')
]);

$candidate->save();

    return view('/candidates/index', [
        'candidate' => $candidate
    ]);
}

遷移 - 似乎正在工作,因為我沒有收到任何錯誤

public function up()
{
    Schema::create('candidates', function (Blueprint $table) {
        $table->increments('id');
        $table->tinyInteger('round_number')->nullable()->default(1);
        $table->string('first_name', 50);
        $table->string('last_name', 50);
        $table->string('email', 50);
        $table->date('birth_date');
        $table->string('mobile_phone', 20);
        $table->text('personal_remarks')->nullable();
        $table->tinyInteger('educational_level')->nullable();
        $table->text('educational_remarks')->nullable();
        $table->tinyInteger('english_reading_level')->nullable();
        $table->tinyInteger('english_writing_level')->nullable();
        $table->tinyInteger('english_conversation_level')->nullable();
        $table->string('cv', 50);
        $table->tinyInteger('cv_grade')->nullable();
        $table->date('cv_grade_date')->nullable();
        $table->text('cv_comment')->nullable();
        $table->date('interview_date')->nullable();
        $table->text('phone_interview_comment')->nullable();
        $table->tinyInteger('phone_interview_grade')->nullable();
        $table->tinyInteger('in_edit')->default(0);
        $table->string('location', 20);
        $table->text('linkendin_profile')->nullable();
        $table->timestamps();

        $table->unsignedInteger('cv_graded_by')->nullable();
        $table->unsignedInteger('phone_interviewer')->nullable();

        $table->foreign('cv_graded_by')->references('id')->on('users');
        $table->foreign('phone_interviewer')->references('id')->on('users');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('candidates');
}

另外,我的觀點,以防萬一我錯過了什么:

     <div class="container">
      <div class="row">
        <div class="col-8">
          <form action="/candidates" method="POST">
              {{ csrf_field()  }}
              <br>
              <div class="form-group">
                <label for="email">Email</label>
                <input name="email" type="email" class="form-control" id="email" placeholder="email" required>
              </div>
          <div class="form-group">
            <label for="first_name">First Name</label>
            <input name="first_name" type="text" class="form-control" id="first_name" required>
          </div>

          <div class="form-group">
            <label for="last_name">Last name</label>
            <input name="last_name" type="text" class="form-control" id="last_name" placeholder="last name" required>
          </div>

           <div class="form-group">
            <label for="location">Location</label>
            <input name="location" type="text" class="form-control" id="location" placeholder="location" required>
          </div>

          <div class="form-group">
            <label for="birth_date">Birth date</label>
            <input name="birth_date" type="" class="form-control" id="birth_date" placeholder="birth date" required>
          </div>

          <div class="form-group">
            <label for="educational_level">Edu level</label>
            <input name="educational_level" type="text" class="form-control" id="educational_level" placeholder="educational_level" required>
          </div>

          <div class="form-group">
            <label for="mobile_phone">Mobile phone</label>
            <input name="mobile_phone" type="text" class="form-control" id="mobile_phone" placeholder="mobile_phone" required>
          </div>


           <div class="form-group">
            <label for="cv">CV</label>
            <input name="cv" type="text" class="form-control" id="cv" placeholder="CV" required>
          </div>

           <div class="form-group">
            <label for="recommendation">Recommendation letter here</label>
            <input name="recommendation" type="text" class="form-control" id="recommendation" placeholder="recommendation">
          </div>

        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
    </div>
  </div>
</div>

網站:

    Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::any('/candidates/index','CandidatesController@index');
Route::get('/candidates/apply','CandidatesController@create');
Route::post('/candidates/','CandidatesController@store');
Route::get('/candidates/add','CandidatesController@add');
Route::get('/candidates/edit/{id}', 'CandidatesController@edit');
Route::post('/candidates/edit/{id}', 'CandidatesController@update');
Route::get('/candidates/view/{id}','CandidatesController@view');
Route::get('/candidates/followup','CandidatesController@showFollowUp');
Route::delete('/candidates/delete/{id}', 'CandidatesController@destroy');
Route::get('/excel/export', 'CandidatesController@excelExport');
Route::get('/session/prepare/excel', 'CandidatesController@sessionPrepareExcel');
Route::any('/candidates/prepemail','CandidatesController@prepEmail');
Route::post('/candidates/prepemail','CandidatesController@sendEmail');

這是因為您明確保存了兩個候選對象

$candidate = new Candidate;
$candidate->save();

Candidate::create([]);

如果您不知道,這個 create 方法會將數據保存到數據庫中。

作為最終答案,您的商店功能應該像這樣簡單。

public function store(Request $request)
{
    $candidate = Candidate::create([
        'first_name' => request('first_name'),
        'last_name' => request('last_name'),            
        'email' => request('email'),
        'birth_date' => request('birth_date'),
        'mobile_phone' => request('mobile_phone'),
        'education_level' => request('education_level'),
        'cv' =>  request('cv'),
        'location' => request('location')
        'recommendation' => request('recommendation')
    ]);

    return view('/candidates/index', [
        'candidate' => $candidate
    ]);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM