【发布时间】:2017-09-07 11:20:51
【问题描述】:
控制器。
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>
这是“var_dump”结果。
object(Illuminate\Support\Collection)[261]
protected 'items' =>
array (size=365)
0 =>
object(stdClass)[262]
public 'BANKACCOUNT' => string '' (length=0)
public 'CUSTOMERID' => int 10000
...
我得到的最终结果是 “未定义的属性:stdClass::$customerid。 但是每个表字段中也有customerid。 那么我怎样才能得到正确的列表视图呢?谢谢。
【问题讨论】:
-
你可能需要 $customer->id
-
你使用的是什么版本的 Laravel?
-
尝试用 customerid 替换 CUSTOMERID
-
'CUSTOMERID' 和其他在这种情况下区分大小写因此将
$customer->customerid更正为$customer->CUSTOMERID -
@Ross 这是 5.4 版。
标签: php laravel type-conversion