代码之家  ›  专栏  ›  技术社区  ›  Dan

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

  •  0
  • Dan  · 技术社区  · 8 年前

    我收到错误:

    尝试获取非对象的属性

    我愿意 var_dump($page) 它返回NULL。

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

    页面控制器:

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

    视图/页面/显示。刀身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();
    });
    

    视图/布局/索引。刀身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 工作岗位代码为:

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

    视图/部分/帖子。刀身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>
    

    视图/发布/显示。刀身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 回复  |  直到 8 年前
        1
  •  0
  •   abrar    8 年前

    这个错误是不言而喻的,这意味着数据库中没有这个slug的记录,查询返回null。如果记录为null,则添加条件。如下所示

    if(isset($page) and count($page)>0) {
      echo $page->title 
    } 
    
    or
    
    if(isset($page->title) and $page->title!='') {
      echo $page->title 
    }
    
    推荐文章