2

我想加载与客户的订单关系,但它null在邮递员中返回

在此处输入图像描述


客户模型

<?php

namespace App;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Customer extends Authenticatable implements JWTSubject
{
     use Notifiable;


    protected $fillable = [
        'first_name',
        'last_name',
        'phone_number',
        'postal_code',
        'email',
        'preferred_method_to_contact',
        'password',
        'address',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function getNameAttribute()
    {
        $name = $this->first_name . " " . $this->last_name;
        return $name;
    }

    public function orders()
    {
      return $this->hasMany('App\Order');
    }

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }
}

订单模式

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    protected $fillable = [
            'first_name',
            'last_name',
            'email',
            'phone_number',
            'post_code',
            'address',
            'alternative_address',
            'property_type',
            'property_type_other',
            'contract_type',
            'contract_type_other',
            'no_of_bedrooms',
            'no_of_bathrooms',
            'customer_id',
            'best_day',
            'best_time',
            'type_of_cleaning',
        ];


    public function customers()
    {
        return $this->belongsTo('App\Customer');
    }
}

订单资源

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class OrderResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        /*return parent::toArray($request);*/
        return [
            'id' => $this->id,
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'email' => $this->email,
            'phone_number' => $this->phone_number,
            'post_code' => $this->post_code,
            'address' => $this->address,
            'alternative_address' => $this->alternative_address,
            'property_type' => $this->property_type,
            'property_type_other' => $this->property_type_other,
            'contract_type' => $this->contract_type,
            'contract_type_other' => $this->contract_type_other,
            'no_of_bedrooms' => $this->no_of_bedrooms,
            'phone_number' => $this->phone_number,
            'no_of_bathrooms' => $this->no_of_bathrooms,
            'customer' => new CustomerResource($this->whenLoaded('customers')),
        ];
    }
}

客户资源

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class CustomerResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
     public function toArray($request)
    {
        /*return parent::toArray($request);*/
        return [
            'id' => $this->id,
            'first_name' => $this->first_name,
            'last_name' => $this->last_name,
            'email' => $this->email,
        ];
    }
}

OrderController这是负责发送 JSON 响应的函数

public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'first_name' => ['required', 'string', 'max:10'],
            'last_name' => ['required', 'string', 'max:10'],
            'email' => ['required', 'string', 'email', 'max:50'],
            'phone_number' => ['required', 'string', 'max:30'],
            'post_code' => ['required', 'string', 'max:30'],
            'address' => ['required', 'string', 'max:300'],
            'alternative_address' => ['required', 'string', 'max:300'],
            'property_type' => ['required', 'string', 'max:30'],
            'contract_type' => ['required', 'string', 'max:30'],
            'no_of_bedrooms' => ['required', 'string', 'max:30'],
            'type_of_cleaning' => ['required', 'string', 'max:30'],
            'best_day' => ['required', 'string', 'max:30'],
            'best_day' => ['required', 'string', 'max:30'],
        ]);

        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        }

        // $customer = Customer::findOrFail($request->customer_id);
        $order = Order::create($request->all());
        // $order = Order::create();
        $order->load(array('customers'));

        return new OrderResource($order);
    }

我希望将客户详细信息与订单一起加载。

4

1 回答 1

0

不知何故,我设法解决了这个问题。实际上,ManyToOne关系函数没有识别customers. 该customers函数以 the 为目标,many-to-one因此根据 laravel 的标准,它应该是单数而不是复数。

public function customer()
{
   return $this->belongsTo('App\Customer');
}

同样在OrderController

$order->load(array('customer')); // <- make it singular as well

在里面OrderResource

'customer' => new CustomerResource($this->whenLoaded('customer')), // <- make it singular
于 2019-09-20T09:43:36.837 回答