PHP code example of omalizadeh / laravel-api-response
1. Go to this page and download the library: Download omalizadeh/laravel-api-response library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
omalizadeh / laravel-api-response example snippets
public function index(EmailFilter $filters)
{
$emailsFilterResult = Email::filter($filters);
return EmailResource::collection([
'data' => $emailsFilterResult->data(),
'count' => $emailsFilterResult->count(),
]);
}
public function show(Email $email)
{
return new EmailResource(['data' => $email, 'message'=> 'email info.']);
}
namespace App\Http\Resources;
use Omalizadeh\ApiResponse\Resources\BaseApiResource;
class EmailResource extends BasicResource
{
protected function transformDataItem($item)
{
return [
'id' => $item->id,
'email' => $item->email,
'status' => $item->status
];
}
}
class PhoneController
{
public function show(Phone $phone)
{
return apiResponse()->data($phone)->message('phone info.')->status(200)->get();
}
public function index()
{
$phones = Phone::all();
return apiResponse()->collection($phones, $phones->count())->message('phone info.')->status(200)->get();
}
public function update(Request $request, Phone $phone)
{
$isUpdated = $phone->update($request->all());
if (!$isUpdated) {
return apiResponse()->errorMessage('phone is not updated');
}
return apiResponse()->data($phone)->message('phone is updated')->get();
}
}