我正在運行這個查詢$results = DB::connection('selection') ->select(" SELECT id, name, email FROM users WHERE email = $this->email ");我希望只能得到結果,我確實得到了,這是我的輸出array:1 [ 0 => { +"id": 1 +"name": "Ted Wood" +"email": "[email protected]" }]我想知道的是如何訪問name而不需要做$results[0]->name我想做的事情$results->name。因為我只得到 1 項,所以我認為不需要 foreach 循環
3 回答
青春有我
TA貢獻1784條經驗 獲得超8個贊
DB::select()返回一個數組。該first()方法適用于集合,因此您需要將數組包裝在集合中才能使用該first()方法。
$results = collect($results)->first()
catspeake
TA貢獻1111條經驗 獲得超0個贊
首先使用
$results?=collect(DB::connection('selection')
????????????????????????????????????->select("
????????????????????????????????????????????SELECT?id,?name,?email
????????????????????????????????????????????FROM?users
????????????????????????????????????????????WHERE?email?=?$this->email
???????????????????????????????????"))->first();但我認為最好的方法是:
??$results?=?DB::connection('selection')->table('users')->select('id',?'name',?'email')
????????->where('email',$this->email)->limit(1)->first();- 3 回答
- 0 關注
- 174 瀏覽
添加回答
舉報
0/150
提交
取消
