dongzhi4073 2017-07-20 22:22
浏览 88
已采纳

Laravel查询返回构建器而不是集合

I am new to laravel and trying to have a result. My code is this:

class GegonosController extends Controller
{
    public function index($gid = null, $cid = null, $nid = null)
    {
        if (is_null($cid) and is_null($nid) and is_null($gid)) {
            $gegon = Gegono::orderBy('created_at','desc')->get(); 
        }
        else{
            if(!is_null($gid)) {
                if($gid == 0) {
                    $gegon = Gegono::where('gegtype_id','>',1);
                }
                else{
                    $gegon = Gegono::where('gegtype_id', '=', $gid);   
                }
            }
            else {
                $gegon = Gegono::where('gegtype_id','>',0);
            }
            if (!is_null($cid)) {
                $gegon->where('city_id', '=',$cid);
            }
            if (!is_null($nid)) {
                $gegon->where('nomos_id','=', $nid);
            }        
            $gegon->orderBy('created_at','desc')->get(); 
        }                        
        $nomoi = \App\Nomoi::orderby('name')->get();
        return view('front.gegonota', compact('gegon','gid','cid','nid','nomoi'));
    }

In the first, if when all variables are null the result is a collection of all the records of the table. In all other, if (other cases) the result is a builder and I get no results,

It doesn't show any error but gives no results.

Any help would save me a lot of time.

  • 写回答

1条回答 默认 最新

  • dongyin0628 2017-07-20 22:26
    关注

    Query Builder's get() method returns the collection of records. In your code, you just call get() method in the air, so it does nothing. You should assign it to a variable or use it in an expression.

    Source: https://laravel.com/docs/5.4/eloquent#collections

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里