laravel 一对多 为 foreach() 提供的参数无效

laravel one to many Invalid argument supplied for 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', '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')); 
}