Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve all records by foreign key with Laravel

Tags:

php

mysql

laravel

I am trying to display all table records that match a foreign key id using Laravel. However, my query is not pulling any records into the view.

How do I find all of the records which match a foreign key id that is passed into the function?

routes.php:

Route::get('/personas/{idPersona}/quotes', 'QuoteController@index');

QuoteController.php:

public function index($id)
    {
        $quotes = Quote::where('idPersona', $id)->get();
        return View::make('quotes.index')->with('quotes', $quotes);
    }

views/quotes/index.blade.php:

<h2> Quotes </h2>

@foreach($quotes as $quote)

    <li>{{ $quote }}</li>

@endforeach

models/Quote.php

class Quote extends Eloquent {

    public $timestamps = false;

    protected $table = 'quote';

    protected $primaryKey = 'idquote';

}

models/Persona.php

class Persona extends Eloquent {


    public $timestamps = false;

    protected $table = 'persona';

    protected $primaryKey = 'idPersona';


}

I have 2 tables, Persona and Quote, and I am trying to pull all the quotes that match the foreign key idPersona:

CREATE TABLE `mountain`.`persona` (
  `idPersona` INT NOT NULL AUTO_INCREMENT,
  `fName` VARCHAR(45) NULL,
  `lName` VARCHAR(45) NULL,
  `mName` VARCHAR(45) NULL,
  `bio` TEXT NULL,
  `dateBorn` VARCHAR(45) NULL,
  `dateDied` VARCHAR(45) NULL,
  PRIMARY KEY (`idPersona`));

CREATE TABLE `mountain`.`quote` (
  `idquote` INT NOT NULL AUTO_INCREMENT,
  `quoteText` TEXT NOT NULL,
  `quoteSource1` VARCHAR(100) NULL,
  `quoteSource2` VARCHAR(100) NULL,
  `tag1` VARCHAR(45) NULL,
  `tag2` VARCHAR(45) NULL,
  `tag3` VARCHAR(45) NULL,
  `idPersona` INT NULL,
  PRIMARY KEY (`idquote`),
  INDEX `idPersona_idx` (`idPersona` ASC),
  CONSTRAINT `idPersona`
    FOREIGN KEY (`idPersona`)
    REFERENCES `mountain`.`persona` (`idPersona`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);
like image 477
Fetus Avatar asked Aug 03 '14 19:08

Fetus


1 Answers

If you are using Eloquent, you have to get benifit of its powerfull ORM, to get all quotes that belongs to specific user you have to declare the relations first:

models/Persona.php

class Persona extends Eloquent {


    public $timestamps = false;

    protected $table = 'persona';

    protected $primaryKey = 'idPersona';

    function quotes() {
        return $this->hasMany('Quote', 'idquote');
    }

}

models/Quote.php

class Quote extends Eloquent {

    public $timestamps = false;

    protected $table = 'quote';

    protected $primaryKey = 'idquote';

    function persona() {
        return $this->belongsTo('Persona', 'idPersona');
    }
}

Then you can simply get the desired persona with all related quotes by using the relation we difined above:

QuoteController.php

public function index($id) {
    $quotes = Persona::with('quotes')->find($id)->quotes;
    return View::make('quotes.index')->with('quotes', $quotes);
}
like image 84
Walid Ammar Avatar answered Sep 28 '22 10:09

Walid Ammar