Undefined variable: page (View: ... Resources views layouts app.blade.php

Asked

Viewed 433 times

1

Variable does not work when logging in.

in the login view I’m injecting the layout:

@extends('layouts.app')

and in the layout has the variable

<p class="num">Telefone: {!!$page->telefone!!}</p>

If you are accessing the pages that do not go through middleware auth, open normally, but otherwise the variable is not recognized, with the error:

Undefined variable: page (View: ...\resources\views\layouts\app.blade.php)

I’m new and don’t know how to make it work inside the middleware?

--controller

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Page;
use Illuminate\Http\Request;

class PagesController extends Controller
{
    public function welcome(Request $request)
    {
        $page = Page::findOrFail(1);

        return view('welcome', compact('page'));
    }

    public function index(Request $request)
    {
        $keyword = $request->get('search');
        $perPage = 25;

        if (!empty($keyword)) {
            $pages = Page::where('titulo', 'LIKE', "%$keyword%")
                ->orWhere('menu', 'LIKE', "%$keyword%")
                ->orWhere('body', 'LIKE', "%$keyword%")
                ->latest()->paginate($perPage);
        } else {
            $pages = Page::latest()->paginate($perPage);
        }
        return view('page.pages.index', compact('pages'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\View\View
     */
    public function create()
    {
        return view('page.pages.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {

        $requestData = $request->all();

        Page::create($requestData);

        return redirect('page/pages')->with('flash_message', 'Page added!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function show($id)
    {
        $page = Page::findOrFail($id);

        return view('page.pages.show', compact('page'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     *
     * @return \Illuminate\View\View
     */
    public function edit($id)
    {
        $page = Page::findOrFail($id);

        return view('page.pages.edit', compact('page'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param  int  $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(Request $request, $id)
    {

        $requestData = $request->all();

        $page = Page::findOrFail($id);
        $page->update($requestData);

        return redirect('page/pages')->with('flash_message', 'Page updated!');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function destroy($id)
    {
        Page::destroy($id);

        return redirect('page/pages')->with('flash_message', 'Page deleted!');
    }
}

  • Enter your controller code.

  • First of all, it has nothing to do with middleware and according to which route is using to happen the given error?

1 answer

0

You are trying to use the variable $page view that inherits layouts.app?

Maybe you explicitly pass the variable $page for your layout.

@extends('layouts.app', compact('page'))

Browser other questions tagged

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