Laravel 5.7 Eloquent 上市关系
Laravel 5.7 Eloquent Relation for listing
我正在尝试通过连接获取数据集,我想使用 eloquent 自动完成,无需手动查询。我也做了关系,但是当我尝试列出连接的数据时,它以某种方式相反。
Laravel 版本 5.7
MySQL 数据库
已更新,添加了数据库架构。
my_tab3s table:
mytab1 table:
错误:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column
'myTab1.my_tab3_id' in 'where clause' (SQL: select * from myTab1
where myTab1
.my_tab3_id
in (1, 2))"
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 索引方法
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
没有数据库很难说,但我认为你颠倒了你的关系,myTab3
应该是 belongsTo
,myTab1
[=15 应该是 hasOne
=]
编辑:
试试 :
public function myTab1(){
return $this->belongsTo('App\myTab1', 'myTab1_id');
}
我正在尝试通过连接获取数据集,我想使用 eloquent 自动完成,无需手动查询。我也做了关系,但是当我尝试列出连接的数据时,它以某种方式相反。
Laravel 版本 5.7 MySQL 数据库
已更新,添加了数据库架构。
my_tab3s table:
mytab1 table:
错误:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'myTab1.my_tab3_id' in 'where clause' (SQL: select * from
myTab1
wheremyTab1
.my_tab3_id
in (1, 2))"
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 索引方法
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
没有数据库很难说,但我认为你颠倒了你的关系,myTab3
应该是 belongsTo
,myTab1
[=15 应该是 hasOne
=]
编辑:
试试 :
public function myTab1(){
return $this->belongsTo('App\myTab1', 'myTab1_id');
}