2015-11-09 22 views
3

我有一個採集控制器一個在Laravel5一對多的關係不正常

看起來像這樣

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

Paymentrecieved

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class paymentrecieved extends Model 
{ 
    // 
    protected $table = 'paymentrecieved'; 

    public function invoice(){ 

      return $this->belongsTo('App\Invoice'); 

     } 


} 

所以對於每一個發票可能有多個付款。

所以在我的getPayment方法,我寫這樣的查詢。

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

它讓我從發票表的詳細信息。但它沒有得到我任何付款詳細

任何一個知道爲什麼會這樣,據我,我也做了關係正確

感謝

回答

1

您使用Id(大寫)主鍵,但Invoice模型正在尋找id(小寫)。

你應該定義在Invoice模型這樣的主鍵:

protected $primaryKey = 'Id'; 
+0

'調用未定義的方法照亮\數據庫\查詢\生成器::負載()'我收到此錯誤 – Vikram

+0

我要獲得發票的所有付款,其ID爲$ id – Vikram

+0

我編輯了我的答案,請嘗試! – kotapeter