this is my first post. I have a problem with my Laravel application. I need to modify the serialization of the data for my endpoint to get the data of a specific bike. The endpoint return this:
{"bike":{"id":32,
"unlock_code":2342,
"rack": {"id":3,
"available_stands":10,
"latitude":"46.754",
"longitude":"8.5732",
"available_bikes":10
},
"bike_state":{"description":"Available"}
}
}
but i want to have this:
{"bike":{"id":32,
"unlock_code":2342,
"rack":{"id":3,
"available_stands":10,
"latitude":"46.754",
"longitude":"8.5732",
"available_bikes":10
},
"bike_state":{"Available"}
}
}
the field name ('description') must be hidden. It's the first time i use laravel and i don't know if it's possible to do this.
This is the model
class BikeState extends Model
{
protected $hidden = ['id'];
public function bikes()
{
return $this->hasMany('App\Bike');
}
}
this is the repository with the method to retrieve the data:
class BikeRepository
{
public function findBikeById($id)
{
return Bike::with('rack','bikeState')->findOrFail($id);
}
}
and this is the controller
class BikeController extends Controller
{
private $bikeRepository;
public function __construct(BikeRepository $bikeRepository)
{
$this->bikeRepository = $bikeRepository;
}
public function getBike($id)
{
return response() ->json(['bike' => $this -> bikeRepository ->
findBikeByid($id)], 200);
}
}
Thank you