【问题标题】:Fetching Data from another table using foreign key laravel使用外键 laravel 从另一个表中获取数据
【发布时间】:2018-12-08 21:03:08
【问题描述】:

我有 2 张桌子 attendancestudent。我正在尝试使用attendance 表中的student 外键从student 表中检索我的stud_name。我有一个控制器,它返回来自attendance 模型的所有结果的视图。我还在studentattendance 模型中添加了关系,但是每当我尝试访问视图时,我都会遇到错误异常尝试获取非对象的属性'stud_name'。谁能帮帮我?

考勤控制器

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

【问题讨论】:

    标签: php laravel


    【解决方案1】:

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

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

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

    【讨论】:

    • 您好,我已尝试更改您添加的代码,但似乎我收到错误“方法 Illuminate\Database\Query\Builder::all 不存在。”
    • @DexterSiah 对不起我的错误,我已经更新了答案,请检查并告诉我
    • 嗨不幸的是我仍然收到错误“尝试获取非对象的属性'stud_name'。”。我添加了一些额外的图片来展示我的学生和考勤表里面的东西希望你能帮助我谢谢
    • @DexterSiah 我已经完成了更改,请检查并告诉我
    • @DexterSiah 请检查错误日志文件,因为此答案没有问题
    【解决方案2】:

    我想你想试试:

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

    还可以查看docs

    另外:我个人不会将模型上的属性称为student 名称为stud_name。我称它为name:从模型名称与名称name 结合起来,应该可以清楚地看出这是一个学生的名字。

    【讨论】:

    • @Nielles 抱歉,但我确保你的意思是不将其命名为 stud_name 而不是名称。因为我使用的是 laravel + voyager,所以我担心如果我在模型中所做的任何更改可能会影响我的其他表
    • @DexterSiah 首先,这个解决方案有效吗?第二:我的意思是:你有一个模型叫student。这个学生有一个名字,对我来说,将此属性(在模型和数据库中)称为name 而不是stud_name 是合乎逻辑的。从这是一个学生的名字这一事实中应该可以清楚地看出这是一个stud(ents)_name。这更像是一种风格的东西,将来可能会很好地记住。它也可能对你当前的代码有太多的影响来改变它......尽管你应该能够使用一个体面的 IDE 最自动地重构。
    • @Nielles 不幸的是,没有解决方案对我不起作用我仍然有同样的错误,说明“试图获取非对象的属性”。好吧,我将在我的数据库中更改为name,但我将如何在模型中更改它?我是否也需要在我的所有模型中声明我的列名?很抱歉问这样一个问题,我还是个新手,并为作业做这个
    • 您不一定需要在模型中声明这些名称,eloquent(Laravel 的一部分)会自动执行此操作...您可以在检索到的模型中访问的属性会发生变化。我认为您的数据库方案(即迁移)有问题,更具体地说,外键未正确定义,导致 eloquent 无法正确定义关系。您能否发布dd($att) 的相关部分以及您创建数据库表的方式(迁移,可能/希望)。
    • 另外:“请记住,Eloquent 将自动确定 Comment 模型上正确的外键列。按照惯例,Eloquent 将采用拥有模型的“蛇形”名称并为其添加后缀_id。因此,对于这个例子,Eloquent 将假设 Comment 模型上的外键是 post_id。" 可能是文档中有趣的部分,以及 documentation on relationships 的其余部分。
    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 2016-10-07
    • 2021-08-17
    • 1970-01-01
    • 2019-05-02
    相关资源
    最近更新 更多