In the example we are passing team_name from Resource A(TeamResource) to Resrouce B(SportResource)
Resource A
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class TeamResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' =>$this->id,
'team_name'=>$this->team_name,
'sport_id'=>$this->sport_id,
'sport_category_id'=>$this->sport_category_id,
'user_id'=>$this->user_id,
'created_at' => $this->created_at,
'updated_at'=>$this->updated_at,
'sport'=> new SportResource($this->sport,$this->team_name),
];
}
}
Resource B
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Storage;
class SportResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*
*
*/
public $team_name;
public function __construct($resource,$team_name)
{
parent::__construct($resource);
$this->resource = $resource;
$this->team_name = $team_name;
}
public function toArray($request)
{
return [
'id' => $this->id,
'team_name' => $this->team_name,
'sport_name' => $this->sport_name,
'sport_image' => url(Storage::url($this->sport_image)),
'is_category' => $this->is_category,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at
];
}
}
Comments
Post a Comment
If you any doubt of any topic then you can comment me.