title: Retrieving data in Laravel using Eloquent and foreign keys
body: <p>I am making a game library system for a project and trying to implement a search function which would display the user games that they are searching for.</p>
  
  <p>I have two models called Game and GameInformation. This is due to many games having the same information, i.e. being the same game. The migration files look like:</p>
  
  <p>GameInformation</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateGameInformationTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('game_information', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('title');
              $table-&gt;text('description');
              $table-&gt;string('genre');
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('Game_Information');
      }
  }
  </code></pre>
  
  <p>Game</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateGamesTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('games', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;integer('info_id')-&gt;unsigned();
              $table-&gt;foreign('info_id')-&gt;references('id')-&gt;on('game_information');
              $table-&gt;enum('type', ['disc', 'cartridge']);
              $table-&gt;integer('platform_id')-&gt;unsigned();
              $table-&gt;foreign('platform_id')-&gt;references('id')-&gt;on('platforms');
              $table-&gt;integer('price');
              $table-&gt;year('year_released');
              $table-&gt;boolean('available')-&gt;default(true);
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('Games');
      }
  }
  </code></pre>
  
  <p>So what should happen once the user has called a search, it should retrieve the all the game informations that match their query, and then loop through them all and find the games that are associated with that information. In the end it should return an array of arrays where the inner arrays contain the game and the game information objects.</p>
  
  <p>Search bar</p>
  
  <pre><code>&lt;form class="form-inline" action="/search" method="POST" role="/search"&gt;
      {{ csrf_field() }}
      &lt;div class="input-group"&gt;
          &lt;input type="text" class="form-control" name="q" placeholder="Search"&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Search function in SearchController</p>
  
  <pre><code>public function search() {
    $q = Input::get ( 'q' );
    if(strlen($q) == 0 || strpos($q, '%') !== false) { // For security
      return view ('home')-&gt;with('failureMsg','Nothing was found...');
    }
    $infos = GameInformation::where('title', 'like', '%' . $q .'%')-&gt;get();
    $games = array();
    foreach($infos as $info) {
      $gamesWithInfo = $info-&gt;games();
      array_push($games, array($info, $gamesWithInfo));
    }
    if (count ( $games ) &gt; 0)
        return view ('home')-&gt;with( $games )-&gt;withQuery($q);
    else
        return view ('home')-&gt;with('failureMsg','Nothing was found...');
  }
  </code></pre>
  
  <p>Display search results</p>
  
  <pre><code>&lt;div class="card"&gt;
      &lt;div class="card-header"&gt;Dashboard&lt;/div&gt;
      &lt;div class="card-body"&gt;
        &lt;div class="container"&gt;
          &lt;div class="row"&gt;
          &lt;/div&gt;
            @if(!empty($failureMsg))
            &lt;div class="alert alert-failure"&gt; {{ $failureMsg }}&lt;/div&gt;
            @endif
            @if(isset($games))
            HELLO
                &lt;p&gt; The search results for your query &lt;b&gt; {{ $query }} &lt;/b&gt; are :&lt;/p&gt;
            &lt;h2&gt;Games found:&lt;/h2&gt;
            &lt;table class="table table-striped"&gt;
                &lt;thead&gt;
                    &lt;tr&gt;
                        &lt;th&gt;Title&lt;/th&gt;
                        &lt;th&gt;Description&lt;/th&gt;
                    &lt;/tr&gt;
                &lt;/thead&gt;
                &lt;tbody&gt;
                    @foreach($games as $game)
                    &lt;tr&gt;
                        &lt;td&gt;{{$game(0)-&gt;title}}&lt;/td&gt;
                        &lt;td&gt;{{$game(0)-&gt;description}}&lt;/td&gt;
                    &lt;/tr&gt;
                    @endforeach
                &lt;/tbody&gt;
            &lt;/table&gt;
            @endif
        &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>It doesn't display anything when a correct query is input, however it does display an error message when an incorrect one is input. So I'm thinking it's return an array of empty arrays.</p>
  
  <p>Also in the SearchController on the line:</p>
  
  <pre><code>$infos = GameInformation::where('title', 'like', '%' . $q .'%')-&gt;get();
  </code></pre>
  
  <p>I have also tried making it:</p>
  
  <pre><code>$infos = GameInformation::where('title', 'like', '%' . $q .'%');
  </code></pre>
  
  <p>But that will return the error message where nothing is found.</p>
  
  <p>Also the models:</p>
  
  <p>Game</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  use Eloquent;
  
  class Game extends Eloquent
  {
    protected $primaryKey = 'id';
  
    public function information() {
      return $this-&gt;belongsTo('App\GameInformation');
    }
  }
  </code></pre>
  
  <p>GameInformation</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  use Eloquent;
  
  class GameInformation extends Eloquent
  {
      protected $table = 'game_information';
      protected $primaryKey = 'id';
  
      public function games() {
        return $this-&gt;hasMany('App\Game', 'info_id');
      }
  }
  </code></pre>