[英]Fetching Data from another table using foreign key laravel
我有2張桌子的attendance
和student
。 我正在嘗試使用attendance
表中的student
外鍵從student
表中檢索我的stud_name
。 我有一個控制器,可以從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($考勤)
我認為您想嘗試:
@foreach($attendance as $att)
<tr>
<td>{{$att->student->stud_name}}</td>
//Other Data
</tr>
@endforeach
還請檢查文檔 。
另外:我個人不會調用名稱為stud_name
的模型student
的屬性。 我將其name
:從模型名稱與name name
組合使用時,應該清楚知道這是一個學生的name
。
我認為您需要在這一行代碼中進行更改
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');
}
如果您有任何疑問,請告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.