(Laravel) 从一个表中获取数据,该表的 ID 与与该表链接的另一个表相对应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42268591/
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
(Laravel) Get data from a table which an ID correspond with another table that link with that table
提问by Kaydarin
I was playing around with my first Laravel project and my question above might be a little bit confusing but let me explain:-
我正在玩我的第一个 Laravel 项目,我上面的问题可能有点令人困惑,但让我解释一下:-
I have 3 tables (actually I have more, but lets ignore it), I have standards, stddetails& sectionsas shown:-
我有 3 个表(实际上我有更多,但让我们忽略它),我有standards, stddetails§ions如图所示:-
So the foreign key corresponds are below:-
所以外键对应如下:-
column
stdPKfrom tablestandards= columnstdFKfrom tablestddetailscolumn
ssctionPKfrom tablesections= columnssctionFKfrom
tablestddetails
柱
stdPK从表standards=列stdFK从表stddetails柱
ssctionPK从表sections=列ssctionFK从
表stddetails
And the scenario is this:-
场景是这样的:-
Lets say I have an $idto be match on stdPKfrom table standards. With that $id, I am required to get all data from table sections. The problem is, I can't find a right query for that as both standardsand sectionstables only linked with stddetailstable.
假设我有一个$id要匹配的stdPK表standards。有了这个$id,我需要从表中获取所有数据sections。问题是,我找不到正确的查询,因为表standards和sections表都只与stddetails表相关联。
Currently my query in my web.phpis this:-
目前我在我的查询web.php是这样的:-
Route::get('getstddtl/{id?}', function ($id) {
$stdsec = Section::
leftJoin('stddetails', 'ssctionFK', '=', 'ssctionPK')
->join('standards', function($join) use ($id){
$join->on('stdPK', '=', 'stdFK')
->where('stdFK', $id);
});
return view('standarddtl', [
'stdsec' => $stdsec
]);
});
I thought it should be easy, boy... I was wrong... I hoped someone can help me with this because my brain have very limited thinking capacity.
我认为这应该很容易,男孩......我错了......我希望有人能帮助我解决这个问题,因为我的大脑思维能力非常有限。
UPDATE 1:-
更新 1:-
I have set the eloquent relationship in each model and uses Laravel's Eloquent method in retrieving the data:-
我已经在每个模型中设置了 eloquent 关系并使用 Laravel 的 Eloquent 方法来检索数据:-
$stdsec = Stddetail::with('section')->find($id);
All data is retrieved from both stddetails& sectionstables, the problem now is difficulty on displaying data from column ssctionNamein sectionstable in a display page as it return an error.
所有数据是从两个检索stddetails&sections表,现在的问题是在从列显示数据的困难ssctionName在sections表中的显示页面,因为它返回一个错误。
The related code on the display page is below:-
显示页面的相关代码如下:-
@foreach ($stdsec as $task)
{{strtoupper($task->ssctionName)}}
@endforeach
The error shown:-
显示的错误:-
I think the eloquent method is good, now the display part is giving me trouble. Any ideas on how to solve this?
我觉得雄辩的方法很好,现在展示部分给我带来了麻烦。关于如何解决这个问题的任何想法?
UPDATE 2:-
更新 2:-
Here's the models for each table:-
这是每个表的模型:-
Table standardsas Standardmodel:-
表standards作为标准模型:-
namespace App;
use Illuminate\Database\Eloquent\Model;
class Standard extends Model
{
public $primaryKey = 'stdPK';
public function stddetail()
{
return $this->hasMany(Stddetail::class, 'stdFK', 'stdPK');
}
}
Table stddetailsas Stddetailmodel:-
表stddetails作为Stddetail模型:-
namespace App;
use Illuminate\Database\Eloquent\Model;
class Stddetail extends Model
{
public $table = "stddetails";
public $primaryKey = 'sdtlPK';
protected $fillable = ['stdFK', 'sdtlPgNo', 'sdtlLnNo', 'sdtlText', 'sdtlShrtnote', 'sdtlSchm', 'svrFK', 'ssctionFK', 'sdtlRefLbl'];
public function standard()
{
return $this->belongsTo(Standard::class, 'stdFK');
}
public function section()
{
return $this->belongsTo(Section::class, 'ssctionFK');
}
}
Table sectionsas Sectionmodel:-
表sections作为截面模型:-
namespace App;
use Illuminate\Database\Eloquent\Model;
class Section extends Model
{
public $primaryKey = 'ssctionPK';
public function stddetails()
{
return $this->hasMany(Stddetail::class, 'ssctionFK', 'ssctionPK');
}
}
回答by Nerman
Take a look at eloquent relationships: https://laravel.com/docs/5.4/eloquent-relationships
看看雄辩的关系:https: //laravel.com/docs/5.4/eloquent-relationships
You should be able to set proper relationships(hasMany, belongsTo, etc) between your models (tables) and get all data with a single command (Eloquent will create needed queries for you).
您应该能够在模型(表)之间设置适当的关系(hasMany、belongsTo 等)并使用单个命令获取所有数据(Eloquent 将为您创建所需的查询)。
On an unrelated note, I would suggest improving your naming convention. It is really hard to understand event basic links with all acronyms and short names used
在一个不相关的说明中,我建议改进您的命名约定。使用所有首字母缩略词和短名称来理解事件基本链接真的很难


![laravel SQLSTATE[42000]:语法错误或访问冲突:1075 表定义不正确;只能有一个自动列,并且必须将其定义为键](/res/img/loading.gif)