Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //item model
- protected $table = 'items';
- public function colors()
- {
- return $this->belongsToMany('AppModelsItemColor');
- }
- public function sizes()
- {
- return $this->belongsToMany('AppModelsItemSize');
- }
- //tiemsize model
- protected $table = 'item_sizes';
- public function items()
- {
- return $this->belongsToMany('AppModelsItem');
- }
- //item colors
- protected $table = 'item_colors';
- public function items()
- {
- return $this->belongsToMany('AppModelsItem');
- }
- //pivot table
- Schema::create('item_size_color', function (Blueprint $table) {
- $table->increments('id');
- $table->unsignedInteger('item_id')->index();
- $table->foreign('item_id')->references('id')->on('items');
- $table->unsignedInteger('item_size_id')->index();
- $table->foreign('item_size_id')->references('id')->on('item_sizes');
- $table->unsignedInteger('item_color_id')->index();
- $table->foreign('item_color_id')->references('id')->on('item_sizes');
- $table->timestamps();
- });
- //items table
- Schema::create('items', function (Blueprint $table) {
- $table->increments('id');
- $table->string('title');
- $table->timestamps();
- });
- //items color table
- Schema::create('item_colors', function (Blueprint $table) {
- $table->increments('id');
- $table->string('title');
- $table->timestamps();
- });
- //items size table
- Schema::create('item_sizes', function (Blueprint $table) {
- $table->increments('id');
- $table->string('title');
- $table->timestamps();
- });
- public function create(Create $request)
- {
- $item = new Item();
- $item->colors->attach($request->input('availableColors'));
- $item->sizes->attach($request->input('availableSizes'));
- $item->sizes = $request->input('color');
- $title = $request('title');
- if($request->hasFile('image')){ $item->image = $this->uploadFile($request, 'image'); }
- $item->save();
- $item->load('sizes');
- $item->load('colors');
- return response()->json([
- 'item' => $item,
- ]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement