我当时正在玩我的第一个Laravel项目,上面的问题可能有点令人困惑,但让我解释一下:-
I was playing around with my first Laravel project and my question above might be a little bit confusing but let me explain:-
我有3张桌子(实际上我有更多张桌子,但让我们忽略它),我有standards,stddetails& sections如下所示:-
I have 3 tables (actually I have more, but lets ignore it), I have standards, stddetails & sections as shown:-
因此,外键对应如下:-
So the foreign key corresponds are below:-
-
表standards中的
-
列stdPK =表中的列stdFK stddetails
column stdPK from table standards = column stdFK from table stddetails
列ssctionPK = 中的列ssctionFK 表stddetails
场景是这样的:-
让我们说我在表standards 中的stdPK上具有一个$id要匹配. 使用该$id ,我需要从表sections 中获取所有数据.问题是,我找不到正确的查询,因为standards和sections表都只与stddetails表链接.
Lets say I have an $id to be match on stdPK from 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 standards and sections tables only linked with stddetails table.
目前我在web.php中的查询是:-
Currently my query in my web.php is this:-
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.
更新1:-
我在每个模型中都设置了雄辩的关系,并使用Laravel的Eloquent方法检索数据:-
I have set the eloquent relationship in each model and uses Laravel's Eloquent method in retrieving the data:-
$stdsec = Stddetail::with('section')->find($id);所有数据均从stddetails& sections表,现在的问题是在显示页面的sections表中的sections表中的列ssctionName中显示数据很困难,因为它返回错误.
All data is retrieved from both stddetails & sections tables, the problem now is difficulty on displaying data from column ssctionName in sections table in a display page as it return an error.
显示页面上的相关代码如下:-
The related code on the display page is below:-
@foreach ($stdsec as $task) {{strtoupper($task->ssctionName)}} @endforeach显示的错误:-
我认为雄辩的方法很好,现在显示部分给我带来麻烦.关于如何解决这个问题有什么想法吗?
I think the eloquent method is good, now the display part is giving me trouble. Any ideas on how to solve this?
更新2:-
这是每个表格的模型:-
Here's the models for each table:-
表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'); } }表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'); } }表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'); } }推荐答案
看看雄辩的关系: laravel/docs/5.4/eloquent-relationships
您应该能够在模型(表)之间设置适当的关系(hasMany,belongsTo等),并通过一个命令获取所有数据(Eloquent将为您创建所需的查询).
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).
不相关的是,我建议您改善命名约定.很难理解带有所有首字母缩写词和简称的事件基本链接
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)从一个表获取数据,该表的ID与另一个链接到该表的表相对应
发布评论