【发布时间】:2014-08-03 19:56:00
【问题描述】:
我正在尝试使用 Laravel 显示与外键 id 匹配的所有表记录。但是,我的查询没有将任何记录拉入视图。
如何找到与传入函数的外键 id 匹配的所有记录?
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';
}
我有 2 个表,Persona 和 Quote,我正在尝试提取与外键 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);
【问题讨论】:
-
注意:
implements UserInterface, RemindableInterface和use UserTrait, RemindableTrait;仅用于User类。