首页 > 解决方案 > 使用外键 laravel 从另一个表中获取数据

问题描述

我有 2 张桌子attendancestudent. 我正在尝试使用表中的外键stud_namestudent表中检索我的。我有一个控制器,它返回模型中所有结果的视图。我还在模型和模型中添加了关系,但是每当我尝试访问视图时,我都会遇到错误异常Trying to get property 'stud_name' of non-object。任何人都可以帮助我?studentattendanceattendancestudentattendance

考勤控制器

<?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($出席)

标签: phplaravel

解决方案


我认为您需要在这行代码中进行更改

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

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


推荐阅读