Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, rather than having your primary keys be <code>tid</code> and <code>fid</code>, just keep them both as <code>id</code>. This is not only best practice, but will allow you to more easily use Laravel's Eloquent ORM as it by default assumes your primary key column is named <code>id</code>.</p> <p>Second thing, make sure your table names are in plural form. Although this is not necessary, the example I'm about to give is using Laravel defaults, and Laravel assumes they are in plural form.</p> <p>Anyway, once you've 'Laravelized' your database, you can use an Eloquent model to setup awesome relationships with very minimal work. Here's what I think you'd want to do.</p> <p><strong>app/models/Team.php</strong> <pre><code>class Team extends Eloquent { // Yes, this can be empty. It just needs to be declared. } </code></pre> <p><strong>app/models/Fixture.php</strong></p> <pre><code>class Fixture extends Eloquent { public function homeTeam() { return $this-&gt;belongsTo('Team', 'home_team_id'); } public function awayTeam() { return $this-&gt;belongsTo('Team', 'away_team_id'); } } </code></pre> <p>Above, we created a simple model <code>Team</code> which Laravel will automatically look for in the <code>teams</code> database table.</p> <p>Second, we created model <code>Fixture</code> which again, Laravel will use the <code>fixtures</code> table for. In this model, we specified two relationships. The <code>belongsTo</code> relationship takes two parameters, what model it is related to, in both cases here they are teams, and what the column name is.</p> <p>Laravel will automatically take the value in <code>away_team_id</code> and search it against the <code>id</code> column in your <code>teams</code> table.</p> <p>With just this minimal amount of code, you can then do things like this.</p> <pre><code>$fixture = Fixture::find(1); // Retrieves the fixture with and id of 1. $awayTeam = $fixture-&gt;awayTeam()-&gt;first(); // var_dump this to see what you get. $homeTeam = $fixutre-&gt;homeTeam()-&gt;first(); </code></pre> <p>Then you can proceed as normal and access the column names for the tables. So say you have a 'name' column in the teams table. You can echo out the the home team name from the fixture like so.</p> <pre><code>$fixture = Fixture::find(1); // Get the fixture. echo $fixture-&gt;homeTeam-&gt;name; </code></pre> <p>It's nearly 2AM, so there might be an error or two above, but it should work.</p> <p>Make sure you check the docs for <a href="http://laravel.com/docs/eloquent" rel="nofollow">Eloquent</a>, especially the bits relating to relationships. Remember to name your columns and tables in the way Laravel wants you to. If you don't, there are ways to specify your custom names.</p> <p>If you want to get even more fancy, you can define the inverse relationship like this on your Team model.</p> <p><strong>app/models/Team.php</strong> <pre><code>class Team extends Eloquent { public function fixturesAtHome() { return $this-&gt;hasMany('Fixture', 'home_team_id'); } public function fixturesAway() { return $this-&gt;hasMany('Fixture', 'away_team_id'); } } </code></pre> <p>Then to get all of a particular team's home fixtures...</p> <pre><code>$team = Team::find(1); // Retreive team with id of 1; $homeFixtures = $team-&gt;fixturesAtHome(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload