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');
}
}