我正在尝试在welcome.blade.php中显示我的类别,但是它给了我一个错误。
我的welcome.blade.php:
<div class="links">
@foreach($categories as $category)
<a href="{{ url('/receitas/'.$category->id)}}">{{ $category->name }}</a>
@endforeach
</div>
我的WelcomeController.php:
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
use App\Welcome;
class WelcomeController extends Controller
{
public function index()
{
return view('welcome');
}
public function show()
{
$categories = Category::all();
return view('welcome', compact([ 'categories' => $categories]));
}
}
我的web.php:
<?php
Route::get('/', 'WelcomeController@index')->name('entrada');
Route::get('/teste/teste', 'UserController@index');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/receitas', 'ReceitasController@index')->name('receitas');
Route::get('/clientes', 'HomeController@clientes');
Route::get('/category', 'CategoryController@index');
我已经尝试使用@if条件来验证变量$ categories是否为空并且它似乎是。
<div class="links">
@if(empty($categories))
Alert: the variable is empty!!
@else
@foreach($categories as $category)
<a href="{{ url('/receitas/'.$category->id)}}">{{ $category->name }}</a>
@endforeach
@endif
</div>
但是在我的数据库中,我有类别记录。
Printscreen of my category table
你能帮帮我吗?