Monday, August 22, 2016

More than you wanted to know about Laravel and M2M

One approach is to create a scaffold for the M2M table and enter the mappings there. I did:

php artisan make:scaffold --schema=name:string person
php artisan make:scaffold --schema=todo:string todo
php artisan make:scaffold --schema='todo_id:integer:foreign,person_id:integer:foreign' PeopleTodo

Then, I had to make some changes with the each of the MVC. Maybe I'll get around to post about using Form:: and  an example where the pivot doesn't have a scaffold but where each class has and add/delete drop down. In any care, here are the raw diffs.

diff --git a/app/Http/Controllers/PeopleTodoController.php b/app/Http/Controllers/PeopleTodoController.php
index f34e019..dafb9fa 100644
--- a/app/Http/Controllers/PeopleTodoController.php
+++ b/app/Http/Controllers/PeopleTodoController.php
@@ -1,5 +1,7 @@
 <?php namespace App\Http\Controllers;
 
+use App\Person;
+use App\Todo;
 use App\Http\Requests;
 use App\Http\Controllers\Controller;
 
@@ -15,9 +17,11 @@ class PeopleTodoController extends Controller {
      */
     public function index()
     {
+        $people = Person::all();
+        $todos = Todo::all();
         $people_todos = PeopleTodo::orderBy('id', 'desc')->paginate(10);
 
-        return view('people_todos.index', compact('people_todos'));
+        return view('people_todos.index', compact('people_todos', 'people', 'todos'));
     }
 
     /**
@@ -27,7 +31,9 @@ class PeopleTodoController extends Controller {
      */
     public function create()
     {
-        return view('people_todos.create');
+        $people = Person::all();
+        $todos = Todo::all();
+        return view('people_todos.create', compact( 'people', 'todos'));
     }
 
     /**
@@ -40,10 +46,8 @@ class PeopleTodoController extends Controller {
     {
         $people_todo = new PeopleTodo();
 
-        $people_todo->todo_id = $request->input("todo_id");
-        $people_todo->todo_id = $request->input("todo_id");
-        $people_todo->person_id = $request->input("person_id");
         $people_todo->person_id = $request->input("person_id");
+        $people_todo->todo_id = $request->input("todo_id");
 
         $people_todo->save();
 
@@ -59,8 +63,10 @@ class PeopleTodoController extends Controller {
     public function show($id)
     {
         $people_todo = PeopleTodo::findOrFail($id);
+        $person = Person::findOrFail($people_todo->person_id)->name;
+        $todo = Todo::findOrFail($people_todo->todo_id)->todo;
 
-        return view('people_todos.show', compact('people_todo'));
+        return view('people_todos.show', compact('people_todo', 'person', 'todo'));
     }
 
     /**
@@ -71,9 +77,12 @@ class PeopleTodoController extends Controller {
      */
     public function edit($id)
     {
+        $people = Person::all();
+        $todos = Todo::all();
         $people_todo = PeopleTodo::findOrFail($id);
 
-        return view('people_todos.edit', compact('people_todo'));
+        return view('people_todos.edit', compact('people_todo', 'people',
+            'todos'));
     }
 
     /**
@@ -88,8 +97,6 @@ class PeopleTodoController extends Controller {
         $people_todo = PeopleTodo::findOrFail($id);
 
         $people_todo->todo_id = $request->input("todo_id");
-        $people_todo->todo_id = $request->input("todo_id");
-        $people_todo->person_id = $request->input("person_id");
         $people_todo->person_id = $request->input("person_id");
 
         $people_todo->save();
diff --git a/app/Http/Controllers/PersonController.php b/app/Http/Controllers/PersonController.php
index 7acbf6b..a7809d9 100644
--- a/app/Http/Controllers/PersonController.php
+++ b/app/Http/Controllers/PersonController.php
@@ -1,5 +1,6 @@
 <?php namespace App\Http\Controllers;
 
+use App\Todo;
 use App\Http\Requests;
 use App\Http\Controllers\Controller;
 
diff --git a/app/PeopleTodo.php b/app/PeopleTodo.php
index d2f1afb..03c1db4 100644
--- a/app/PeopleTodo.php
+++ b/app/PeopleTodo.php
@@ -6,5 +6,4 @@ use Illuminate\Database\Eloquent\Model;
 
 class PeopleTodo extends Model
 {
-    //
 }
diff --git a/app/Person.php b/app/Person.php
index ebc7dea..f6be618 100644
--- a/app/Person.php
+++ b/app/Person.php
@@ -6,5 +6,8 @@ use Illuminate\Database\Eloquent\Model;
 
 class Person extends Model
 {
-    //
+    public function todos()
+    {
+        return $this->belongsToMany('App\Todo', 'people_todos');
+    }
 }
diff --git a/app/Todo.php b/app/Todo.php
index e21e027..28febf5 100644
--- a/app/Todo.php
+++ b/app/Todo.php
@@ -6,5 +6,8 @@ use Illuminate\Database\Eloquent\Model;
 
 class Todo extends Model
 {
-    //
+    public function people()
+    {
+        return $this->belongsToMany('App\Person', 'people_todos');
+    }
 }
diff --git a/resources/views/people/show.blade.php b/resources/views/people/show.blade.php
index 7534417..f5edabe 100644
--- a/resources/views/people/show.blade.php
+++ b/resources/views/people/show.blade.php
@@ -19,13 +19,14 @@
 
             <form action="#">
                 <div class="form-group">
-                    <label for="nome">ID</label>
-                    <p class="form-control-static"></p>
-                </div>
-                <div class="form-group">
                      <label for="name">NAME</label>
                      <p class="form-control-static">{{$person->name}}</p>
                 </div>
+                 <label>TODOS</label>
+                <ul>
+                    <? foreach ($person->todos as $todo)
+                        { echo ('<li>'.$todo->todo.'</li>'); } ?>
+                </ul>
             </form>
 
             <a class="btn btn-link" href="{{ route('people.index') }}"><i class="glyphicon glyphicon-backward"></i>  Back</a>
diff --git a/resources/views/people_todos/create.blade.php b/resources/views/people_todos/create.blade.php
index eadd702..335ad2a 100644
--- a/resources/views/people_todos/create.blade.php
+++ b/resources/views/people_todos/create.blade.php
@@ -18,29 +18,38 @@
                 <input type="hidden" name="_token" value="{{ csrf_token() }}">
 
                 <div class="form-group @if($errors->has('todo_id')) has-error @endif">
-                       <label for="todo_id-field">Todo_id</label>
-                    <input type="text" id="todo_id-field" name="todo_id" class="form-control" value="{{ old("todo_id") }}"/>
-                       @if($errors->has("todo_id"))
-                        <span class="help-block">{{ $errors->first("todo_id") }}</span>
-                       @endif
-                    </div>
-                    <div class="form-group @if($errors->has('todo_id')) has-error @endif">
-                       <label for="todo_id-field">Todo_id</label>
-                    <input type="text" id="todo_id-field" name="todo_id" class="form-control" value="{{ old("todo_id") }}"/>
+
+                    <select
+                        id="todo_id-field"
+                        name="todo_id"
+                        class="form-control">
+                        <?php foreach ($todos as $todo) { ?>
+                            <option value="<?php echo $todo->id; ?>">
+                                <?php echo $todo->todo; ?>
+                            </option>
+                        <?php } ?>
+                    </select>
+
                    @if($errors->has("todo_id"))
-                        <span class="help-block">{{ $errors->first("todo_id") }}</span>
-                       @endif
-                    </div>
-                    <div class="form-group @if($errors->has('person_id')) has-error @endif">
-                       <label for="person_id-field">Person_id</label>
-                    <input type="text" id="person_id-field" name="person_id" class="form-control" value="{{ old("person_id") }}"/>
-                       @if($errors->has("person_id"))
-                        <span class="help-block">{{ $errors->first("person_id") }}</span>
+                   <span class="help-block">{{ $errors->first("todo_id")
+}}</span>
                    @endif
                     </div>
+
                     <div class="form-group @if($errors->has('person_id')) has-error @endif">
-                       <label for="person_id-field">Person_id</label>
-                    <input type="text" id="person_id-field" name="person_id" class="form-control" value="{{ old("person_id") }}"/>
+                    <label for="person_id-field">Person</label>
+
+                    <select
+                        id="person_id-field"
+                        name="person_id"
+                        class="form-control">
+                        <?php foreach ($people as $person) { ?>
+                            <option value="<?php echo $person->id; ?>">
+                                <?php echo $person->name; ?>
+                            </option>
+                        <?php } ?>
+                    </select>
+
                     @if($errors->has("person_id"))
                     <span class="help-block">{{ $errors->first("person_id") }}</span>
                     @endif
diff --git a/resources/views/people_todos/edit.blade.php b/resources/views/people_todos/edit.blade.php
index 6ef2168..dfcdb0c 100644
--- a/resources/views/people_todos/edit.blade.php
+++ b/resources/views/people_todos/edit.blade.php
@@ -19,29 +19,37 @@
                 <input type="hidden" name="_token" value="{{ csrf_token() }}">
 
                     <div class="form-group @if($errors->has('todo_id')) has-error @endif">
-                       <label for="todo_id-field">Todo_id</label>
-                    <input type="text" id="todo_id-field" name="todo_id" class="form-control" value="{{ is_null(old("todo_id")) ? $people_todo->todo_id : old("todo_id") }}"/>
-                       @if($errors->has("todo_id"))
-                        <span class="help-block">{{ $errors->first("todo_id") }}</span>
-                       @endif
-                    </div>
-                    <div class="form-group @if($errors->has('todo_id')) has-error @endif">
-                       <label for="todo_id-field">Todo_id</label>
-                    <input type="text" id="todo_id-field" name="todo_id" class="form-control" value="{{ is_null(old("todo_id")) ? $people_todo->todo_id : old("todo_id") }}"/>
+                    <label for="todo_id-field">Todo</label>
+
+                    <select
+                        id="todo_id-field"
+                        name="todo_id"
+                        class="form-control">
+                        <?php foreach ($todos as $todo) { ?>
+                            <option value="<?php echo $todo->id; ?>">
+                                <?php echo $todo->todo; ?>
+                            </option>
+                        <?php } ?>
+                    </select>
+
                    @if($errors->has("todo_id"))
                    <span class="help-block">{{ $errors->first("todo_id") }}</span>
                    @endif
                     </div>
+
                     <div class="form-group @if($errors->has('person_id')) has-error @endif">
-                       <label for="person_id-field">Person_id</label>
-                    <input type="text" id="person_id-field" name="person_id" class="form-control" value="{{ is_null(old("person_id")) ? $people_todo->person_id : old("person_id") }}"/>
-                       @if($errors->has("person_id"))
-                        <span class="help-block">{{ $errors->first("person_id") }}</span>
-                       @endif
-                    </div>
-                    <div class="form-group @if($errors->has('person_id')) has-error @endif">
-                       <label for="person_id-field">Person_id</label>
-                    <input type="text" id="person_id-field" name="person_id" class="form-control" value="{{ is_null(old("person_id")) ? $people_todo->person_id : old("person_id") }}"/>
+                    <label for="person_id-field">Person</label>
+
+                    <select
+                        id="person_id-field"
+                        name="person_id"
+                        class="form-control">
+                        <?php foreach ($people as $person) { ?>
+                            <option value="<?php echo $person->id; ?>">
+                                <?php echo $person->name; ?>
+                            </option>
+                        <?php } ?>
+                    </select>
                     @if($errors->has("person_id"))
                     <span class="help-block">{{ $errors->first("person_id") }}</span>
                     @endif
diff --git a/resources/views/people_todos/index.blade.php b/resources/views/people_todos/index.blade.php
index a7ce276..43d51a8 100644
--- a/resources/views/people_todos/index.blade.php
+++ b/resources/views/people_todos/index.blade.php
@@ -17,11 +17,8 @@
                 <table class="table table-condensed table-striped">
                     <thead>
                         <tr>
-                            <th>ID</th>
-                            <th>TODO_ID</th>
-                        <th>TODO_ID</th>
-                        <th>PERSON_ID</th>
-                        <th>PERSON_ID</th>
+                        <th>TODO</th>
+                        <th>PERSON</th>
                             <th class="text-right">OPTIONS</th>
                         </tr>
                     </thead>
@@ -29,11 +26,8 @@
                     <tbody>
                         @foreach($people_todos as $people_todo)
                             <tr>
-                                <td>{{$people_todo->id}}</td>
-                                <td>{{$people_todo->todo_id}}</td>
-                    <td>{{$people_todo->todo_id}}</td>
-                    <td>{{$people_todo->person_id}}</td>
-                    <td>{{$people_todo->person_id}}</td>
+                                <td>{{$todos->find($people_todo->todo_id)->todo}}</td>
+                                <td>{{$people->find($people_todo->person_id)->name}}</td>
                                 <td class="text-right">
                                     <a class="btn btn-xs btn-primary" href="{{ route('people_todos.show', $people_todo->id) }}"><i class="glyphicon glyphicon-eye-open"></i> View</a>
                                     <a class="btn btn-xs btn-warning" href="{{ route('people_todos.edit', $people_todo->id) }}"><i class="glyphicon glyphicon-edit"></i> Edit</a>
diff --git a/resources/views/people_todos/show.blade.php b/resources/views/people_todos/show.blade.php
index b3e6087..923ed04 100644
--- a/resources/views/people_todos/show.blade.php
+++ b/resources/views/people_todos/show.blade.php
@@ -19,24 +19,12 @@
 
             <form action="#">
                 <div class="form-group">
-                    <label for="nome">ID</label>
-                    <p class="form-control-static"></p>
+                     <label for="person_id">PERSON</label>
+                     <p class="form-control-static">{{$person}}</p>
                 </div>
                 <div class="form-group">
-                     <label for="todo_id">TODO_ID</label>
-                     <p class="form-control-static">{{$people_todo->todo_id}}</p>
-                </div>
-                    <div class="form-group">
-                     <label for="todo_id">TODO_ID</label>
-                     <p class="form-control-static">{{$people_todo->todo_id}}</p>
-                </div>
-                    <div class="form-group">
-                     <label for="person_id">PERSON_ID</label>
-                     <p class="form-control-static">{{$people_todo->person_id}}</p>
-                </div>
-                    <div class="form-group">
-                     <label for="person_id">PERSON_ID</label>
-                     <p class="form-control-static">{{$people_todo->person_id}}</p>
+                     <label for="todo_id">TODO</label>
+                     <p class="form-control-static">{{$todo}}</p>
                 </div>
             </form>
 


No comments: