我想要过滤一个json字段(post)在数据库中与自序建设者。
我需要帮助来询问顺序。
attributes: array:17 [▼
"id" => 2
"admin_id" => 0
"email" => "test@gmail.com"
"fullname" => "asdasdasd"
"subject" => "asdasdasdasd"
"desc" => """
sadasd\r\n
"""
"cell_phone" => "091200000000"
**"post" => "{"modir": 0, "saken": 0, "ozv_modir": 0}"**
"elevator_spec" => null
"type" => null
"control" => null
"count_stop" => null
"door_type" => null
"lift_capacity" => null
"insurance" => null
"created_at" => "2017-10-28 21:24:54"
"updated_at" => "2017-10-28 21:24:54"
]发布于 2017-10-29 09:29:04
You should use setter and getter in the model in case of that specific field that you want .
public function setPostAttribute($value)
{
$this->attributes['post'] = json_encode($value, JSON_UNESCAPED_UNICODE);
}
public function getPostAttribute()
{
$model = new $this;
$jsonToConvert = $this->attributes['post'];
$modelArray = $model->fromJson($jsonToConvert);
return $model->newInstance($modelArray);
}
after that in your controller :
Orders::where('post->modir', 0)->get(发布于 2017-10-30 02:25:18
Laravel为您提供了一个简单的方法,使用数组& JSON浇铸
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'post' => 'array',
];
}一旦定义了强制转换,您就可以访问options属性,并且它将自动从JSON反序列化为PHP array。当您设置options属性的值时,给定的数组将自动序列化回JSON以进行存储:
$user->post = ['modir' => 'blabla'];
$user->save();https://stackoverflow.com/questions/46998615
复制相似问题