我想使用laravel 5.6中的Resource将关系数据导入json
当我查询时,我得到 response.data.created_by
作为对象 . (框中标记的第一个数据)(我需要使用API资源的这种功能)
但是API Resources只在 response.response.data.created_by
中显示 id
而不是 "created_by" object
. (框中标有第二个数据)
*数据差异在框内标出 .
*使用eager fetch获取数据 .
网址: http://localhost:8000/api/product/unit
响应:
{
"data": [
{
"id": 1,
"unit": "Meter",
"symbol": "m",
"decimal": 1,
+----------------------------------------------------------------------------------+
|"created_by": { |
| "id": 1, |
| "name": "Admin", |
| "email": "admin@gmail.com", |
| "api_token": "$2y$10$.c7eJGS6x/C8JN9Hd.Qc1OgPUS8txMDuIHjZNBRRlHQVGrYbJcC5u", |
| "created_at": "2018-05-09 15:45:59", |
| "updated_at": "2018-06-08 15:38:41" |
|}, |
+----------------------------------------------------------------------------------+
"updated_by": {
"id": 1,
"name": "Admin",
"email": "admin@gmail.com",
"api_token": "$2y$10$.c7eJGS6x/C8JN9Hd.Qc1OgPUS8txMDuIHjZNBRRlHQVGrYbJcC5u",
"created_at": "2018-05-09 15:45:59",
"updated_at": "2018-06-08 15:38:41"
},
"created_at": "2018-06-19 00:38:54",
"updated_at": "2018-06-19 20:00:16"
}
],
"resource": {
"data": [
{
"id": 1,
"unit": "Meter",
"symbol": "m",
"decimal": 1,
+----------------+
|"createdBy": 1, |
+----------------+
"updatedBy": 1,
"createdAt": {
"date": "2018-06-19 00:38:54.000000",
"timezone_type": 3,
"timezone": "Asia/Kolkata"
},
"updatedAt": {
"date": "2018-06-19 20:00:16.000000",
"timezone_type": 3,
"timezone": "Asia/Kolkata"
}
}
]
}
}
UnitController.php:
namespace App\Http\Controllers\Product;
use App\Models\Product\Unit;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use App\Http\Resources\Product\UnitResourceCollection;
use App\Http\Resources\Product\UnitResource;
use Illuminate\Validation\ValidationException;
class UnitController extends Controller {
public function index()
{
$units = Unit::with(['created_by', 'updated_by'])->get();
+------------------------------------------------------+
|return [ |
| 'data' => $units, |
| 'resource' => new UnitResourceCollection($units) |
|]; |
+------------------------------------------------------+
}
}
单位型号:
namespace App\Models\Product;
use Illuminate\Database\Eloquent\Model;
class Unit extends Model
{
public function created_by() {
return $this->belongsTo('App\User', 'created_by', 'id');
}
public function updated_by() {
return $this->belongsTo('App\User', 'updated_by', 'id');
}
}
UnitResource.php
<pre>
namespace App\Http\Resources\Product;
use App\Http\Resources\UserResource;
use Illuminate\Http\Resources\Json\JsonResource;
class UnitResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'unit' => $this->unit,
'symbol' => $this->symbol,
'decimal' => $this->decimal,
'createdBy' => $this->created_by,
'updatedBy' => $this->updated_by,
'createdAt' => $this->created_at,
'updatedAt' => $this->updated_at
];
}
}
1 回答
问题出在单元模型中:我必须使用与列名
created_by
不同的方法名created_by()
.更改以下代码后,它正在工作:
Unit.php
型号:public function created_by() {
- >public function createdby() {
public function updated_by() {
- >public function updatedby() {
UnitController.php
控制器:$units = Unit::with(['created_by', 'updated_by'])->get();
- >$units = Unit::with(['createdby', 'updatedby'])->get();
UnitResource.php
资源:'createdBy' => $this->created_by,
- >'createdBy' =>new UserResource($this->createdby),
'updatedBy' => $this->updated_by,
- >'updatedBy' => new UserResource($this->updatedby),