/** * Store a newly created resource in storage. * * @param BooksRequest $request * @return \Illuminate\Contracts\Http\Response */ public function store(BooksRequest $request) { // Merging author_id for Demo purpose. // In real project we should use $request->user()->id instead. $data = array_merge($request->all(), ['author_id' => 1]); if (!($book = $this->model->create($data))) { return json()->internalError('Failed to create !'); } // respond created item with 201 status code and location header return json()->setStatusCode(StatusCode::CREATED)->setHeaders(['Location' => route('v1.books.show', ['id' => $book->id, 'include' => 'author'])])->withItem($book, new BookTransformer()); // respond simple message with 201 status code //return json()->created('Created'); // You can also pass Eloquent model to the created method // Then it will append created resource to the response body //return json()->created($book); }
/** * 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"); }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return $this|\Illuminate\Contracts\Http\Response */ public function store(Request $request) { $this->validate($request, ['title' => 'required|min:2', 'description' => 'min:2']); // Merging author_id for Demo purpose. // In real project we should use $request->user()->id instead. $data = array_merge($request->all(), ['author_id' => 1]); if (!($book = Book::create($data))) { return json()->internalError('Failed to create !'); } // respond created item with 201 status code return json()->setStatusCode(201)->withItem($book, new BookTransformer()); // respond with simple message //return json()->created('Created'); }
/** * 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(); }