/** * Include books. * * @param \Appkr\Api\Example\Author $author * @param \League\Fractal\ParamBag|null $params * @return \League\Fractal\Resource\Collection */ public function includeBooks(Author $author, ParamBag $params = null) { $transformer = new BookTransformer($params); $parsed = $transformer->getParsedParams(); $books = $author->books()->limit($parsed['limit'])->offset($parsed['offset'])->orderBy($parsed['sort'], $parsed['order'])->get(); return $this->collection($books, new BookTransformer()); }
/** * Run the database seeds. * * @return void */ public function run() { $sqlite = in_array(config('database.default'), ['sqlite', 'testing'], true); if (!$sqlite) { DB::statement('SET FOREIGN_KEY_CHECKS=0'); } if (is_50() or is_51()) { Eloquent::unguard(); } $faker = Faker::create(); // Seeding authors table Author::truncate(); foreach (range(1, 10) as $index) { Author::create(['name' => $faker->userName, 'email' => $faker->safeEmail]); } $this->command->line("<info>Seeded:</info> authors table"); // Seeding resources table Book::truncate(); $authorIds = is_50() ? Author::pluck('id') : Author::pluck('id')->toArray(); foreach (range(1, 100) as $index) { Book::create(['title' => $faker->sentence(), 'author_id' => $faker->randomElement($authorIds), 'published_at' => $faker->dateTimeThisCentury, 'description' => $faker->randomElement([$faker->paragraph(), null]), 'out_of_print' => $faker->randomElement([true, false]), 'created_at' => $faker->dateTimeThisYear]); } if (is_50() or is_51()) { Eloquent::regard(); } if (!$sqlite) { DB::statement('SET FOREIGN_KEY_CHECKS=1'); } $this->command->line("<info>Seeded:</info> books table"); }
/** * Display a listing of the resource. * * @param null $id * @return \Illuminate\Contracts\Http\Response */ public function index($id = null) { // process v1.authors.books route if ('v1.authors.books' === \Route::currentRouteName()) { return json()->setMeta($this->meta)->withPagination(Author::find($id)->books()->latest()->paginate(5), new BookTransformer()); } // Respond with pagination return json()->setMeta($this->meta)->withPagination($this->model->latest()->paginate(5), new BookTransformer()); // Respond as a collection //return json()->setMeta($this->meta)->withCollection( // $this->model->latest()->get(), // new BookTransformer //); }
/** * Run the database seeds. * * @return void */ public function run() { if (!is_52()) { Eloquent::unguard(); } $faker = Faker::create(); // Seeding authors table Author::truncate(); foreach (range(1, 10) as $index) { Author::create(['name' => $faker->userName, 'email' => $faker->safeEmail]); } $this->command->line("<info>Seeded:</info> authors table"); // Seeding resources table Book::truncate(); $authorIds = is_50() ? Author::lists('id') : Author::lists('id')->toArray(); foreach (range(1, 100) as $index) { Book::create(['title' => $faker->sentence(), 'author_id' => $faker->randomElement($authorIds), 'published_at' => $faker->dateTimeThisCentury, 'description' => $faker->randomElement([$faker->paragraph(), null]), 'out_of_print' => $faker->randomElement([0, 1]), 'created_at' => $faker->dateTimeThisYear]); } if (!is_52()) { Eloquent::regard(); } $this->command->line("<info>Seeded:</info> books table"); }
/** @before */ public function stub() { $faker = \Faker\Factory::create(); $this->author = \Appkr\Api\Example\Author::create(['name' => 'foo', 'email' => $faker->safeEmail]); $this->books = \Appkr\Api\Example\Book::create(['title' => $faker->sentence(), 'author_id' => $this->author->id, 'published_at' => $faker->dateTimeThisCentury, 'description' => $faker->randomElement([$faker->paragraph(), null]), 'out_of_print' => $faker->randomElement([0, 1]), 'created_at' => $faker->dateTimeThisYear])->toArray(); }
/** * Display the specified resource. * * @param int $id * @return \Illuminate\Contracts\Http\Response */ public function show($id) { return json()->setMeta($this->meta)->withItem($this->model->findOrFail($id), new AuthorTransformer()); }
/** * Include books. * * @param \Appkr\Api\Example\Author $author * @param \League\Fractal\ParamBag|null $paramBag * @return \League\Fractal\Resource\Collection */ public function includeBooks(Author $author, ParamBag $paramBag = null) { $transformer = new BookTransformer($paramBag); $books = $author->books()->limit($transformer->getLimit())->offset($transformer->getOffset())->orderBy($transformer->getSortKey(), $transformer->getSortDirection())->get(); return $this->collection($books, new BookTransformer()); }