Dan
Dan

Reputation: 946

Laravel Voyager /admin not working after routes to pages defined

I made it so that I can access pages other than the index. This works perfectly. In the admin panel I can view every page except /admin. So I can view the /admin/posts and /admin/media page etc etc.

I am getting the error

Trying to get property of non-object

It references this line in my show.blade:

<header class="masthead" style="background-image: url('/storage/{{ $page->image }}')">

I understand it is because there is no $page->image object, and believe /admin/something is being treated as a post, where as /admin is a page and so it trying to look for this not existent object?

What I am wanting is for the /admin page to load as it did previously, while retaining the functionality that the pages/show.blade.php provides throughout the front end of the website. So pretty much for my existing code not to consider /admin.

I did try

$host = $_SERVER['REQUEST_URI'];
if($host !== '/admin') 
{
    my show.blade.php content here
}

But this still doesn't escape that the admin page is being treated as a page and so is still looking at the show.blade file and so it leaves me needing to create a custom /admin page and I would rather for now stick with the out of the box voyager admin page.

routes:

Route::get('/', 'TrainController@index');

Route::get('post/{slug}', 'TrainController@show');
Route::get('{slug}', 'PagesController@show');


Route::group(['prefix' => 'admin'], function () {
    Voyager::routes();

PagesController

<?php

namespace App\Http\Controllers;

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

class PagesController extends Controller
{
    public function show($slug)
    {
        $page = Page::findBySlug($slug);
        return view('page.show', ['page' => $page]);
    }
}

Pages Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    public static function findBySlug($slug)
    {
        return static::where('slug', $slug)->first();
    }
}

pages/show.blade.php

@extends('layouts.index')

@section('header')


<!-- Page Header -->
<header class="masthead" style="background-image: url('/storage/{{ $page->image }}')">
    <div class="overlay"></div>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-md-10 mx-auto">
                <div class="page-heading"> <h1>{!! $page->title !!}</h1>
                    <span class="subheading"></span>
                </div>
            </div>
        </div>
    </div>
</header>

@stop

@section('content')

<!-- Main Content -->

    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-md-10 mx-auto">
                {!! $page->body !!}
            </div>
        </div>
    </div>


@stop

Upvotes: 1

Views: 4267

Answers (1)

Dan
Dan

Reputation: 946

if (!Request::is('admin'))
{
    Route::get('{slug}', 'PagesController@show');
}

Upvotes: 3

Related Questions