【问题标题】:Use laravel raw queries with dingo/APi + Fractal/transformers with laravel 5.1将 laravel 原始查询与 dingo/APi + Fractal/transformers 与 laravel 5.1 一起使用
【发布时间】:2016-04-30 00:04:22
【问题描述】:

我有一个带有索引方法的 ArticleCommentsController

class ArticleCommentsController extends BaseController
{
    public function index($id)
    {

        $comments = DB::table('comments')
            ->leftJoin('users', 'users.id', '=', 'comments.user_id')
            ->where('comments.article_id', '=', $id)
            ->get();

         return $this->response->item($comments, new CommentTransformer);
    }
}

这是变压器类

namespace App\Transformers;

use League\Fractal\TransformerAbstract;

class CommentTransformer extends TransformerAbstract{
    public function transform($comment)
    {
        return $comment; //simplified
    }
}

响应如下错误:

get_class() expects parameter 1 to be object, array given.

显然,我需要在调用 Fractal\transform 时发送一个注释对象的实例,但我不知道该怎么做,因为 laravel 的原始查询只返回一个数组或 QueryBuilder 类的实例。

【问题讨论】:

  • 尝试从您的查询中删除->get()
  • @smartrahat。我这样做了,但随后它返回了 QueryBuilder 对象的实例并返回了此错误:Argument 1 passed to Dingo\Api\Http\Response\Factory::collection() must be an instance of Illuminate\Support\Collection, instance of Illuminate\Database\Query\Builder given, called in C:\xampp\htdocs\escape\app\Http\Controllers\ArticlesCommentsController.php on line 41 and defined

标签: php laravel eloquent dingo-api


【解决方案1】:

遗憾的是,response 对象上的item 方法似乎需要和对象而不是数组。使用array 方法会起作用,但不会使用您传递的任何转换器。

所以,我认为您可能会使用ArrayObject,如下所示:

return $this->response->item(new ArrayObject($comments), new CommentTransformer);

记得在文件顶部加上use ArrayObject;

【讨论】:

    【解决方案2】:

    这是很久以前的事了,但如果我失忆了,我会为这个人或其他人或我写下答案哈哈哈

    class ArticleCommentsController extends BaseController
    {
        public function index($id)
        {
    
            $comments = DB::table('comments')
                ->leftJoin('users', 'users.id', '=', 'comments.user_id')
                ->where('comments.article_id', '=', $id)
                ->get();
    
             return $this->response->collection(Collection::make($comments), new CommentTransformer);
    
        }
    }
    

    当然你需要把它添加到控制器 ArticleCommentsController

    // Dingo
    use Dingo\Api\Routing\Helpers;
    
    //Convert query to collective
    use Illuminate\Support\Collection;
    
    //Transformers for API
    use App\Transformers\CommentTransformer;
    

    在你的函数之前在你的控制器里面

    //Use for Dingo Helpers
    use Helpers;
    

    大家一起:

    <?php
    
    namespace App\Http\Controllers;
    use Response;
    use App\User;
    use App\Http\Requests;
    use Illuminate\Http\Request;
    
    // Dingo
    use Dingo\Api\Routing\Helpers;
    
    //Convert query from LMS lbrary to collective
    use Illuminate\Support\Collection;
    
    //Transformers for API
    use App\Transformers\CommentTransformer;
    
    class ArticleCommentsController extends BaseController
    {
    
        //Use for Dingo Helpers
        use Helpers;
    
        public function index($id)
        {
    
            $comments = DB::table('comments')
                ->leftJoin('users', 'users.id', '=', 'comments.user_id')
                ->where('comments.article_id', '=', $id)
                ->get();
    
             return $this->response->collection(Collection::make($comments), new CommentTransformer);
    
        }
    }
    

    问候!,我希望这对以后的其他人有所帮助:D

    【讨论】:

      【解决方案3】:

      执行以下步骤,就可以了:

      1.改变

      return $this-&gt;response-&gt;item($comments, new CommentTransformer);

      return $this-&gt;response-&gt;collection(Collection::make($comments), new CommentTransformer);

      2.变压器类

      namespace App\Transformers;
      use League\Fractal\TransformerAbstract;
      
      class CommentTransformer extends TransformerAbstract{
           public function transform($comment)
           {
               return [
                  'id' => $comment->id,
                  ...
               ];
           }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-07-22
        • 2016-04-29
        • 2018-06-17
        • 2016-01-08
        • 2016-06-28
        • 2021-09-18
        • 2017-05-11
        • 2016-01-30
        相关资源
        最近更新 更多