dotj78335 2015-04-13 22:41
浏览 60
已采纳

Laravel 5 - 从数据库中获取集合中的所有记录

Ok,so I am setting up a relationship between my users and roles table, everything went fine until I tried to get every record. For example, If I just go with:

User::with('roles')->findOrFail(1);

Then all good, it returns the user and in that instance the 2 roles assigned. However what I can't wrap my head around us this:

Say I want to get all the users and all their roles so I can display them out in a table for example, how would I do that? I just can't seem to wrap my head around it.

  • 写回答

2条回答 默认 最新

  • duanchazhou6779 2015-04-14 02:49
    关注

    If you use findOrFail by itself without chaining another method User::findOrFail(1), it will return a collection, but once you begin adding other conditions you must call either ->get() for a collection of users, ->first or ->last() for a single user object or if you do something with ->lists() you will get an array. Otherwise you will get the builder class, here's an example of a {{ dd(User::where('id', 1)) }}

    Builder {#1097 ▼
      #query: Builder {#1096 ▶}
      #model: User {#409 ▶}
      #eagerLoad: array:1 [▶]
      #macros: array:4 [▶]
      #onDelete: Closure {#1102 ▶}
      #passthru: array:12 [▶]
    }
    

    The Builder class is like a prepared statement which was never executed. When call to dd() you should see the something similar to the following.

    This will give you a collection {{ dd(\App\User::where('id', 1)->get()) }}

    Collection {#1105 ▼
      #items: array:1 [▼
        0 => User {#1108 ▼
          #cardUpFront: false
          #dates: array:3 [▶]
          #table: "users"
          #fillable: array:2 [▶]
          #hidden: array:2 [▶]
          #connection: null
          #primaryKey: "id"
          #perPage: 15
          +incrementing: true
          +timestamps: true
          #attributes: array:17 [▶]
          #original: array:17 [▶]
          #relations: []
          #visible: []
          #appends: []
          #guarded: array:1 [▶]
          #casts: []
          #touches: []
          #observables: []
          #with: []
          #morphClass: null
          +exists: true
          #forceDeleting: false
        }
      ]
    }
    

    or to get a single object with {{ dd(\App\User::where('id', 1)->first()) }}

    User {#1108 ▼
      #cardUpFront: false
      #dates: array:3 [▶]
      #table: "users"
      #fillable: array:2 [▶]
      #hidden: array:2 [▶]
      #connection: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:17 [▶]
      #original: array:17 [▶]
      #relations: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      #forceDeleting: false
    }
    

    and finally an array with {{ dd(\App\User::where('id', 1)->lists('email','id')) }}

    array:1 [▼
      1 => "godfrey17@example.net"
    ]
    

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题