如何从Laravel中的外键查看表字段 [英] How to see table fields from foreign keys in Laravel
问题描述
我是Laravel的新手,语法不好.我想通过外键(该表的ID)查看另一个表的值.
https://ibb.co/pXRFRHn 您可以在这张图片中看到用户& ;班级.我想要与这些ID相关的标题.
我有表部分,用户和一个类.我使用class_id
& user_id作为节表中的外键.当我尝试显示数据时,我看到了ID,但我想使用&名称.从该ID提取的其他字段.
控制器
public function index()
{
$sections = Section::all();
$classs = Classs::all();
$users = User::all();
return view('sections.index')->with('sections', $sections)->with('classs', $classs)->with('users', $users);
}
刀片/视图
<div class="box">
<div class="box-header">
<h3 class="box-title">All Sections</h3>
</div>
<div class="box-body">
<table class="table table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>User</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
@foreach($sections as $cat)
<tr>
<td>{{$cat->title}}</td>
<td>{{$cat->class_id}}</td>
<td>{{$cat->user_id}}</td>
<td>
<button class="btn btn-info" data-mytitle="{{$cat->title}}"
data-myclassid="{{$cat->class_id}}" data-myuserid="{{$cat->user_id}}"
data-catid={{$cat->id}} data-toggle="modal" data-target="#edit">Edit
</button>
<button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal"
data-target="#delete">Delete
</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
特别是...
<td>{{$cat->class_id}}</td>
由此,我得到了班级ID,但是我也想要它的名字.
{{$cat->(class_id)->name}}"
但是,它没有用.
我已经编辑了模型
class Classs extends Model
{
//
protected $fillable = [
'id',
'title',
];
public function section()
{
return $this->belongsTo('App\Section');
}
}
部分模型
class Section extends Model
{
//
protected $fillable = [
'id',
'title',
'class_id',
'user_id',
];
public function classs()
{
return $this->hasMany('App\Classs');
}
}
您所要做的就是
data-myclassid="{{$cat->class_id}}"
而不是尝试这个
data-myclassid="{{ \Illuminate\Support\Facades\DB::table('class')->where('id',$cat->class_id)->value('name')}}"
我假设您的表名是class,因为您想显示类名,所以在该数据库类名(列名)中将是简单的"name",这就是为什么我将name放在值字段中. >
I'm new at Laravel and not good with syntax. I want to see the values of another table through the foreign key(id of that table).
https://ibb.co/pXRFRHn You can see in this picture I get ids under user & class. I want the titles associated with these ids.
I have tables sections,users and a class. I use class_id
& user_id as the foreign key in the section table. When I try to show data, I see the id, but I want the name & other fields extracted from that id.
Controller
public function index()
{
$sections = Section::all();
$classs = Classs::all();
$users = User::all();
return view('sections.index')->with('sections', $sections)->with('classs', $classs)->with('users', $users);
}
Blade/View
<div class="box">
<div class="box-header">
<h3 class="box-title">All Sections</h3>
</div>
<div class="box-body">
<table class="table table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Class</th>
<th>User</th>
<th>Modify</th>
</tr>
</thead>
<tbody>
@foreach($sections as $cat)
<tr>
<td>{{$cat->title}}</td>
<td>{{$cat->class_id}}</td>
<td>{{$cat->user_id}}</td>
<td>
<button class="btn btn-info" data-mytitle="{{$cat->title}}"
data-myclassid="{{$cat->class_id}}" data-myuserid="{{$cat->user_id}}"
data-catid={{$cat->id}} data-toggle="modal" data-target="#edit">Edit
</button>
<button class="btn btn-danger" data-catid={{$cat->id}} data-toggle="modal"
data-target="#delete">Delete
</button>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
Specifically...
<td>{{$cat->class_id}}</td>
From this, I get the class id, but I want its name also.
{{$cat->(class_id)->name}}"
However, it didn't work.
I have edited Models
class Classs extends Model
{
//
protected $fillable = [
'id',
'title',
];
public function section()
{
return $this->belongsTo('App\Section');
}
}
Section Model
class Section extends Model
{
//
protected $fillable = [
'id',
'title',
'class_id',
'user_id',
];
public function classs()
{
return $this->hasMany('App\Classs');
}
}
All you have to do is
data-myclassid="{{$cat->class_id}}"
rather than this try this
data-myclassid="{{ \Illuminate\Support\Facades\DB::table('class')->where('id',$cat->class_id)->value('name')}}"
I am assuming that your table name is class as you want to show class name so in that database class name (column name) would be simple 'name' , that why I put name in value field....
这篇关于如何从Laravel中的外键查看表字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!