Php 将laravel原始查询与dingo/APi+;具有laravel 5.1的分形/变压器
我有一个带有索引方法的ArticleCommentsControllerPhp 将laravel原始查询与dingo/APi+;具有laravel 5.1的分形/变压器,php,laravel,eloquent,dingo-api,Php,Laravel,Eloquent,Dingo Api,我有一个带有索引方法的ArticleCommentsController class ArticleCommentsController extends BaseController { public function index($id) { $comments = DB::table('comments') ->leftJoin('users', 'users.id', '=', 'comments.user_id')
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类的实例。遗憾的是,
响应
对象上的项
方法似乎需要and对象,而不是数组。使用array
方法可以工作,但不会使用您传递的任何转换器
因此,我认为您可以使用ArrayObject
,如下所示:
return$this->response->item(新建ArrayObject($comments),新建CommentTransformer)代码>
记住放置使用ArrayObject
位于文件顶部。遗憾的是,响应
对象上的项
方法似乎需要and对象,而不是数组。使用array
方法可以工作,但不会使用您传递的任何转换器
因此,我认为您可以使用ArrayObject
,如下所示:
return$this->response->item(新建ArrayObject($comments),新建CommentTransformer)代码>
记住放置使用ArrayObject代码>在文件顶部。这是很久以前的事了,但如果我失去记忆,我会为这个人或其他人或我写答案哈哈哈
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);
}
}
这是很久以前的事了,但是如果我失忆了,我会为这个人或其他人或我将来写答案哈哈哈
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);
}
}
执行以下步骤,它会起作用:
1.改变
return$this->response->item($comments,newcommenttransformer)代码>
到
return$this->response->collection(collection::make($comments),newcommenttransformer)代码>
2.变压器类
namespace App\Transformers;
use League\Fractal\TransformerAbstract;
class CommentTransformer extends TransformerAbstract{
public function transform($comment)
{
return [
'id' => $comment->id,
...
];
}
}
按照以下步骤进行操作,结果是:
1.改变
return$this->response->item($comments,newcommenttransformer)代码>
到
return$this->response->collection(collection::make($comments),newcommenttransformer)代码>
2.变压器类
namespace App\Transformers;
use League\Fractal\TransformerAbstract;
class CommentTransformer extends TransformerAbstract{
public function transform($comment)
{
return [
'id' => $comment->id,
...
];
}
}
尝试从您的应用程序中删除->get()
query@smartrahat. 我这样做了,但它返回了QueryBuilder对象的一个实例,并返回了以下错误:传递给Dingo\Api\Http\Response\Factory::collection()的参数1必须是illumb\Support\collection的实例,给定的illumb\Database\Query\Builder的实例,在第41行的C:\xampp\htdocs\escape\app\Http\Controllers\articlescommentscocontroller.php中调用并定义了
请尝试从query@smartrahat. 我这样做了,但它返回了QueryBuilder对象的一个实例,并返回了以下错误:传递给Dingo\Api\Http\Response\Factory::collection()的参数1必须是illumb\Support\collection的实例,给定的illumb\Database\Query\Builder的实例,在第41行的C:\xampp\htdocs\escape\app\Http\Controllers\articlescommentscocontroller.php中调用并定义