Php 为foreach()提供的laravel一对多无效参数
您好,我需要帮助来解决此问题。 我正试图让“一对多”关系的价值进入视野。但我得到错误“为foreach()提供的参数无效” 供应商模型Php 为foreach()提供的laravel一对多无效参数,php,laravel,laravel-6,laravel-5.8,laravel-7,Php,Laravel,Laravel 6,Laravel 5.8,Laravel 7,您好,我需要帮助来解决此问题。 我正试图让“一对多”关系的价值进入视野。但我得到错误“为foreach()提供的参数无效” 供应商模型 <?php namespace App; use App\Purchase; use Illuminate\Database\Eloquent\Model; class Supplier extends Model { protected $table = 'suppliers'; protected $fillable
<?php
namespace App;
use App\Purchase;
use Illuminate\Database\Eloquent\Model;
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\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Supplier;
use App\Billproduct;
use App\Purchase;
use App\Item_list;
use Illuminate\Support\Facades\Auth;
class PurchaseController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
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'));
}
}
看法
SL号
账单号
账单日期
供应品名称
票据金额
@foreach($key=>$entrys作为条目)
{{$key+1}}
{{$entrys->bill_no}
{{$entrys->bill_date}
{{$entrys->supplier_name}
{{$entrys->账单总额}
@endforeach
我想你忘了给外交部打电话了。
您需要调用:$entry->purchases或$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>
我不明白你能发布你的答案吗?我返回了“return Supplier::find($suppliers\u master\u id)->purchases”;这是我得到的值,但当我使用foreach时,它显示错误我修改了我的代码,作为你的答案。get error“尝试获取非对象的属性‘purchases’”,你可能需要使用findOrFail()而不是find,或者处理找不到有效入口的情况。您能否在回答中更新公共函数purchaseEntryList($suppliers\u master\u id){$entry=Supplier::where('delete\u status','not\u DELETED')->findOrFail($suppliers\u master\u id);返回视图('purchase.purchase entry list',compact('entry');}谢谢
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Supplier;
use App\Billproduct;
use App\Purchase;
use App\Item_list;
use Illuminate\Support\Facades\Auth;
class PurchaseController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
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>
<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'));
}