第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從Laravel 5.8中的另一個表中調(diào)用數(shù)據(jù)

如何從Laravel 5.8中的另一個表中調(diào)用數(shù)據(jù)

PHP
慕慕森 2021-05-18 13:14:16
我有兩個表格,一個用于“模型”,另一個用于“投票”,我想通過投票獲得模型名稱(投票字段=模型ID)投票遷移表public function up(){    Schema::create('votantes', function (Blueprint $table) {        $table->increments('id');        $table->string('nome', 100);        $table->string('email', 60)->unique();        $table->integer('voto')->unsigned();        $table->timestamps();                 $table->foreign('voto')->references('id')->on('candidatas')->onDelete('restrict');    });}投票模型<?phpnamespace App;use Illuminate\Database\Eloquent\Model;use App\Candidata;class Votante extends Model{    protected $fillable = ['nome', 'email', 'voto'];    public function candidata()     {        return $this->belongsTo('App\Candidata');    }}型號遷移表public function up(){    Schema::create('candidatas', function (Blueprint $table) {        $table->increments('id');        $table->string('nome', 100);        $table->string('clube', 60);        $table->string('foto', 100);        $table->timestamps();    });}我的控制器public function votacao(){    $linhas = Votante::orderBy('nome')->get();    // dd($linhas);    return view('votacao', ['linhas' => $linhas]);}我要在其中顯示投票成員,電子郵件和model_name的刀片文件<table class="table table-hover">    <thead>      <tr>        <th>Cód Votante</th>        <th>Nome Votante</th>        <th>Modelo Escolhida(Cód)</th>        <th>e-mail</th>      </tr>    </thead>    @foreach ($linhas as $linha)    <tr>      <td> {{ $linha->id }} </td>      <td> {{ $linha->nome }} </td>      <td> {{ $linha->candidata->nome }} </td>      <td> {{ $linha->email }} </td>    @endforeach我要記錄投票項,并從該數(shù)據(jù)在另一個窗口中顯示投票者的姓名,電子郵件和模型的名稱。當(dāng)前整個列表工作正常,唯一的障礙是顯示模板的名稱,而不是在投票表中注冊的“ id”。
查看完整描述

2 回答

?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗 獲得超6個贊

如果您的父模型不使用id作為其主鍵,或者您希望將子模型連接到其他列,則可以將第三個參數(shù)傳遞給belongsTo方法,以指定父表的自定義鍵


public function candidata() 

{

    return $this->belongsTo('App\Candidata','voto','id');

}

檢查此鏈接


查看完整回答
反對 回復(fù) 2021-05-28
  • 2 回答
  • 0 關(guān)注
  • 211 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號