PHP code example of behamin / bresources

1. Go to this page and download the library: Download behamin/bresources 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/ */

    

behamin / bresources example snippets


    public function index(EmailFilter $filters)
    {
        list($emails, $count) = Email::filter($filters);
        $emails = $emails->get();
        
        return EmailResource::collection(['data' => $emails, 'count' => $count]);
    }

    public function show(Email $email)
    {
        return new EmailResource(['data' => $email, 'message'=> 'email info.']);
    }



namespace App\Http\Resources;

use Behamin\BResources\Resources\BasicResource;

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()->errors('phone is not updated');
        }
        
        return apiResponse()->data($phone)->message('phone is updated')->get();
    }
    
    public function delete(Phone $phone)
    {
        $phone->delete();
        
        return apiResponse()->message('phone info.')->next('https://debut.test')->status(200)->get();
    }
}
bash
php artisan make:bresource ResourceClassName
bash
php artisan make:brequest RequestClassName