0

I am trying to display my categories in the welcome.blade.php, however it gives me an error.

My welcome.blade.php:

<div class="links">
    @foreach($categories as $category)
        <a href="{{ url('/receitas/'.$category->id)}}">{{ $category->name }}</a>
    @endforeach
</div>

My 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]));
    }
}

And my 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');

I have already tried to use a @if condition to verify if the variable $categories is empty and it seem it is.

<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>

However in my database I have records of categories.

Printscreen of my category table

Can you help me please!

CodeJr
  • 47
  • 1
  • 6
  • Can you report the error message? – Daniele D. Nov 02 '17 at 11:53
  • do something like this `return view('welcome', ['categories' =>$categories]); ` – Bhaumik Pandhi Nov 02 '17 at 11:56
  • @Webinion I tried your suggestion, but it returns the same error... – CodeJr Nov 02 '17 at 12:13
  • @DanieleD. the error message is: "Undefined variable: categories (View: /Sites/laravel/laravel-intro/resources/‌​views/welcome.blade.‌​php)" – CodeJr Nov 02 '17 at 12:16
  • use only `compact('categories')` in WelcomeController or use `view('welcome')->with('categories', $categories);` – Jignesh Solanki Nov 02 '17 at 12:23
  • @JigneshSolanki I had already tried that, but still not working... I tried all these options: `return view('welcome', compact([ 'categories' => $categories])); //return view('welcome', compact('categories')); //return view('/')->withCategories($categories); //return view('welcome', $categories); //return View::make('welcome')->with('categories', $categories);` – CodeJr Nov 02 '17 at 12:40
  • @ribeiro.csa are you sure you are invoking correct method? you have 2 methods `index()` and `show()`. you have to call `show()` instead of `index()`. – Jignesh Solanki Nov 02 '17 at 12:45
  • @JigneshSolanki I am using index() now and it's working! – CodeJr Nov 02 '17 at 14:49

0 Answers0