我正在学习Laravel,尝试在视图中显示列表时出现了一个愚蠢的错误。我有一个名为“ catalogs”的表,其中包含一些条目,但无法在视图中显示此条目。
我一直在寻找问题好几个小时,但我无法解决...以前我已经做到了,但是现在我找不到问题了...
这是我的代码
CatalogController(索引函数)
namespace TBZPlus\Http\Controllers;
use TBZPlus\Catalog;
use Illuminate\Http\Request;
class CatalogController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$catalogs = Catalog::all();
return view('catalogs.index',compact('catalogs'));
}
Model Catalog.php
<?php
namespace TBZPlus;
use Illuminate\Database\Eloquent\Model;
class Catalog extends Model
{
//
}
路线
Route::resource('catalogs', 'CatalogController');
UP功能(闪烁)
public function up()
{
Schema::create('catalogs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('xmlcatalogo');
$table->timestamps();
});
}
查看(仅foreach)
@foreach($catalogs as $catalog)
<tr>
<td>{{$catalog->$id}}</td>
<td>{{$catalog->$name}}</td>
<td>{{$catalog->$xmlcatalogo}}</td>
</tr>
@endforeach
答案 0 :(得分:3)
您可以尝试使用它吗?
@foreach($catalogs as $catalog)
<tr>
<td>{{$catalog->id}}</td>
<td>{{$catalog->name}}</td>
<td>{{$catalog->xmlcatalogo}}</td>
</tr>
@endforeach
请注意在字段名消失之前的$
。因为您在$catalog
实例上使用了属性。
答案 1 :(得分:1)
1。请在模型中指定表格
=> Catalog.php
<?php
namespace TBZPlus;
use Illuminate\Database\Eloquent\Model;
class Catalog extends Model
{
protected $table = 'your table name';
}
2。更改视图文件
@foreach($catalogs as $catalog)
<tr>
<td>{{$catalog->id}}</td>
<td>{{$catalog->name}}</td>
<td>{{$catalog->xmlcatalogo}}</td>
</tr>
@endforeach