簡體   English   中英

"如何使用 Laravel 通過外鍵檢索所有記錄"

[英]How to retrieve all records by foreign key with Laravel

我正在嘗試使用 Laravel 顯示與外鍵 id 匹配的所有表記錄。 但是,我的查詢沒有將任何記錄拉入視圖。

如何找到與傳遞給函數的外鍵 id 匹配的所有記錄?

路線.php:

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

報價控制器.php:

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

意見/報價/index.blade.php:

<h2> Quotes </h2>

@foreach($quotes as $quote)

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

@endforeach

模型/Quote.php

class Quote extends Eloquent {

    public $timestamps = false;

    protected $table = 'quote';

    protected $primaryKey = 'idquote';

}

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

如果您使用的是Eloquent,則必須受益於其功能強大的ORM,要獲取屬於特定用戶的所有引號,必須先聲明該關系:

型號/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);
}

使用Eloquent在角色和行情模型之間建立關系可能會有所幫助。

在您的情況下,您可能想將Quote:-> BelongsTo-> Personas和“ Personas”引用成一個“ hasMany / belongsTo”關系?

http://laravel.com/docs/eloquent提供了有關可能關系的更詳細的概述。

如果您不想使用模型關系,您可以簡單地使用 Database: Query Builder了解更多詳細信息<\/a>

試試這個:


假設我有兩個表:

1.reports_has_tests<\/strong>

2.測試<\/strong>。 在測試表中我有我的外鍵。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM