我正在尝试使用join获取数据集,而我想使用雄辩而自动地进行操作,而无需手动查询。我也做过这种关系,但是当我尝试列出联接的数据时,不知何故它会这样做。

Laravel 5.7版
MySQL数据库

UPDATED,添加了数据库架构。
my_tab3s表:
php - Laravel 5.7 Eloquent 关系上市-LMLPHP

mytab1表:
php - Laravel 5.7 Eloquent 关系上市-LMLPHP

错误:


  “ SQLSTATE [42S22]:找不到列:1054未知列
  'where子句'中的'myTab1.my_tab3_id'(SQL:从myTab1中选择*
  其中(1,2)中的myTab1my_tab3_id


myTab3.php

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class myTab3 extends Model
{
    //
    protected $table = 'my_tab3s';
    public $timestamps = true;


    protected $fillable = [
    'coolField',
    'muhCurrentDate',
    'rast',
    'myTab1_id'
  ];


    public function myTab1(){
        return $this->hasOne('App\myTab1');
    }
}


myTab1.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class myTab1 extends Model
{
    //
    public function scopeBigger($query){
        return $query->where('id','>','1');
    }
    public function scopeHasLetterZ($query){
        return $query->where('someField','like','%z%');
    }

    public function gimmeAll(){
        return myTab1::All();
    }
    protected $table = 'myTab1';
    public $timestamps = true;

    protected $fillable = [
    'someField'
  ];


    public function myTab3(){
        return $this->belongsTo('App\myTab3');
    }
}


myTab3Controller.php INDEX方法

 public function index()
    {

        /*$dataSet = myTab3::All();*/
        //$dataSet->myTab1()->get();
        $dataSet = myTab3::with('myTab1')->get();
        return view('myTab3.index',compact('dataSet'));
    }


查看零件

@foreach($dataSet as $data)
        <tr>
            <td>{{$data->id}}</td>
            <td>{{$data->coolField}}</td>
            <td>{{$data->muhCurrentDate}}</td>
            <td>{{$data->created_at}}</td>
            <td>{{$data->updated_at}}</td>
            <td>{{$data->rast}}</td>
            <td>{{$data->myTab1->someField}}</td>
        </tr>
        @endforeach

最佳答案

没有数据库很难说,但是我认为您颠倒了您的关系,对于belongsTo应该是myTab3,对于hasOne应该是myTab1

编辑:

尝试:

public function myTab1(){
    return $this->belongsTo('App\myTab1', 'myTab1_id');
}

10-08 16:15