使用 Laravel 从两个表中检索数据并在同一页上显示数据

Retrieving data from two tables and to show the data on same page using Laravel

发自内心的问候! 我正在使用 Laravel v-5.6。我正在尝试从两个 table 检索数据并将它们显示在同一页面上。我正在使用两个控制器和两个模型。与 tables 没有关系。 注意: subcondetails 是我要显示数据的视图

第一个控制器名称:PostController 使用它从数据库中检索数据。 Table 名称是分包商 代码:

public function index()
{
    $subcontractors = DB::select('select * from subcontractors');
    return view('subcondetails', ['subcontractors' => $subcontractors]);
}

第二个控制器名称:SubemController 使用它从数据库中检索数据。 Table 名字是子雇员 代码:

public function index()
{
    $subemployees = DB::select('select * from subemployees');
    return view('subcondetails', ['subemployees' => $subemployees]);
}

现在要在同一页面上显示数据,以下代码 第一个控制器

工作正常
    <thead>
        <tr>
            <th>ID</th>
            <th>COMPANY NAME</th>
    </tr>
    </thead>
    <tbody>
        @foreach($subcontractors as $subcontractor)
            <tr>
                <td>{{ $subcontractor->id }}</td>
                <td>{{ $subcontractor->companyname }}</td>
            </tr>
        @endforeach

但是在同一页面上,当我尝试加载 table 时,出现错误 Undefined Variable 'subemployees' 代码如下:

        <thead>
          <tr>
              <th>ID</th>
              <th>NAME</th>
        </tr>
      </thead>
    
        <tbody>
          @foreach($subemployees as $subemployee)
              <tr>
                  <td>{{ $subemployee->id }}</td>
                  <td></td>
              </tr>
              @endforeach

我的网络(路由)文件代码如下:

    Route::get('/', 'PostController@index');
    Route::resource('posts', 'PostController');

我使用此代码创建了第二个控制器 'SubempController'

    php artisan make:controller SubempController --resource

我的第一个 table 迁移代码如下:

Schema::create('subcontractors', function (Blueprint $table) {
    $table->increments('id');
    $table->string('branches');
    $table->string('companyname');
    $table->string('companyaddress');
    $table->string('contactperson');
    $table->integer('contactnumber');
    $table->string('contactdepartment');
    $table->string('emailaddress');
    $table->string('invoiceterms');
    $table->text('paymentterms');
    $table->integer('vatregistration')->nullable();
    $table->integer('payrate');
    $table->timestamps();
});

第 2 Table:

Schema::create('subemployees', function (Blueprint $table) {
    $table->increments('id');
    $table->string('staff_name');
    $table->integer('contact_number');
    $table->integer('sia_number')->nullable();
    $table->string('sia_type')->nullable();
    $table->date('my_date')->nullable();
    $table->date('my_date2');
    $table->integer('ni_number');
    $table->string('training_name');
    $table->integer('certificate_number');
    $table->string('address1');
    $table->timestamps();
});

我想有一个解决方案。如果您需要任何信息,请告诉我。

问题是您的路线在 PostController 处使用 index 方法。它 returns 视图 subcondetails,传递 subcontractors 变量,但不传递 subemployees.

您必须使用同一个控制器查询这两个资源:

public function index()
{
    $subemployees = DB::select('select * from subemployees');
    $subcontractors = DB::select('select * from subcontractors');

    return view('subcondetails', [
        'subcontractors' => $subcontractors,
        'subemployees' => $subemployees,
    ]);
}

您可以使用 Eloquent 模型来执行查询。普通的 SQL 可能不兼容,如果你以后想改变你的数据库,应该尽可能避免。

public function index()
{
    $subemployees = Subemployee::all(); //Change to your model name
    $subcontractors = Subcontractor::all(); //Change to your model name

    return view('subcondetails', [
        'subcontractors' => $subcontractors,
        'subemployees' => $subemployees,
    ]);
}