Reputation: 101
How can I save the following array values in my sql table?
Array (
[0] => "/path/to/file/file_name1.jpeg"
[1] => "file_name1.jpeg"
)
Array (
[0] => "/path/to/file/file_name2.jpeg"
[1] => "file_name2.jpeg"
)
Following is my table structure,
id - int - auto inc
path - varchar(200)
name - varchar(50)
Thanks in Advance.
Upvotes: 1
Views: 103
Reputation: 752
You can add multiple values with the following
$data = array(
array('name'=>'value', 'path'=>'value'),
array('name'=>'value', 'path'=>'2048'),
//...
);
Model::insert($data);
You can find more here https://laravel.com/docs/5.3/queries#inserts
Upvotes: 0
Reputation: 17658
You can use foreach
and create
method as:
foreach ($array as $key => $value) {
ModelName::create(['path' => $value[0], 'name' => $value[0]]);
}
Remember to add path
and name
in your model's fillable attribute.
Upvotes: 0
Reputation: 163768
You can insert an array of data with insert()
method:
$data = [
['path' => '/path/to/file/file_name1.jpeg', 'name' => 'File 1'],
['path' => '/path/to/file/file_name2.jpeg', 'name' => 'File 2'],
['path' => '/path/to/file/file_name3.jpeg', 'name' => 'File 3'],
];
Model::insert($data);
Or:
....
DB::table('some_table')->insert($data);
Upvotes: 2