Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. public function index()
  2. {
  3. return ProductResource::collection(Product::with('inventories')->paginate(25));
  4.  
  5. }
  6.  
  7. class Product extends Model
  8.  
  9. public function inventories(){
  10.  
  11. return $this->belongsToMany('AppInventory','inventory_product')->withPivot('price','stock')->withTimestamps();
  12.  
  13. }
  14.  
  15. class Inventory extends Model
  16.  
  17. public function products(){
  18.  
  19. return $this->belongsToMany('AppProduct','inventory_product')->withPivot('price','stock')->withTimestamps();
  20.  
  21. }
  22.  
  23. public function toArray($request)
  24. {
  25.  
  26. return [
  27. 'id'=>$this->id,
  28. 'name'=>$this->name,
  29. 'description'=>$this->description,
  30. 'short_description'=>$this->short_description,
  31. 'category'=>$this->category,//category_id
  32. 'url'=>$this->url,
  33. 'image'=>$this->image,
  34. 'relevant'=>$this->relevant,
  35. 'month'=>$this->month,
  36.  
  37. 'price'=>$this->price,
  38. 'stock'=>$this->stock
  39. ];
  40. }
  41.  
  42. Schema::create('inventories', function (Blueprint $table) {
  43. $table->increments('id');
  44. $table->string('name');
  45. $table->unsignedInteger('city_id');
  46. $table->timestamps();
  47.  
  48. $table-> foreign('city_id')->references('id')->on('cities')->onDelete('cascade');
  49.  
  50. });
  51.  
  52. Schema::create('products', function (Blueprint $table) {
  53. $table->increments('id');
  54. $table ->string('name');
  55. //$table ->integer('stock');
  56. $table ->string('description');
  57. $table ->string('short_description');
  58. $table ->unsignedInteger('category');//category_id
  59. //$table ->integer('price');
  60. $table ->string('url');
  61. $table ->string('image');
  62. $table ->boolean('relevant');
  63. $table ->boolean('month');
  64. $table->timestamps();
  65.  
  66. $table-> foreign('category')->references('id')->on('categories')->onDelete('cascade');
  67.  
  68. });
  69.  
  70. $table->increments('id');
  71. $table->integer('inventory_id')->unsigned();
  72. $table->integer('product_id')->unsigned();
  73. $table ->integer('price');
  74. $table ->integer('stock');
  75. $table->timestamps();
  76.  
  77. $table-> foreign('inventory_id')->references('id')->on('inventories')->onDelete('cascade');
  78. $table-> foreign('product_id')->references('id')->on('products')->onDelete('cascade');
  79.  
  80. {
  81.  
  82. {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement