【发布时间】:2019-09-25 00:01:48
【问题描述】:
我试图从 Laravel 的查询生成器中获取单个值,但问题是我得到了一个数组。 这是我在 Postman 中使用 dd($myVar) 的查询结果:
[{"is_liked":1}]
还有 echo($myVar):
Collection {#976
#items: array:1 [
0 => NewsCommentLike {#970
#fillable: array:3 [
0 => "user_id"
1 => "news_comment_id"
2 => "is_liked"
]
#connection: "mysql"
#table: "news_comment_likes"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:1 [
"is_liked" => 1
]
#original: array:1 [
"is_liked" => 1
]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
}
]
}
我的代码是:
$previous_is_liked = NewsCommentLike::query()->where(['user_id' => $user_id, 'news_comment_id' => $newsComment->id])->get(['is_liked']);
如何从 is_liked 而非数组中获取单个值?
【问题讨论】:
-
使用 first() 方法代替 get()
-
使用
first()然后get() -
此方法无效。 @jigs
-
这个方法不起作用,我也得到了一个数组。 @karan-sadana
标签: php laravel query-builder