php - 如何获取与其他表的元素id和帖子相关的另一个表的记录?

标签 php mysql laravel

我正在尝试在支付页面上显示支付表信息。

payments{student_id, course_id, payment_type_id}
students{id, name} 
courses{id, name} 
payment_types{id,name}

对于列 > course_id(在付款表中),它在付款页面中显示类(class)名称。但对于其他两列,它没有发布。我的错误在哪里。提前谢谢您!

这是我的 Payment.php 模型

protected $fillable = [
    'student_id',
     'course_id',
     'payment_types_list_id',
];

protected $filter = [
   'page' => false,
   '_token' => false,
   'student_id' => '=',
   'course_id'  => '=',
   'payment_types_list_id' => '=',
];

public function Payment() {
    return $this->hasOne('App\Payment');
}

public function course() {
   return $this->belongsTo('App\Course');
}

public function students() {
    return $this->belongsToMany('App\Student');
}

public function PaymentType() {
   return $this->belongsTo('App\PaymentType');
}

这是payments_table

 public function up() {
      Schema::create('payments', function(Blueprint $table)
      {
          $table->increments('id');
          $table->integer('student_id')->unsigned()->index();
          $table->foreign('student_id')->references('id')->on('students');
          $table->integer('course_id')->unsigned()->index();
          $table->foreign('course_id')->references('id')->on('courses');
          $table->integer('payment_types_list_id');
          $table->foreign('payment_types_list_id')
                ->references('id')->on('payment_types_list');
          $table->timestamps();
      });
 }

这是PaymentController.php

public function index(Request $request) {
        $filter = $request->input();
        $sort = $request->query();
        $payments = Payment::filter($filter)->sort($sort)->paginate(10);
        foreach(PaymentType::all() as $payment_type) {
            $payment_types[$payment_type->id] = $payment_type->name;
        }
        foreach(Course::all() as $course) {
            $courses[$course->id] = $course->name;
        }
        foreach(Student::all() as $student) {
            $students[$student->id] = $student->name;
        }
        return view('payments.index', compact('payments', 'filter','courses','students', 'payment_types'));
    }

这里是 Payment/index.php

@forelse ($payments as $key => $payment)
   <tr>
      <td>{{ ++$key }}</td>
      <td>{{ $payment->student['name'] }}</td>
      <td>{{ $payment->course['name']}}</td>
      <td>{{$payment->payment_types_list['name']}}</td> 
   </tr> 
@endforelse

最佳答案

所以问题出在关系的名称上。当您定义一个关系时,应该使用相同的定义名称访问它。

例如——

如果您的关系名称是 PaymentType,那么您应该访问该关系:

{{ $payment->PaymentType->name }}

{{ $payment->PaymentType['name'] }}

对于学生关系,它应该是 belongsTo 而不是 belongsToMany 所以 -

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

然后你可以访问它:

{{ $payment->student->name }}

Docs

关于php - 如何获取与其他表的元素id和帖子相关的另一个表的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43344361/

相关文章:

php - fopen php mysql import - sql 中的空行

php - 如何回显列的名称

php - 将数组从 php 5.3 转换为 php 4

mysql - 如何提取具有最大值的行?

mysql - 从边界坐标列创建mysql多边形列

php - 从 Laravel 5.1 中的通用数据库查询获取 Eloquent 模型的实例

php - 在 woocommerce 管理设置面板中重命名电子邮件通知标题

Mysql count 给我带来了非常糟糕的性能,我做错了吗?

php - Laravel 后退按钮

laravel - 如何返回带有Flash消息的 View ?