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

时间:2018-06-21 15:14:50

标签: laravel-5 eloquent laravel-5.6 laravel-resource

我想使用laravel 5.6中的Resource将关系数据放入json

查询时,我得到--configuration作为对象。 (在框内标记了第一个数据) (我需要使用API​​资源的此类功能)
但是对于API Resources,它仅显示response.data.created_by,而不显示id中的"created_by" object(第二个数据在框中标记)

*数据差异在方框内标出。
*使用渴望的获取来获取数据。

url:response.response.data.created_by
响应:


    {
        "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');
        }
    }
    
http://localhost:8000/api/product/unit

1 个答案:

答案 0 :(得分:0)

问题出在单元模型中:我必须使用与列名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),