php 如何使用 Laravel 通过外键检索所有记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25108536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:39:58  来源:igfitidea点击:

How to retrieve all records by foreign key with Laravel

phpmysqllaravel

提问by Burntspaghetti

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.

我正在尝试使用 Laravel 显示与外键 ID 匹配的所有表记录。但是,我的查询没有将任何记录拉入视图。

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

如何找到与传递给函数的外键 ID 匹配的所有记录?

routes.php:

路线.php:

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

QuoteController.php:

报价控制器.php:

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

views/quotes/index.blade.php:

意见/报价/index.blade.php:

<h2> Quotes </h2>

@foreach($quotes as $quote)

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

@endforeach

models/Quote.php

模型/报价.php

class Quote extends Eloquent {

    public $timestamps = false;

    protected $table = 'quote';

    protected $primaryKey = 'idquote';

}

models/Persona.php

模型/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:

我有 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);

回答by Walid Ammar

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

如果您使用 Eloquent,您必须受益于其强大的 ORM,要获取属于特定用户的所有引号,您必须先声明关系:

models/Persona.php

模型/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

模型/报价.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 personawith all related quotes by using the relation we difined above:

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

QuoteController.php

报价控制器.php

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

回答by dtdi

It may be helpful to set up a Relationship between your Personas and your Quotes Model using Eloquent.

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

In your case you might want to apply a "hasMany/belongsTo" relationship as Quote->BelongsTo->Personas and a Personas has many Quotes?

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

http://laravel.com/docs/eloquentgives you a more detailed overview over possible relationships.

http://laravel.com/docs/eloquent为您提供有关可能关系的更详细概述。