我正试图得到"一对多";视图中的关系值。但是我得到了错误";为foreach((提供的参数无效">
供应商型号
<?php
namespace App;
use AppPurchase;
use IlluminateDatabaseEloquentModel;
class Supplier extends Model
{
protected $table = 'suppliers';
protected $fillable = ['party_id','suppliers_master_id','suppliers_unic_id','supplier_name','email','phone','address','city','state','pincode','GSTIN','delete_status'];
public function purchases()
{
return $this->hasMany(Purchase::class, 'suppliers_master_id', 'suppliers_master_id');
}
}
购买型号
<?php
namespace App;
use AppPurchase;
use IlluminateDatabaseEloquentModel;
class Purchase extends Model
{
protected $table = 'purchases';
protected $fillable =
['suppliers_master_id','suppliers _unic_id','party_id','ssupplier_name','GSTIN','bill_no','ill_date','bill_entry_date','total_bill_amount','hone','incode','state','address','delete_status','part_no'];
}
控制器
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesHash;
use AppSupplier;
use AppBillproduct;
use AppPurchase;
use AppItem_list;
use IlluminateSupportFacadesAuth;
class PurchaseController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return IlluminateHttpResponse
*/
public function purchaseentrylists($suppliers_master_id)
{
$entry = Supplier::where('delete_status','!=','NOT_DELETED')->find($suppliers_master_id);
return view('purchase.purchase-entry-list',compact('entry'));
}
}
路线
Route::get('purchas/eentry/lists{id}', 'PurchaseController@purchaseentrylists')- >name('purchaseentrylists');
查看
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>SL-No</th>
<th>BILL NO</th>
<th>BILL DATE</th>
<th>SUPPLIES NAME</th>
<th>BILL AMOUNT</th>
</tr>
</thead>
<tbody>
@foreach ($entry as $key=>$entrys)
<td>{{ $key + 1 }}</td>
<td>{{ $entrys->bill_no }}</td>
<td>{{ $entrys->bill_date }}</td>
<td>{{ $entrys->supplier_name }}</td>
<td>{{ $entrys->total_bill_amount }}</td>
</tbody>
@endforeach
</table>
我想你忘了实际调用对外关系。您需要调用:$entry->purchases or $entry->purchases()->get()
来获取供应商的采购集合,然后可以对其进行迭代。
<tbody>
@foreach ($entry->purchases as $key=>$entrys)
<td>{{ $key + 1 }}</td>
<td>{{ $entrys->bill_no }}</td>
<td>{{ $entrys->bill_date }}</td>
<td>{{ $entrys->supplier_name }}</td>
<td>{{ $entrys->total_bill_amount }}</td>
@endforeach
</tbody>
在您的控制器中:
public function purchaseentrylists($suppliers_master_id)
{
$entry = Supplier::where('delete_status','NOT_DELETED')
->findOrFail($suppliers_master_id);
return view('purchase.purchase-entry-list',compact('entry'));
}