我有一個簡單的資源:
<?php
namespace App\Http\Resources\Integration\PayGo;
use Illuminate\Http\Resources\Json\JsonResource;
class VendaVenderResource extends JsonResource
{
public $preserveKeys = true;
protected $method;
public function paymentMethod(string $method = null)
{
$this->method = $method;
return $this;
}
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
dd("ENTROU");
return [
'id' => $this->id,
];
}
}
當我做了一個簡單的服務,在代碼中間呼叫這個資源,如下所示:
$vendaVenderResource = (new Resources\Integration\PayGo\VendaVenderResource($entry));
dd("PASSOU");
Resource 中的此方法 toArray() 未執行且未進入我的dd("ENTROU"); 出于測驗目的,它正在執行此dd("PASSOU"); 在資源之后我不知道為什么!
我在做什么錯?
uj5u.com熱心網友回復:
您不能以這種方式呼叫您的資源。資源應該被認為是一個 api(所以接受 json 將被發送,而 Laravel 應該知道要回傳什么......
如果你想以這種方式使用資源,你應該使用方法resolve (new Resources\Integration\PayGo\VendaVenderResource($entry))->resolve();
Ps.: Entrou e passou are so heebrbr kkkkkk
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/336377.html
標籤:拉拉维尔
