Laravel5中的一对多关系无法正常工作


one to many relationship in Laravel5 is not working properly

我有一个采集控制器

看起来像这个

<?php
namespace App'Http'Controllers;
use Illuminate'Http'Request;
use Illuminate'Support'Facades'Auth;
use App'Http'Requests;
use App'Http'Controllers'Controller;
use App'Http'Middleware'Role;
use Illuminate'Support'Facades'Input;
use App'User;
use App'Invoice;
use Session;
use Validator;

class CollectionController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
  public function __construct(){
    $this->middleware('role:collector'); // replace 'collector' with whatever role you need.
}
   public function getPayment($invoiceid){

         $id =$invoiceid;
         $invoice=Invoice::where('Id', $id)->with(['payments'])->get();

         return View('collectionmodule/payment')->with(array('invoice'=>$invoice));
 }

}

然后我有以下型号发票

<?php
namespace App;
use Illuminate'Database'Eloquent'Model;
class Invoice extends Model
{
    //
     protected $table = 'event_invoice';
     public function payments(){
        return $this->hasMany('App'paymentrecieved','invoice_id');
    }
    public function comments(){
        return $this->hasMany('App'comments','invoice_id');
    }
}

收到的付款

<?php
namespace App;
use Illuminate'Database'Eloquent'Model;
class paymentrecieved extends Model
{
    //
    protected $table = 'paymentrecieved';
    public function invoice(){
            return $this->belongsTo('App'Invoice');
        }

}

因此,每张发票都可能有多次付款。

所以在我的getPayment方法中,我写的Query是这样的。

$invoice=Invoice::where('Id', $id)->with(['payments'])->get();

它给我带来了发票明细表。但它没有给我任何付款详细信息

任何人都知道为什么会发生这种情况,根据我的说法,我已经做好了的关系

感谢

您使用Id(大写)作为主键,但Invoice模型正在查找id(小写)。

您应该在Invoice模型中这样定义主键:

protected $primaryKey = 'Id';