访问 laravel 中其他页面上的数据库内容

access database content on other pages in laravel

我收到错误:

Trying to get property of non-object

我做 var_dump($page) 并且它 returns NULL。

我正在尝试完全按照我在 post 项目中所做的那样进行操作,在那里我可以做到 {{ $post->title }} 但是对于页面我收到此错误并且我没有不明白为什么(我理解错误的含义,但在上下文中我不知道为什么它对页面不起作用但对 posts 有效。如果我不尝试拉,页面会加载任何数据库内容。我真的不知道我错过了什么。

页面控制器:

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

页面模型

namespace App;

use Illuminate\Database\Eloquent\Model;

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

views/page/show.blade.php

@extends('layouts.index')

@section('title', '-' . $page->title)
@section('header')
<?php //var_dump($page) ?>

<!-- Page Header -->
<header class="masthead" style="background-image: url('')">
    <div class="overlay"></div>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-md-10 mx-auto">
                <div class="page-heading">
                    <span class="subheading">This is what I do.</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

路线:

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

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


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

views/layouts/index.blade.php:

<!DOCTYPE html>
<html lang="en">

  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>{{ setting('site.title') }}</title>

    <!-- Bootstrap core CSS -->
    <link href="/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom fonts for this template -->
    <link href="/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>

    <!-- Custom styles for this template -->
    <link href="/css/clean-blog.min.css" rel="stylesheet">
    <link href="/css/traintesting.css" rel="stylesheet">

  </head>

  <body>

    @include('partials.nav')
    @yield('header')


    <!-- Main Content -->
    <div class="container">
        @yield('content')
    </div>

    <hr>
    @include('partials.footer')
    <!-- Bootstrap core JavaScript -->
    <script src="/vendor/jquery/jquery.min.js"></script>
    <script src="/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Custom scripts for this template -->
    <script src="/js/clean-blog.min.js"></script>

  </body>

</html>

数据库: post 有效的代码是:

Post 型号:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function author()
    {
        return $this->belongsTo(User::class,'author_id');
    }

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

控制器:

namespace App\Http\Controllers;

use App\Post;
use Illuminate\Http\Request;

class TrainController extends Controller
{
    public function index()
    {
        $posts = Post::simplePaginate(2);
        return view('index', ['posts' => $posts]);
    }

    public function show($slug)
    {
        $post = Post::findBySlug($slug);
        return view('post.show',['post'=>$post]);
    }
}

views/partials/post.blade.php(用于索引以点击 post 详细信息)

<div class="post-preview">
    <a href="/post/{{ $post->slug }}">
        <h2 class="post-title">
            {{ $post->title }}
        </h2>
        <h3 class="post-subtitle">
            {{ $post->excerpt }}
        </h3>
    </a>
    <p class="post-meta">Posted by
        <a href="#">{{ $post->author->name }}</a>
        on {{ $post->created_at->format('l d F, Y') }}</p>
</div>
<hr>

views/post/show.blade.php:

@extends('layouts.index')

@section('title', '-' . $post->title)

@section('header')
<!-- Page Header -->
<header class="masthead" style="background-image: url('/storage/{{ $post->image }}')">

    <div class="overlay"></div>
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-md-10 mx-auto">
                <div class="post-heading">
                    <h1>{{ $post->title }}</h1>
                    <h2 class="subheading">{{ $post->sub_title }}</h2>
                    <span class="meta">Posted by
                <a href="#">{{ $post->author->name }}</a>
                {{ $post->created_at->format('l d F, Y') }}</span>
                </div>
            </div>
        </div>
    </div>
</header>
@stop

@section('content')


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

<hr>


@stop

错误是self-explanatory,这意味着数据库中没有这个slug的记录,查询return为null。如果记录为空,则添加条件。喜欢下面

if(isset($page) and count($page)>0) {
  echo $page->title 
} 

or

if(isset($page->title) and $page->title!='') {
  echo $page->title 
}