private function retrieveGoods($commodityId, $specification) { // match $commodity = Commodity::find($commodityId); $goods = $commodity->goods->where('specification', $specification); // constraint error if ($goods->count() > 0) { throw new \Exception('Commodities and specification unique constraint error!'); } // create Goods if not exist if ($goods->isEmpty()) { $goods = new Goods(); $goods->commodity()->associate($commodity); $goods->specification = $specification; $goods->save(); } return $goods; }
public function destroy($id) { Commodity::find($id)->delete(); $commodities = Commodity::orderBy('commodity')->paginate(env('COMMODITIES_PAGINATION_MAX')); return view('commodities.index'); }
/* |-------------------------------------------------------------------------- | Model Factories |-------------------------------------------------------------------------- | | Here you may define all of your model factories. Model factories give | you a convenient way to create models for testing and seeding your | database. Just tell the factory how a default model should look. | */ $factory->define(User::class, function (Faker\Generator $faker) { return ['name' => $faker->name, 'email' => $faker->email, 'password' => bcrypt(str_random(10)), 'remember_token' => str_random(10)]; }); $factory->define(Buyer::class, function (Faker\Generator $faker) { return ['name' => str_random(5)]; }); $factory->define(Maker::class, function (Faker\Generator $faker) { return ['name' => str_random(10)]; }); $factory->define(Commodity::class, function (Faker\Generator $faker) { return ['name' => str_random(10)]; }); $factory->define(Goods::class, function (Faker\Generator $faker) { return ['specification' => str_random(3) . '-' . rand(100, 999), 'commodity_id' => Commodity::all()->random()->id]; }); $factory->define(ExternalOrder::class, function (Faker\Generator $faker) { return ['buyer_id' => Buyer::all()->random()->id]; }); $factory->define(InternalOrder::class, function (Faker\Generator $faker) { return ['maker_id' => Maker::all()->random()->id]; });