[英]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');
}
}
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();
它從發票表中獲取詳細信息。 但它沒有給我任何付款細節
任何人都知道為什么會發生這種情況,據我說我已經正確地完成了這段關系
謝謝
您使用Id
(使用大寫)作為主鍵,但Invoice模型正在查找id
(使用小寫)。
您應該在Invoice
模型中定義主鍵,如下所示:
protected $primaryKey = 'Id';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.