Swap the Welcome view for another in Laravel 5.3

Asked

Viewed 434 times

-1

I plan to replace the Welcome view with another one I created with a controller. For better understanding, here’s my attempt that’s not working:

Route

Route::get('/portal', 'portal\SiteController@index');

Controller

public function getPostsCarousel() //Busca os posts para o carousel
{
    return Post::orderBy('id', 'DESC')->take(5)->get();
}

public function getPostsSite() //Busca os posts que ficam abaixo do carousel
{
    return Post::orderBy('id', 'DESC')->take(6)->get();
}

Sitecontroller

public function index()
{     

    return view('portal.home')
        ->with('postCarousel', $this->getPostsCarousel())
        ->with('postsSite', $this->getPostsSite());
}

home view

@extends('portal.welcome')

@section('conteudo')
    <div>
        <div id="carousel-example-generic" class="carousel slide carousel-home" data-ride="carousel">


            <!-- Indicators -->
            <ol class="carousel-indicators">
                @foreach($postCarousel as $postCar)
                    {{--<li data-target="#carousel-example-generic" data-slide-to="{{$i}}"></li>--}}
                    <li data-target="#carouselExampleIndicators" data-slide-to="{{ $loop->index }}" class="{{ $loop->first ? 'active' :'' }}"></li>
                @endforeach
            </ol>

            <!-- Wrapper for slides -->
            <div class="carousel-inner" role="listbox">
                @foreach($postCarousel as $item)
                    <div class="item {{ $loop->first ? 'active' : '' }}">
                        <img src="{{$item->imagem}}" alt="...">
                        <div class="carousel-caption" style="background-color: rgba(133,178,0,0.7)">
                            <p><h3>{{$item->titulo}}</h3></p>
                        </div>
                    </div>

                @endforeach

            </div>

            <!-- Controls -->
            <a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>
        </div>

        <div class="homeNotic">
            <hr>

            <div class="row">
                @foreach ($postsSite as $key =>$value)

                <div class="col-sm-3 col-md-4">

                    <div class="thumbnail">
                        <img src="{{$value->imagem}}" alt="{{$value->titulo}}">
                        <div class="noticia-titulo-home" >
                            <p class="titVisualizado"><b>{{$value->titulo}}</b></p>
                        </div>

                        <div class="noticia-conteudo-home">
                            <p class="contVisualizado">{{$value->descricao}}</p>
                        </div>


                        <div class="caption" style="height: 20px; padding: 5px" >
                            <p>
                                <a href="{{url('visualizar-noticia/'.$value->id)}}" class="btn btn-success" role="button">Ler Mais</a>
                            </p>
                        </div>

                        <div class="caption" style="height: 25px; padding: 5px" >
                            <p>
                                <span class="pull-right" style="color: #e38d13">{{$value->created_at->diffForHumans()}}</span>
                            </p>
                        </div>

                    </div>
                </div>
                    @endforeach


                    <p>
                        <a style="width: 100%; font-size: 14pt" href="{{url('index-noticia')}}" class="btn btn-success" role="button">VER MAIS NOTÍCIAS <i class="glyphicon glyphicon-forward"></i></a>
                    </p>
            </div>
        </div>

    </div>
@endsection

And I’m getting the following mistake:

Notfoundhttpexception in Routecollection.php line 161:

I’ve also tried to change course to:

Route::get('/', function (){
   return view('portal.home');
});

And made the following mistake:

Errorexception in 2574245cf5e2373fc28372419f83853770ad5aae.php line 8: Undefined variable: postCarousel (View: C: xampp htdocs GEIGRE_WEB5_3 Resources views portal home.blade.php)

1 answer

0

Let’s go to a brief check list to use views in the Windows.

  • By default views are in the folder resources/views
  • The call from a view must be separated by ., then to the call view('portal.home') the following file must exist in this path resources/views/portal/home.blade.php

Important If your controller is in a subfolder of App\Http\Controllers you need to adjust 2 things:

  1. Adjust the namespace declaration in this controller to reflect this folder structure. Otherwise autoload composer you won’t find the archive.
  2. You must import the base controller App\Http\Controllers\Controller through the declaration use right after the namespace of that file.

Note: Step 2 described above is only required for controllers that are not at the same file level App\Http\Controllers\Controller.php;

The example below describes the summary of a controller in the path App/Http/Controllers/Portal:

<?php
// Este arquivo deverá estar dentro de pasta Portal em App/Http/Controllers
namespace App\Http\Controllers\Portal;

use App\Http\Controllers\Controller;

class SiteController extends Controller
{
    public function index()
    {     
        return view('portal.home', [
            'postCarousel' => $this->getPostsCarousel(),
            'postsSite' => $this->getPostsSite()
        ]);
    }

    public function getPostsCarousel() //Busca os posts para o carousel
    {
        return Post::orderBy('id', 'DESC')->take(5)->get();
    }

    public function getPostsSite() //Busca os posts que ficam abaixo do carousel
    {
        return Post::orderBy('id', 'DESC')->take(6)->get();
    }
}

The route archive should be as follows:

Route::get('portal', 'Portal\SiteController@index');`

After that you can execute the following commands (cmd or bash) at the root of your project
composer dump-autoload and php artisan route:clear to redo the Build autoload structure with the new(s) namespace(s) and clear the route cache respectively.

Project structure:

root
├── app
│   ├── Http
│   │   ├── Controllers
│   │   │   ├── Portal
│   │   │   │   ├── SiteController.php
│   │   │   ├── Controller.php
├── resources
│   ├── views
│   │   ├── portal
│   │   │   ├── home.blade.php

About the error:

Errorexception in 2574245cf5e2373fc28372419f83853770ad5aae.php line 8: Undefined variable: postCarousel (View: C: xampp htdocs GEIGRE_WEB5_3 Resources views portal home.blade.php)

It’s because your template is trying to use data that has not been passed to view within clousure. $postCarousel

Browser other questions tagged

You are not signed in. Login or sign up in order to post.