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

问题描述

您好,我需要帮助解决此问题。

我正在尝试获得“一对多”关系。但出现错误“为foreach()提供了无效的参数”

供应商模型

<?PHP

namespace App;
use App\Purchase;
use Illuminate\Database\Eloquent\Model;

class @R_404_3996@ extends Model
{
     
     protected $table = '@R_404_3996@s';
     protected $fillable = ['party_id','@R_404_3996@s_master_id','@R_404_3996@s_unic_id','@R_404_3996@_name','email','phone','address','city','state','pincode','GSTIN','delete_status'];



        public function purchases()
{
    return $this->hasMany(Purchase::class,'@R_404_3996@s_master_id');
}


}

购买模型

<?PHP

namespace App;
use App\Purchase;
use Illuminate\Database\Eloquent\Model;

class Purchase extends Model
{
     protected $table = 'purchases';
     protected $fillable = 

['@R_404_3996@s_master_id','@R_404_3996@s_unic_id','party_id','@R_404_3996@_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\@R_404_3996@;
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($@R_404_3996@s_master_id)
    { 
      
        $entry = @R_404_3996@::where('delete_status','!=','NOT_DELETED')->find($@R_404_3996@s_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->@R_404_3996@_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')); 
}