'Array data is not uploading in mysql database - Laravel
I have multiple records in the $validData array. I have printed the $validData record using foreach and var_dump just to show you its format. All I want to do is to store that $validData in the database but I'm not able to do that.
$validData is being populated using forloop. $sheet has valid data, don't worry about that. Below is the function inside the controller
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);
// -------------------------
Result of Customer::insert($validData):
Insert result : 1
Result of Customer::create($validData):
Insert result : {"updated_at":"2022-05-20T11:22:24.000000Z","created_at":"2022-05-20T11:22:24.000000Z","id":348}```
The customer model class looks like this
class Customer extends Model
{
use HasFactory;
public $table = 'customers';
protected $fillable = [
'id',
'name',
'email',
'contact',
'address',
'nutritionplan_id',
'company_id',
];
}
The database is also correctly connected in .env, I verified it by adding a new user.
What I have tried?
- Matched all the
$fillableattributes as to the controller attributes names - Added
'strict' => false,inmysqlarray ofdatabase.php
I'm not sure what am I missing?
The output of the above echoes
Edit
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');
}
};
Solution 1:[1]
It is your $validData correctly formatted? Here is an example to follow:
$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
Check your dump($validData).
Another thing, make sure that your migration accepts null or has default value set where your have missing values in your $validData.
OR YOU CAN TRY A DIFFERENT METHOD:
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();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

