Advertisement
Guest User

Laravel Resourve

a guest
Jan 23rd, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.94 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Resources;
  4.  
  5. use Illuminate\Http\Resources\Json\ResourceCollection;
  6. use Config;
  7. use Carbon\Carbon;
  8.  
  9. class ExpensesRes extends ResourceCollection
  10. {
  11.  
  12.   public function toArray($request)
  13.   {
  14.     // dd($request->all());
  15.     return [
  16.       'data' => $this->collection->transform(function($expences) {
  17.  
  18.         //protected
  19.         $protected = false;
  20.         $dateProtected = new Carbon('-'. Config::get('app.protected') .' day');
  21.         if($expences->created_at <= $dateProtected) {
  22.           $protected = true;
  23.         }
  24.  
  25.         //category
  26.         $category_name = '';
  27.         if($expences->categories) {
  28.           $category_name = $expences->categories->name;
  29.         }
  30.  
  31.         //expenses_name
  32.         $expenses_name = '';
  33.         if($expences->expancenames) {
  34.           $expenses_name = $expences->expancenames->name;
  35.         }
  36.  
  37.         //photos
  38.         if(isset($expences->photos)) {
  39.           // dd($expences->photos[0]->url);
  40.           $exp_photo = $expences->photos->transform(function($photo) {
  41.             return [
  42.               'id' => $photo->id,
  43.               'name' => $photo->file_name,
  44.               'url' => $photo->url,
  45.               'image' => $photo->image,
  46.               ];
  47.           });
  48.           // $car_photo = $expences->photos[0]->url;
  49.         } else { $exp_photo = ''; }
  50.  
  51.         return [
  52.           'id' => $expences->id,
  53.           'category' => $expences->category,
  54.           'category_name' => $category_name,
  55.           'expenses_name' => $expenses_name,
  56.           'date' => $expences->date,
  57.           'cost' => $expences->cost,
  58.           'seller_id' => $expences->seller_id,
  59.           'description' => $expences->description,
  60.           'mileage' => $expences->mileage,
  61.           'created_at' => $expences->created_at,
  62.           'extras' => $expences->extras,
  63.           'exp_photo' => $exp_photo,
  64.           'protected' => $protected,
  65.         ];
  66.       }),
  67.     ];
  68.   }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement