使用外键 Laravel 从另一个表中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51105604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:51:24  来源:igfitidea点击:

Fetching Data from another table using foreign key laravel

phplaravel

提问by Dexter Siah

I have 2 tables attendanceand student. I am trying to retrieve my stud_namefrom studenttable by using the studentforeign key in attendancetable. I have a controller that return the view of all the results from attendancemodel. I have also added the relationship in both studentand attendancemodel but whenever i try accessing the view i got an error exception of Trying to get property 'stud_name' of non-object. Anyone able to help me?

我有 2 张桌子attendancestudent. 我正在尝试使用表中的外键stud_namestudent表中检索我的表。我有一个控制器,它返回模型中所有结果的视图。我还在两者和模型中添加了关系,但是每当我尝试访问视图时,我都会收到一个错误异常,尝试获取非对象的属性“stud_name”。任何人都可以帮助我吗?studentattendanceattendancestudentattendance

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

<?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

<?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 File

刀片文件

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

ADDITIONAL INFOMATION

附加信息

Student Table student table

学生桌 学生桌

Attendance Table Attendance Table

考勤表 考勤表

The reason all the primary key is named 'id' is because i am using voyager and it doesnt allow overriding of primary key name.

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

dd($attendance)

dd($出勤)

采纳答案by Avinash Singh Rathi

I think you need to make changes in this line of code

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

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

考勤.php

public function student()
{
    return $this->belongsTo('App\Student','stud_id');
}

Please let me know if you have any queries.

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

回答by Niellles

I think you wanna try:

我想你想尝试:

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

Also check out the docs.

另请查看文档

In addition: I personally wouldn't call the property on the model studentthat holds the name stud_name. I'd call it name: the fact that it's a students name should be clear from the model name in combination with the name name.

另外:我个人不会在student拥有 name的模型上调用属性stud_name。我称之为name:它是一个学生的名字这一事实应该从模型名称和 name 的组合中清楚地看出name