将JSON类型数据输入到Laravel 5.2

时间:2021-08-14 14:22:14

I have file txt (data.txt) with this value :

我有这个值的文件txt(data.txt):

{"id":"1","title":"first Testing","completed":"Yes"}

{"id":"2","title":"Second Testing","completed":"no"}

{"id":"3","title":"Third Testing","completed":"no"}

and table Data with field id, title, and completed..

和表数据与字段ID,标题和完成..

how I can input all value in data.txt into my Table (Data) with laravel 5.2??

我如何使用laravel 5.2将data.txt中的所有值输入到我的表(数据)中?

Thanks before.. and sorry for my bad english..

先谢谢..抱歉我的英文不好..

3 个解决方案

#1


2  

Read file and convert it to PHP Array

读取文件并将其转换为PHP数组

 $arrays = json_decode(file_get_contents('data.txt'), true);

Then with simple foreach

然后用简单的foreach

foreach($arrays as $array) {
   $myObject = new Model();
   $myObject->id = $array['id'];
   $myObject->title = $array['title'];
   $myObject->completed = $array['completed'];
   $myObject->save();
}

Or you could do it straightfoward if you defined $fillable attributes

或者如果定义了$ fillable属性,你可以直接做到

foreach($arrays as $array)
   Model::create($array);

Eloquent: Mass Assignment

雄辩:群众作业

Using Query Builder

使用查询生成器

DB::table('data')->insert($arrays);

Query Builder: Insert

查询生成器:插入

#2


0  

Try these codes

试试这些代码

in laravel you could use filesystem to get the file. but here is the basic version. Hope this will help.

在laravel中,您可以使用文件系统来获取文件。但这是基本版本。希望这会有所帮助。

$file = fopen("data.txt","r");

while(! feof($file))
  {
    $data = json_decode(fgets($file), true);

    //write your code to Store your data

  }

fclose($file);

#3


0  

Seeing you data.txt example it seems like the value is not well formatted JSON. To handle this kind of value first you need to explode your data with \n as delimeter. Then filter it to get rid all empty elements, after that map each item using json_decode, this should be worked:

看到你data.txt的例子看起来似乎没有格式化JSON的值。要首先处理此类值,您需要使用\ n作为分隔符来分解数据。然后过滤它以摆脱所有空元素,在使用json_decode映射每个项目之后,这应该是有效的:

$data = file_get_contents('path/to/data.txt');
$data = array_filter(explode("\n", $data));
$data = array_map(function ($item) {
    return json_decode($item, true);
}, $data);

// Insert to your database table
DB::table('mytable')->insert($data);

If you data.txt contains well formatted JSON then you can skip the exploding, filtering and mapping steps:

如果data.txt包含格式良好的JSON,那么您可以跳过爆炸,过滤和映射步骤:

$data = file_get_contents('path/to/data.txt');

// Insert to your database table
DB::table('mytable')->insert($data);

If you are about to do it more than once, it's recommended to use eloquent model updateOrCreate() method to prevent duplicated rows. Here is example:

如果您不止一次这样做,建议使用eloquent model updateOrCreate()方法来防止重复行。这是一个例子:

// Uses $data value from the first example
foreach($data as $row) {
    MyModel::updateOrCreate(['id' => $row['id'], $row);
}

Just an addition in case you want to go with Laravel's way (Filesystem). Put your data.txt in storage/app folder then use Storage facade to retrieve the data.

如果您想要使用Laravel的方式(Filesystem),那么这只是一个补充。将data.txt放入storage / app文件夹,然后使用Storage facade检索数据。

$data = Storage::get('data.txt');
$data = array_filter(explode("\n", $data));
$data = array_map(function ($item) {
    return json_decode($item, true);
}, $data);

DB::table('mytable')->insert($data);

// Or

foreach($data as $row) {
    MyModel::updateOrCreate(['id' => $row['id'], $row);
}

#1


2  

Read file and convert it to PHP Array

读取文件并将其转换为PHP数组

 $arrays = json_decode(file_get_contents('data.txt'), true);

Then with simple foreach

然后用简单的foreach

foreach($arrays as $array) {
   $myObject = new Model();
   $myObject->id = $array['id'];
   $myObject->title = $array['title'];
   $myObject->completed = $array['completed'];
   $myObject->save();
}

Or you could do it straightfoward if you defined $fillable attributes

或者如果定义了$ fillable属性,你可以直接做到

foreach($arrays as $array)
   Model::create($array);

Eloquent: Mass Assignment

雄辩:群众作业

Using Query Builder

使用查询生成器

DB::table('data')->insert($arrays);

Query Builder: Insert

查询生成器:插入

#2


0  

Try these codes

试试这些代码

in laravel you could use filesystem to get the file. but here is the basic version. Hope this will help.

在laravel中,您可以使用文件系统来获取文件。但这是基本版本。希望这会有所帮助。

$file = fopen("data.txt","r");

while(! feof($file))
  {
    $data = json_decode(fgets($file), true);

    //write your code to Store your data

  }

fclose($file);

#3


0  

Seeing you data.txt example it seems like the value is not well formatted JSON. To handle this kind of value first you need to explode your data with \n as delimeter. Then filter it to get rid all empty elements, after that map each item using json_decode, this should be worked:

看到你data.txt的例子看起来似乎没有格式化JSON的值。要首先处理此类值,您需要使用\ n作为分隔符来分解数据。然后过滤它以摆脱所有空元素,在使用json_decode映射每个项目之后,这应该是有效的:

$data = file_get_contents('path/to/data.txt');
$data = array_filter(explode("\n", $data));
$data = array_map(function ($item) {
    return json_decode($item, true);
}, $data);

// Insert to your database table
DB::table('mytable')->insert($data);

If you data.txt contains well formatted JSON then you can skip the exploding, filtering and mapping steps:

如果data.txt包含格式良好的JSON,那么您可以跳过爆炸,过滤和映射步骤:

$data = file_get_contents('path/to/data.txt');

// Insert to your database table
DB::table('mytable')->insert($data);

If you are about to do it more than once, it's recommended to use eloquent model updateOrCreate() method to prevent duplicated rows. Here is example:

如果您不止一次这样做,建议使用eloquent model updateOrCreate()方法来防止重复行。这是一个例子:

// Uses $data value from the first example
foreach($data as $row) {
    MyModel::updateOrCreate(['id' => $row['id'], $row);
}

Just an addition in case you want to go with Laravel's way (Filesystem). Put your data.txt in storage/app folder then use Storage facade to retrieve the data.

如果您想要使用Laravel的方式(Filesystem),那么这只是一个补充。将data.txt放入storage / app文件夹,然后使用Storage facade检索数据。

$data = Storage::get('data.txt');
$data = array_filter(explode("\n", $data));
$data = array_map(function ($item) {
    return json_decode($item, true);
}, $data);

DB::table('mytable')->insert($data);

// Or

foreach($data as $row) {
    MyModel::updateOrCreate(['id' => $row['id'], $row);
}