Laravel资源集合自升级到5.6后不再工作
我有以下代码: 型号Laravel资源集合自升级到5.6后不再工作,laravel,Laravel,我有以下代码: 型号 class Document extends Model { use OwnedByCustomer, OwnedByUser; public function document_type() { return $this->belongsTo(DocumentType::class); } } use Illuminate\Http\Resources\Json\Resource; class DocumentResou
class Document extends Model
{
use OwnedByCustomer, OwnedByUser;
public function document_type() {
return $this->belongsTo(DocumentType::class);
}
}
use Illuminate\Http\Resources\Json\Resource;
class DocumentResource extends Resource {
public function toArray($request) {
return [
'id' => $this->id,
'created_at' => $this->created_at,
'document_type' => DocumentType::make($this->document_type),
'description' => $this->description,
'batch_number' => $this->batch_number,
'size' => $this->size,
'file_name' => $this->file_name
];
}
}
及
资源
class Document extends Model
{
use OwnedByCustomer, OwnedByUser;
public function document_type() {
return $this->belongsTo(DocumentType::class);
}
}
use Illuminate\Http\Resources\Json\Resource;
class DocumentResource extends Resource {
public function toArray($request) {
return [
'id' => $this->id,
'created_at' => $this->created_at,
'document_type' => DocumentType::make($this->document_type),
'description' => $this->description,
'batch_number' => $this->batch_number,
'size' => $this->size,
'file_name' => $this->file_name
];
}
}
及
使用\Http\Resources\Json\Resource
class DocumentTypeResource extends Resource {
public function toArray($request) {
return [
'id' => $this->id,
'enum' => $this->enum,
'name' => $this->getTranslations('name')
];
}
}
然后我有一个控制器,它可以做到这一点:
class DocumentController extends Controller {
public function index() {
$query = Document::query();
$query->withoutGlobalScope(OwnedByUser::class);
return DocumentResource::collection($query->paginate());
}
这是我一直广泛使用的一种常见设置,没有太多问题。但是,由于某些原因,集合没有包装在json数据属性中,请求中也缺少链接和元属性。
而且,返回的对象包含所有属性,即使我去掉了其中的一些属性
我现在完全不知道是什么导致了这一切。我从5.6开始就有这种情况,但找不到原因
有人有什么想法吗?回复有点晚,但这实际上解决了我的问题,,而不是rapping()强> 只需使用这些:
- use Illuminate\Http\Resources\Json\JsonResource;
- JsonResource::withoutWrapping();
而不是
- use Illuminate\Http\Resources\Json\Resource;
- Resource::withoutWrapping();
您可能通过web路由而不是api路由使用它。据我所知,laravel资源仅适用于api路由。