如何解决laravel为foreach提供的一对多无效参数
您好,我需要帮助解决此问题。
我正在尝试获得“一对多”关系。但出现错误“为foreach()提供了无效的参数”
供应商模型
<?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');
}
}
购买模型
<?php
namespace App;
use App\Purchase;
use Illuminate\Database\Eloquent\Model;
class Purchase extends Model
{
protected $table = 'purchases';
protected $fillable =
['suppliers_master_id','suppliers_unic_id','party_id','supplier_name','GSTIN','bill_no','bill_date','bill_entry_date','total_bill_amount','phone','pincode','state ','address','delete_status','part_no'];
}
控制器
<?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>
解决方法
我认为您忘了实际联系外交关系了。
您将需要致电:$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'));
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。