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条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波