Laravel 数组 & JSON 转换为 Algolia
Laravel Array & JSON Casting to Algolia
我正在尝试通过 toSearchableArray 将一些数据发送到 Algolia。我存储在我的数据库中的任何字符串都可以正常发送,但是我在尝试推送嵌套的 JSON 数据时遇到了障碍 — 信息以带有转义字符的字符串形式发送。
这是我存储在 table 中的嵌套对象的示例(MySQL,数据类型为 JSON):
[
{
"id": 19,
"name": "Mathematics",
"short": "Math"
},
{
"id": 23,
"name": "Science",
"short": "Science"
},
{
"id": 14,
"name": "Health and Life Skills",
"short": "Health"
}
]
我的模型是这样的:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Resource extends Model
{
use Searchable;
protected $primaryKey = 'objectID';
public function toSearchableArray()
{
$data = $this->toArray();
$data['grades'] = explode(';', $data['grades']);
$data['units'] = explode(';', $data['units']);
return $data;
}
}
我得到如下所示的输出:
array:22 [
"objectID" => 1
"name" => "Resource #1"
"slug" => "resource-1"
"write_up" => """
This is an example write up.
"""
"author" => "johnny"
"type_name" => "Lesson Plan"
"language" => "English"
"grades" => array:3 [
0 => "Kindergarten"
1 => "Grade 1"
2 => "Grade 4"
]
"subjects" => "[{"id": 19, "name": "Mathematics", "short": "Math"}, {"id": 23, "name": "Science", "short": "Science"}, {"id": 14, "name": "Health and Life Skills", "short": "Health"}]"
"units" => array:2 [
0 => "Unit A"
1 => "Unit B"
]
"main_image" => "https://dummyimage.com/250x325/000000/fff.png&text=Just+a+Test"
"loves" => 88
"downloads" => 280
"created_at" => "2018-01-01 13:26:47"
"updated_at" => "2018-01-02 10:10:32"
]
如您所见,'subjects' 属性被存储为字符串。我知道 5.5 中有属性转换(我是 运行 5.5),但我不太清楚我将如何在我上面的工作中实现数组和 JSON 转换的示例。 https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting
有人愿意给我举个例子吗?
我会依赖属性转换,在你的模型中添加一个 $casts
属性,它会自动完成。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Resource extends Model
{
use Searchable;
protected $primaryKey = 'objectID';
protected $casts = [
'subjects' => 'array',
];
public function toSearchableArray()
{
// Same function as you posted
}
}
您也可以使用 $data['subjects'] = json_decode($this->subjects, true);
在 toSearchableArray 方法中手动执行此操作
我在其他帖子中回答了更多细节:https://discourse.algolia.com/t/laravel-array-json-casting-to-algolia/4125/2
我正在尝试通过 toSearchableArray 将一些数据发送到 Algolia。我存储在我的数据库中的任何字符串都可以正常发送,但是我在尝试推送嵌套的 JSON 数据时遇到了障碍 — 信息以带有转义字符的字符串形式发送。
这是我存储在 table 中的嵌套对象的示例(MySQL,数据类型为 JSON):
[
{
"id": 19,
"name": "Mathematics",
"short": "Math"
},
{
"id": 23,
"name": "Science",
"short": "Science"
},
{
"id": 14,
"name": "Health and Life Skills",
"short": "Health"
}
]
我的模型是这样的:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Resource extends Model
{
use Searchable;
protected $primaryKey = 'objectID';
public function toSearchableArray()
{
$data = $this->toArray();
$data['grades'] = explode(';', $data['grades']);
$data['units'] = explode(';', $data['units']);
return $data;
}
}
我得到如下所示的输出:
array:22 [
"objectID" => 1
"name" => "Resource #1"
"slug" => "resource-1"
"write_up" => """
This is an example write up.
"""
"author" => "johnny"
"type_name" => "Lesson Plan"
"language" => "English"
"grades" => array:3 [
0 => "Kindergarten"
1 => "Grade 1"
2 => "Grade 4"
]
"subjects" => "[{"id": 19, "name": "Mathematics", "short": "Math"}, {"id": 23, "name": "Science", "short": "Science"}, {"id": 14, "name": "Health and Life Skills", "short": "Health"}]"
"units" => array:2 [
0 => "Unit A"
1 => "Unit B"
]
"main_image" => "https://dummyimage.com/250x325/000000/fff.png&text=Just+a+Test"
"loves" => 88
"downloads" => 280
"created_at" => "2018-01-01 13:26:47"
"updated_at" => "2018-01-02 10:10:32"
]
如您所见,'subjects' 属性被存储为字符串。我知道 5.5 中有属性转换(我是 运行 5.5),但我不太清楚我将如何在我上面的工作中实现数组和 JSON 转换的示例。 https://laravel.com/docs/5.5/eloquent-mutators#attribute-casting
有人愿意给我举个例子吗?
我会依赖属性转换,在你的模型中添加一个 $casts
属性,它会自动完成。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Resource extends Model
{
use Searchable;
protected $primaryKey = 'objectID';
protected $casts = [
'subjects' => 'array',
];
public function toSearchableArray()
{
// Same function as you posted
}
}
您也可以使用 $data['subjects'] = json_decode($this->subjects, true);
我在其他帖子中回答了更多细节:https://discourse.algolia.com/t/laravel-array-json-casting-to-algolia/4125/2