使用外键 Laravel 从另一个表中获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51105604/
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
Fetching Data from another table using foreign key laravel
提问by Dexter Siah
I have 2 tables attendance
and student
. I am trying to retrieve my stud_name
from student
table by using the student
foreign key in attendance
table. I have a controller that return the view of all the results from attendance
model. I have also added the relationship in both student
and attendance
model but whenever i try accessing the view i got an error exception of Trying to get property 'stud_name' of non-object. Anyone able to help me?
我有 2 张桌子attendance
和student
. 我正在尝试使用表中的外键stud_name
从student
表中检索我的表。我有一个控制器,它返回模型中所有结果的视图。我还在两者和模型中添加了关系,但是每当我尝试访问视图时,我都会收到一个错误异常,尝试获取非对象的属性“stud_name”。任何人都可以帮助我吗?student
attendance
attendance
student
attendance
AttendanceController
考勤控制器
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Attendance;
class GenerateReportController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$attendance = Attendance::with('student')->get();
return view('generate')->with('attendance',$attendance);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
Student.php
学生.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
//Table
protected $table = 'students';
//Primary Key
public $primaryKey = 'id';
//Timestamps
public $timestamp = true;
public function programme(){
return $this->belongsTo('App\Programme');
}
public function attendance(){
return $this->hasMany('App\Attendance');
}
}
Attendance.php
考勤.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Attendance extends Model
{
//Table
protected $table = 'attendance';
//Primary Key
public $primaryKey = 'id';
//Timestamps
public $timestamp = true;
protected $fillable = [
'att_status','date','time',
];
public function student()
{
return $this->belongsTo('App\Student','s_id');
}
Blade File
刀片文件
@foreach($attendance as $att)
<tr>
<td>{{$att->stud_Id->stud_name}}</td>
//Other Data
</tr>
@endforeach
ADDITIONAL INFOMATION
附加信息
The reason all the primary key is named 'id' is because i am using voyager and it doesnt allow overriding of primary key name.
所有主键都被命名为“id”的原因是因为我正在使用 voyager 并且它不允许覆盖主键名称。
dd($attendance)
dd($出勤)
采纳答案by Avinash Singh Rathi
I think you need to make changes in this line of code
我认为您需要在这行代码中进行更改
public function index()
{
$attendance = Attendance::with('student')->get();
return view('generate')->with('attendance',$attendance);
}
@foreach($attendance as $att)
<tr>
<td>{{$att->student->stud_name}}</td>
//Other Data
</tr>
@endforeach
Attendance.php
考勤.php
public function student()
{
return $this->belongsTo('App\Student','stud_id');
}
Please let me know if you have any queries.
如果您有任何疑问,请告诉我。
回答by Niellles
I think you wanna try:
我想你想尝试:
@foreach($attendance as $att)
<tr>
<td>{{$att->student->stud_name}}</td>
//Other Data
</tr>
@endforeach
Also check out the docs.
另请查看文档。
In addition: I personally wouldn't call the property on the model student
that holds the name stud_name
. I'd call it name
: the fact that it's a students name should be clear from the model name in combination with the name name
.
另外:我个人不会在student
拥有 name的模型上调用属性stud_name
。我称之为name
:它是一个学生的名字这一事实应该从模型名称和 name 的组合中清楚地看出name
。