我正在尝试检索属于'profile'模型的'request_item'模型中的所有记录,并且'profile'模型具有blob列。
$items = request_item::orderBy('created_at','asc')->orderBy('updated_at','asc')->take(200)->get();
foreach($items as $i):
$i->name = $i->profile->fullname;
$i->item_name = $i->inventory->item_name;
endforeach;
return response()->json([ 'success' => true, 'items' => $items ]);
您可以从上述代码中看到,我记录了200条记录,并以'created_at'和'updated_at'的升序排列,然后遍历对象并创建一个名为'name'的属性,其中包含对象'fullname ”,然后将其作为json响应(来自前端的Ajax请求)返回。这是'request_item'模型和'profile_model'
“个人资料”模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class profile extends Model
{
protected $table = "profiles";
protected $primaryKey = "username";
public $incrementing = false;
public function request_item(){
return $this->hasMany('App\request_item','username');
}
}
和'request_item'模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class request_item extends Model
{
protected $table = "request_items";
public function profile(){
return $this->belongsTo('App\profile','username');
}
}
但是它从响应中给了我这个错误
格式错误的UTF-8字符,可能编码错误
基于此错误,即使我指定只获取“全名”(我也不知道为什么'blob'列与'fullname'一起拉)]
$i->profile->fullname;
所以,有一种方法可以将Blob转换为base64,以便它可以是有效的json响应?我知道我可以做到]
base64_encode($data);
但是我只是不知道在哪里插入,哪一部分,哪一行?还是有一种方法可以通过模型完成?像当我拉它时会自动将其转换为base64吗?
PS:我在Laravel 5.2上运行
您的Blob可能包含无效字符。尝试utf8_encode($ blobfield)