Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. //item model
  2. protected $table = 'items';
  3.  
  4.  
  5. public function colors()
  6. {
  7. return $this->belongsToMany('AppModelsItemColor');
  8. }
  9.  
  10. public function sizes()
  11. {
  12. return $this->belongsToMany('AppModelsItemSize');
  13. }
  14.  
  15. //tiemsize model
  16.  
  17. protected $table = 'item_sizes';
  18.  
  19.  
  20. public function items()
  21. {
  22. return $this->belongsToMany('AppModelsItem');
  23. }
  24.  
  25. //item colors
  26.  
  27. protected $table = 'item_colors';
  28.  
  29.  
  30. public function items()
  31. {
  32. return $this->belongsToMany('AppModelsItem');
  33. }
  34.  
  35. //pivot table
  36. Schema::create('item_size_color', function (Blueprint $table) {
  37. $table->increments('id');
  38. $table->unsignedInteger('item_id')->index();
  39. $table->foreign('item_id')->references('id')->on('items');
  40. $table->unsignedInteger('item_size_id')->index();
  41. $table->foreign('item_size_id')->references('id')->on('item_sizes');
  42. $table->unsignedInteger('item_color_id')->index();
  43. $table->foreign('item_color_id')->references('id')->on('item_sizes');
  44. $table->timestamps();
  45. });
  46.  
  47. //items table
  48. Schema::create('items', function (Blueprint $table) {
  49. $table->increments('id');
  50. $table->string('title');
  51. $table->timestamps();
  52. });
  53.  
  54. //items color table
  55. Schema::create('item_colors', function (Blueprint $table) {
  56. $table->increments('id');
  57. $table->string('title');
  58. $table->timestamps();
  59. });
  60.  
  61. //items size table
  62. Schema::create('item_sizes', function (Blueprint $table) {
  63. $table->increments('id');
  64. $table->string('title');
  65. $table->timestamps();
  66. });
  67.  
  68. public function create(Create $request)
  69. {
  70. $item = new Item();
  71. $item->colors->attach($request->input('availableColors'));
  72. $item->sizes->attach($request->input('availableSizes'));
  73. $item->sizes = $request->input('color');
  74. $title = $request('title');
  75. if($request->hasFile('image')){ $item->image = $this->uploadFile($request, 'image'); }
  76. $item->save();
  77.  
  78. $item->load('sizes');
  79. $item->load('colors');
  80.  
  81.  
  82. return response()->json([
  83. 'item' => $item,
  84. ]);
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement