doudi8525 2019-08-02 13:55
浏览 77

Laravel,当使用Eloquent检索数据分页和获取方法时返回不同的输出

I am having a hard time with Laravel. I have a query that returns all the table columns. The problem is that a field "foto" returns as an empty string in the JSON when I use the "get" or "find" methods. Paginate it works as expected.

This query:

$resultado = $query
           ->where('id_admin', '=', $id_admin)
           ->with('telasPermissao.itemPermissao')
           ->paginate(50);

foto comes as:

"foto": "data/image:93483940349"

Now, with this:

$resultado = $query
           ->where('id_admin', '=', $id_admin)
           ->with('telasPermissao.itemPermissao')
           ->find(1);



$resultado = $query
           ->where('id_admin', '=', $id_admin)
           ->with('telasPermissao.itemPermissao')
           ->get(50);

foto comes as:

"foto" : ""

I am returning the data from the controller like this:

return response()->json($resultado, 200);

In my operador model I have the following mutator:

public function getFotoAttribute($value)
{
    if ($value != null)
    {
        return pg_escape_string(fgets($value));
    }

        return null;
}
  • 写回答

2条回答 默认 最新

  • dongshao6591 2019-08-02 14:12
    关注

    Try updating your mutator like this:

    public function getFotoAttribute()
    {
        if ($this->attributes['foto'] != null)
        {
            return pg_escape_string(fgets($this->attributes['foto']));
        }
    
        return null;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图