php - 使用 Laravel 和 AngularJS 从数据库获取用户特定信息

标签 php database angularjs laravel

我正在使用 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/

相关文章:

angularjs - 如何使用智能表和angularjs实现自定义搜索

php - 将所有图像从 mysql 数据库 blob 提取到目录

php - Docusign 从模板下载文档

php - 为所有用户而不是某一用户显示个人资料图片

javascript - 如何从执行 Stripe Integration 的 Controller 中获取 DOM 元素?

javascript - 如何在 Angular 中重复 ajax 调用并在需要时取消它?

php - 如何使用 Ajax 和 PHP 从动态文本框中插入数据

php - 将格式化的地址状态代码更改为 Woocommerce 订单中的状态名称

sql - 水平计数和垂直计数哪个更快?

database - 外键字段可以与它在另一个表中引用的字段具有不同的名称吗?