Я хочу загрузить отношение заказа с клиентом, но оно возвращает 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);
}
Я хочу, чтобы данные клиента загружались вместе с заказом.