簡體   English   中英

mysql 數據庫中未上傳數組數據 - Laravel

[英]Array data is not uploading in mysql database - Laravel

我在$validData數組中有多個記錄。 我已經使用foreachvar_dump打印了$validData記錄,只是為了向您展示它的格式。 我只想將$validData存儲在數據庫中,但我做不到。

$validData正在使用forloop填充。 $sheet有有效數據,不用擔心。 下面是controller里面的function

public function import(Request $request)
{
foreach ($row_range as $rowID) {
            $name = $sheet->getCell('A' . $rowID)->getValue();
            $email = $sheet->getCell('B' . $rowID)->getValue();
            $contact = $sheet->getCell('C' . $rowID)->getValue();
            $address = $sheet->getCell('D' . $rowID)->getValue();
            $nutritionPlan_id = $sheet->getCell('E' . $rowID)->getValue();
            $company_id = $sheet->getCell('F' . $rowID)->getValue();
            $validData[] = [
                    'name' =>  $name,
                    'email' => $email,
                    'contact' =>  $contact,
                    'address' => $address,
                    'nutritionplan_id' => $nutritionPlan_id,
                    'company_id' => $company_id,
                ];
}

 echo "<br><br><b>Valid Data</b><br>";
 foreach ($validData as $key => $value) {
     echo ($value['name'] . "<br>" . $value['email'] . "<br><br> ");
 }
     var_dump($validData);
     // -------------------------
     $result = Customer::insert($validData); // <----- Trying to insert using this line.
     echo ("Insert result : " . $result);

 
      $result = Customer::create($validData); 
      echo ("Create result : " . $result);
     // -------------------------

Customer::insert($validData)的結果

Insert result : 1

Customer::create($validData)的結果

Insert result : {"updated_at":"2022-05-20T11:22:24.000000Z","created_at":"2022-05-20T11:22:24.000000Z","id":348}```

客戶 model class 看起來像這樣


class Customer extends Model
{
    use HasFactory;
    public $table = 'customers';
    protected $fillable = [
        'id',
        'name',
        'email',
        'contact',
        'address',
        'nutritionplan_id',
        'company_id',
    ];
}

數據庫也在.env中正確連接,我通過添加新用戶驗證了它。

我試過什么?

  • 匹配所有$fillable fillable 屬性至 controller 屬性名稱
  • database.phpmysql數組中添加了'strict' => false,

我不確定我錯過了什么?

上面回顯的output

在此處輸入圖像描述

編輯

database\migrations\2022_05_13_124803_create_customers_table.php

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email');
            $table->string('contact');
            $table->string('address');
            $table->bigInteger('nutritionPlan_id')->unsigned();
            $table->bigInteger('company_id')->unsigned();
            $table->timestamps();
        });
    }

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

你的 $validData 格式正確嗎? 這是一個要遵循的示例:

$data = [
    ['user_id'=>'Coder 1', 'subject_id'=> 4096],
    ['user_id'=>'Coder 2', 'subject_id'=> 2048],
    //...
];

Model::insert($data); // Eloquent approach
DB::table('table')->insert($data); // Query Builder approach

檢查你的轉儲($validData)。

另一件事,確保您的遷移接受 null 或在 $validData 中缺少值的地方設置默認值。

或者您可以嘗試不同的方法:

public function import(Request $request)
{
foreach ($row_range as $rowID) {
            $name = $sheet->getCell('A' . $rowID)->getValue();
            $email = $sheet->getCell('B' . $rowID)->getValue();
            $contact = $sheet->getCell('C' . $rowID)->getValue();
            $address = $sheet->getCell('D' . $rowID)->getValue();
            $nutritionPlan_id = $sheet->getCell('E' . $rowID)->getValue();
            $company_id = $sheet->getCell('F' . $rowID)->getValue();

$customer = new Customer();
$customer->name = $name;
$customer->email = $email;
$customer->contact = $contact;
$customer->address = $naddressme;
$customer->nutritionPlan_id = $nutritionPlan_id;
$customer->company_id = $company_id;
$customer->save();
           
}

暫無
暫無

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

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