Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5 Laravel 5.6 API资源未显示关系数据_Laravel 5_Eloquent_Laravel 5.6_Laravel Resource - Fatal编程技术网

Laravel 5 Laravel 5.6 API资源未显示关系数据

Laravel 5 Laravel 5.6 API资源未显示关系数据,laravel-5,eloquent,laravel-5.6,laravel-resource,Laravel 5,Eloquent,Laravel 5.6,Laravel Resource,我想使用Laravel5.6中的资源将关系数据转换成json 当我查询时,我会得到由创建的response.data。(第一个数据标记在框中)(我需要使用API资源的这种功能) 但是,在response.response.data.created\u by中,它只显示id,而不显示“created\u by”对象。(第二个数据在框中标记) *数据差异在框内标记。 *数据是使用即时获取获取的 url:http://localhost:8000/api/product/unit 答复: {

我想使用Laravel5.6中的资源将关系数据转换成json

当我查询时,我会得到由创建的
response.data。(第一个数据标记在框中)(我需要使用API资源的这种功能)

但是,在
response.response.data.created\u by
中,它只显示
id
,而不显示
“created\u by”对象
。(第二个数据在框中标记)

*数据差异在框内标记。
*数据是使用即时获取获取的

url:
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" } } ] } } { “数据”:[ { “id”:1, “单位”:“米”, “符号”:“m”, “十进制”:1, +----------------------------------------------------------------------------------+ |“创建人”:{| |“id”:1| |“名称”:“管理员”| |“电子邮件”:admin@gmail.com", | |“api_代币”:“$2y$10$.c7eJGS6x/C8JN9Hd.QC1OGPUS8TXMDUIHJZNBRRHQVGRYBJCC5U”| |“创建时间”:“2018-05-09 15:45:59”| |“更新时间:”2018-06-08 15:38:41| |}, | +----------------------------------------------------------------------------------+ “更新人”:{ “id”:1, “名称”:“管理员”, “电子邮件”:admin@gmail.com", “api_代币”:“$2y$10$.c7eJGS6x/C8JN9Hd.QC1OGPUS8TXMDUIHJZNBRRHQVGRYBJCC5U”, “创建时间”:“2018-05-09 15:45:59”, “更新时间:”2018-06-08 15:38:41 }, “创建时间”:“2018-06-19 00:38:54”, “更新时间”:“2018-06-19 20:00:16” } ], “资源”:{ “数据”:[ { “id”:1, “单位”:“米”, “符号”:“m”, “十进制”:1, +----------------+ |“createdBy”:1| +----------------+ “更新人”:1, “创建数据”:{ “日期”:“2018-06-19 00:38:54.000000”, “时区类型”:3, “时区”:“亚洲/加尔各答” }, “updatedAt”:{ “日期”:“2018-06-19 20:00:16.000000”, “时区类型”:3, “时区”:“亚洲/加尔各答” } } ] } } 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) | |]; | +------------------------------------------------------+ } } 名称空间App\Http\Controllers\Product; 使用App\Models\Product\Unit; 使用\Http\Request; 使用App\Http\Controllers\Controller; 使用Illumb\Support\Facades\Validator; 使用App\Http\Resources\Product\UnitResourceCollection; 使用App\Http\Resources\Product\UnitResource; 使用Illumb\Validation\ValidationException; 类UnitController扩展控制器{ 公共职能指数() { $units=Unit::with(['created_by','updated_by'])->get(); +------------------------------------------------------+ |返回[| |“数据”=>美元单位| |“资源”=>新的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'); } } 名称空间App\Models\Product; 使用Illumb\Database\Elount\Model; 类单元扩展模型 { 由()创建的公共函数{ 返回$this->belongsTo('App\User','created\u by','id'); } 公共职能由()更新{ 返回$this->belongsTo('App\User','updated\u by','id'); } }
UnitResource.php
名称空间App\Http\Resources\Product;
使用App\Http\Resources\UserResource;
使用\Http\Resources\Json\JsonResource;
类UnitResource扩展了JsonResource
{
公共功能待命($request)
{
返回[
'id'=>this->id,
“单位”=>$this->unit,
'symbol'=>$this->symbol,
“十进制”=>this->decimal,
“createdBy”=>$this->创建人,
'updatedBy'=>this->updatedBy,
'createdAt'=>$this->created\u在,
'updatedAt'=>$this->updated\u在
];
}
}

问题出在单元模型中:我必须使用不同的方法名
created\u by()
,而不是列名
created\u by

更改以下代码后,它正在工作:

Unit.php
模型:

public function created_by(){
->
public function createdby(){

public function updated_by(){
->
public function updated by(){


UnitController.php
Controller:

$units=Unit::with(['createdby','updatedby'])->get();
->
$units=Unit::with(['createdby','updatedby'])->get();


UnitResource.php
Resource:

'createdBy'=>$this->createdBy,
->
'createdBy'=>新用户资源($this->createdBy),

'updatedBy'=>$this->updatedBy,
->
'updatedBy'=>新用户资源
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
        ];
    }
}