我有2个表attendance
和student
。我试图通过使用stud_name
表中的student
外键从student
表中检索attendance
。我有一个控制器,它返回来自attendance
模型的所有结果的视图。我还在student
和attendance
模型中都添加了关系,但是每当我尝试访问视图时,我都会得到一个错误例外,即试图获取非对象的属性'stud_name' >。有人可以帮助我吗?
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
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
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($ attenceance)
答案 0 :(得分:1)
我认为您想尝试:
@foreach($attendance as $att)
<tr>
<td>{{$att->student->stud_name}}</td>
//Other Data
</tr>
@endforeach
还要签出docs。
此外:我个人不会在名称为student
的模型stud_name
上调用该属性。我将其称为name
:应该从模型名称与名称name
的组合中清楚地知道它是学生姓名。
答案 1 :(得分:0)
我认为您需要在这一行代码中进行更改
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
public function student()
{
return $this->belongsTo('App\Student','stud_id');
}
如果您有任何疑问,请告诉我。