【问题标题】:How to retrieve all records by foreign key with Laravel如何使用 Laravel 通过外键检索所有记录
【发布时间】: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, RemindableInterfaceuse UserTrait, RemindableTrait; 仅用于 User 类。

标签: php mysql laravel


【解决方案1】:

如果你使用 Eloquent,你必须从它强大的 ORM 中获益,要获得所有 属于特定用户的引号,你必须先声明关系:

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');
    }
}

然后您可以使用我们上面定义的关系简单地获得所需的persona 以及所有相关引号:

QuoteController.php

public function index($id) {
    $quotes = Persona::with('quotes')->find($id)->quotes;
    return View::make('quotes.index')->with('quotes', $quotes);
}

【讨论】:

    【解决方案2】:

    使用 Eloquent 在您的角色和报价模型之间建立关系可能会有所帮助。

    在您的情况下,您可能希望将“hasMany/belongsTo”关系应用为 Quote->BelongsTo->Personas 并且 Personas 有很多 Quotes?

    http://laravel.com/docs/eloquent 让您更详细地了解可能的关系。

    【讨论】:

    • 如果他将使用 Eloquent,他将使用 Quotes 或 Personas id。但如果他将使用 Fluent,那么他可以创建一个查询来使用 FK。
    【解决方案3】:

    如果你不想使用模型关系,你可以简单地使用 Database: Query Builder for more details

    试试这个:


    假设我有两个表:

    1.报告_has_tests

    2.测试。在测试表中我有我的外键。

     DB::table('reports_has_tests')
            ->join('tests', 'tests.id', '=', 'reports_has_tests.tests_id')
            ->select('tests.*')->where('reports_has_tests.reports_id',$yourID)->get();
    

    与模型类似,您可以这样做:

     YourModelName::join('tests', 'tests.id', '=', 'reports_has_tests.tests_id')
                ->select('tests.*')->where('reports_has_tests.reports_id',$yourID)->get();
    

    注意:我添加此答案只是为了帮助那些不想使用 laravel 模型关系并希望直接从有关单个 ID 的表中获取数据的人。为了使用上述查询,您的迁移/表中必须有关系。

    【讨论】:

      猜你喜欢
      • 2013-07-26
      • 2013-05-23
      • 2016-03-03
      • 2019-09-04
      • 2016-02-18
      • 1970-01-01
      • 2019-09-18
      • 2014-03-11
      • 2015-04-16
      相关资源
      最近更新 更多