我正在使用 Laravel 和 AngularJS 开发一个应用程序。
我的问题是我想从某个用户的表中获取所有信息。
在routes.php 文件中,我声明了一个组,以便我可以通过以下方式访问所有评论:
localhost/project/public/api/comments.
I also want to be able to get all comments by a given user by going to:
localhost/project/public/api/comment/id.
Route::group(array('prefix' => 'api'), function() {
Route::resource('comments', 'CommentController', array('only' => array('index', 'store', 'destroy')));
Route::get('comment/{id}', function($id) {
$col = 'user_id';
return Comments::where($col, '=', $id);
});
}
使用此代码时出现错误:
ErrorException Object of class Illuminate\Database\Eloquent\Builder could not be converted to string
I can receive the first result by adding:
return Comment::where($col, '=', $id)->first();
但我想收到给定用户的所有评论。这怎么办。
最佳答案
你需要得到一个结果
return Comments::where($col, '=', $id)->get();
但是你应该将其序列化为 JSON 格式(例如),所以你应该这样做:
$comments = Comments::where($col, '=', $id)->get();
return Response::json(array('success'=>true,'comments'=>$comments->toJson()));
关于php - 使用 Laravel 和 AngularJS 从数据库获取用户特定信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23825689/