Laravel v5.4 PHP

时间:2017-09-07 11:20:52

标签: php laravel type-conversion

控制器。     

namespace App\Http\Controllers;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class TestDBController extends Controller
{
    public function index()
    {
        $db_ext = \DB::connection('extdb');
        $customer = $db_ext->table('customer')->get();
        var_dump($customer);
        // var_dump($customer);
        return view('customers.index', ['customer' => $customer]);
    }
}

查看

<table class="table table-responsive" id="kunder-table">
    <thead>
        <th>ID</th>
        <th>name</th>
        <th>Email</th>
        <th>Mobil</th>
        <th>Address</th>
        <th>Address2</th>
        <th>Address3</th>
    </thead>
    <tbody>
    @foreach($customer as $customer)
        <tr>
            <td>{!! $customer->customerid !!}</td>
            <td>{!! $customer->name !!}</td>
            <td>{!! $customer->email !!}</td>
            <td>{!! $customer->mobile !!}</td>
            <td>{!! $customer->adress1 !!}</td>
            <td>{!! $customer->adress2 !!}</td>
            <td>{!! $customer->adress3 !!}</td>
        </tr>
    @endforeach
    </tbody>
</table>

这是&#34; var_dump&#34;结果

    object(Illuminate\Support\Collection)[261]
          protected 'items' => 
            array (size=365)
              0 => 
                object(stdClass)[262]
                  public 'BANKACCOUNT' => string '' (length=0)
                  public 'CUSTOMERID' => int 10000
                  ...

我得到的最终结果是&#34;未定义的属性:stdClass :: $ customerid 。 但每个表字段中也有customerid。 那我怎么能得到正确的列表视图?感谢。

1 个答案:

答案 0 :(得分:0)

请尝试以下更改:

TestDBController

return view('customers.index', ['customers' => $customer]);

在视图文件

@foreach($customers as $customer)
        <tr>
            <td>{!! $customer->customerid !!}</td>
            <td>{!! $customer->navn !!}</td>
            <td>{!! $customer->email !!}</td>
            <td>{!! $customer->mobile !!}</td>
            <td>{!! $customer->adress1 !!}</td>
            <td>{!! $customer->adress2 !!}</td>
            <td>{!! $customer->adress3 !!}</td>
        </tr>
    @endforeach

在控制器部分中,您将传递customer变量中的值,并在视图中使用相同名称的foreach循环。

foreach循环的默认语法是:

foreach($array as $values)

根据您的代码,您使用的是与您的集合数组相同的变量。