Gleb 5 gadi atpakaļ
vecāks
revīzija
e06f7ccbed

+ 11 - 0
app/Category.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App;
+use Illuminate\Database\Eloquent\Model;
+
+class Category extends Model
+{
+    public function products(){
+        return $this->belongsToMany('App\Product');
+    }
+}

+ 10 - 3
app/Http/Controllers/Product.php

@@ -1,10 +1,10 @@
 <?php
 
 namespace App\Http\Controllers;
-
+use App\Product;
 use Illuminate\Http\Request;
 
-class Product extends Controller
+class ProductCntrl extends Controller
 {
     /**
      * Display a listing of the resource.
@@ -13,7 +13,14 @@ class Product extends Controller
      */
     public function index()
     {
-        //
+        $products =new Product;
+        $category = new Category;
+        $category->id = 1;
+
+        $products->id = 1;
+        $products->categories()->attach([$category->id,$products->id]);
+        $prd = Product::with('categories')->get();
+        return view('welcome',['products'=>$prd]);
     }
 
     /**

+ 12 - 0
app/Product.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace App;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Product extends Model
+{
+ public function categories(){
+        return $this->belongsToMany('App\Category');
+    }
+}

+ 4 - 1
database/migrations/2019_02_28_185732_orders.php

@@ -13,7 +13,10 @@ class Orders extends Migration
      */
     public function up()
     {
-
+        Schema::create('orders', function (Blueprint $table) {
+            $table->increments('id');
+            $table->timestamps();
+        });
     }
 
     /**

+ 4 - 0
resources/views/welcome.blade.php

@@ -81,6 +81,10 @@
 
             <div class="content">
                 <div class="title m-b-md">
+                    @foreach($products as $product)
+                        <div>{{$product->title}}</div>
+                     <div>{{$product->categories[0]->title}}</div>
+                        @endforeach
                     Здесь будет магазин!
                 </div>
 

+ 1 - 1
routes/web.php

@@ -16,5 +16,5 @@ Route::get('/', function () {
 });
 
 Auth::routes();
-
+Route::get('/','ProductCntrl@index');
 Route::get('/home', 'HomeController@index')->name('home');