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

时间:2018-04-10 04:04:35

标签: php laravel voyager

我收到错误:

  

尝试获取非对象的属性

我做var_dump($page)并返回NULL。

我正在尝试完全按照我在帖子项目中所做的那样,我能够{{ $post->title }}但是对于页面我收到此错误并且我不明白为什么(我理解错误的含义,但在上下文中,我不知道为什么它不适用于页面但是适用于帖子。如果我不尝试提取任何数据库内容,页面会加载。我真的不知道自己错过了什么。

PagesController:

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

视图/页/ 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();
});

视图/布局/ 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>

数据库: enter image description here enter image description here 有效的帖子代码是:

发布模型:

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(用于索引点击以发布详细信息)

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

视图/后/ 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

1 个答案:

答案 0 :(得分:0)

错误是不言自明的,这意味着DB中没有此slug的记录,查询返回null。如果记录为null,则添加条件。如下所示

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

or

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