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>
 


Thursday, August 11, 2016

Tuesday, July 12, 2016

Cygwin and Laravel

I played with getting cygwin to run Laravel and the l5scaffold. Here are the additions you need to install in cygwin
  1. php, git, sqlite3, and curl.
  2. The following php extensions.
    1. json
    2. phar
    3. mbstring
    4. zip
    5. tokenizer
    6. pdo-sqlite
    7. ctype
Don't if anyone else will ever, need this, but there ya go.

Sunday, May 29, 2016

Photo panoramas

I've used and liked Hugin before and blogger about it here. I recently downloaded the latest version and I just couldn't figure out how to use it. My bad no doubt, but after an hour getting no traction using the user's guide, I gave up and looked for something else. autostitch was the answer. Three clicks and I was done.

Thursday, March 24, 2016

Audacity create labels

Geez I like Audacity. I recently needed to put track markers in a long "song" and it's built right in. Choose Analyze -> Regular Interval Tracks. I like ten minutes and the number after the "Label":

Wednesday, January 27, 2016

How to become a security professional

Well, really, here are some useful links.

http://www.darkreading.com/careers-and-people/why-cybersecurity-certifications-matter----or-not/d/d-id/1324004?_mc=RSS_DR_EDT
http://www.zdnet.com/article/getting-started-with-a-career-in-cybersecurity/
http://www.troyhunt.com/2015/09/troys-ultimate-list-of-security-links.html
https://www.cybrary.it/course/advanced-penetration-testing/

Friday, January 15, 2016

Rockbox Rocks

I wanted to put a bunch of music on a Sansa Clip+ or Sport Clip and both seriously messed up the order of the tracks. First, ID3 v1 doesn't support more than 255 tracks, which wasn't enough. I used mutagen (https://bitbucket.org/lazka/mutagen) to make sure the tracks were numbered correctly via this script
TOTAL=`ls -1 | wc -l | sed -e "s/ //g"`
COUNT=1

for i in *
do
    mid3v2 -T "$COUNT/$TOTAL" "$i"
    mid3v2 --delete-v1 "$i"
    COUNT=$((COUNT+1))
done
The downside with mutagen is that it always converts to ID3 v2.4 that many players won't recognize. Nicely, with mp3tag (http://www.mp3tag.de/en/) one can choose the version desired, and there is an automated track numbering tool. Still no joy. As best as I can tell, the Sansa clips have a bug or two that make the whole effort futile. Enter Rockbox (http://www.rockbox.org/). I had no idea there was such a thing as free music player firmware. Cool! I downloaded the installer and the necessary Sansa firmware from http://forums.sandisk.com/t5/Sansa-Clip-Sansa-Clip/Sansa-Clip-Firmware-Update-01-02-16/td-p/150227, ran the installer, and it worked! So I did what I always do: donated to the mp3tag and Rockbox projects. The Clip Sport is not currently supported by Rockbox...
I learned far more about mp3 tagging than I ever wanted to, so thought I'd pass a little of the learning on.