php - 使用外键 laravel 从另一个表中获取数据
问题描述
我有 2 张桌子attendance
和student
. 我正在尝试使用表中的外键stud_name
从student
表中检索我的。我有一个控制器,它返回模型中所有结果的视图。我还在模型和模型中添加了关系,但是每当我尝试访问视图时,我都会遇到错误异常Trying to get property 'stud_name' of non-object。任何人都可以帮助我?student
attendance
attendance
student
attendance
考勤控制器
<?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)
{
//
}
}
学生.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');
}
}
考勤.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');
}
刀片文件
@foreach($attendance as $att)
<tr>
<td>{{$att->stud_Id->stud_name}}</td>
//Other Data
</tr>
@endforeach
附加信息
所有主键都命名为“id”的原因是因为我使用的是 voyager,它不允许覆盖主键名称。
dd($出席)
解决方案
我认为您需要在这行代码中进行更改
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
考勤.php
public function student()
{
return $this->belongsTo('App\Student','stud_id');
}
如果您有任何疑问,请告诉我。
推荐阅读
- javascript - 如何在 puppeteer 上等待消失微调器?
- python - 如何使用 requests.post python 模块登录网站
- javascript - 如何通过 ReactJs 将巨大的 Json 转换为数组
- python - 如何使用beautifulsoup仅提取文本?
- c - C - 使用 scanf 读取特殊字符,如“ã”
- c++ - 冒泡排序 - 如何使用它?
- php - Symfony HttpClient GET 请求具有多个具有相同名称的查询字符串参数
- json - Golang 中的类型转换
- c++ - QUDPSocket 无法从远程以太网接收
- angular - 如何使用 Firebase 功能部署 Angular 9 通用应用程序