【问题标题】:Retrieving data from two tables and to show the data on same page using Laravel从两个表中检索数据并使用 Laravel 在同一页面上显示数据
【发布时间】:2021-07-22 00:08:38
【问题描述】:

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

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

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

第二个控制器名称:SubemController 使用它从数据库中检索数据。表名是 subemployees 代码:

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

但在同一页面上,当我尝试加载表时,它会给出错误 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

第一个表的迁移代码如下:

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();
});

第二张桌子:

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();
});

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

【问题讨论】:

    标签: mysql laravel eloquent laravel-5.6


    【解决方案1】:

    问题是您的路由在PostController 使用index 方法。它返回视图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,
        ]);
    }
    

    【讨论】:

    • 非常感谢您的回答!你说的这两种方法我都试过了,可惜问题依旧。同样的错误“未定义的变量:子雇员(0)”
    猜你喜欢
    • 2013-04-23
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2021-02-18
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    相关资源
    最近更新 更多