gpt4 book ai didi

php - 使用外键laravel从另一个表中获取数据

转载 作者:行者123 更新时间:2023-12-02 00:32:53 25 4
gpt4 key购买 nike

我有 2 个表 attendancestudent。我正在尝试使用 attendance 表中的 student 外键从 student 表中检索我的 stud_name。我有一个 Controller ,它返回来自 attendance 模型的所有结果的 View 。我还在 studentattendance 模型中添加了关系,但每当我尝试访问 View 时,我都会收到错误异常 Trying to get property 'stud_name' of非对象。谁能帮帮我?

考勤 Controller

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

Blade 文件

@foreach($attendance as $att)
<tr>
<td>{{$att->stud_Id->stud_name}}</td>
//Other Data
</tr>
@endforeach

附加信息

学生表 student table

考勤表 Attendance Table

所有主键都命名为“id”的原因是因为我使用的是 voyager,它不允许覆盖主键名称。

dd($attendance)

最佳答案

我认为你需要对这行代码进行修改

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

如果您有任何疑问,请告诉我。

关于php - 使用外键laravel从另一个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51105604/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com