doudeng1870 2019-05-15 20:26
浏览 20
已采纳

为什么$ post不是第三个代码中的对象?

I started learning Laravel today and I've ran into a problem. There are 3 seperated posts. Each one has titles and I made an other title too which should be in the url of the page of the posts, for example: something.com/posts/post_three instead of posts/3 With the id version (posts/3) it worked perfectly, but I tried to change the code so it will go to posts/post_three, but I keep getting the following error:

Trying to get property 'created_at' of non-object (View: C:\xampp\htdocs\lsappesources\views\posts\show.blade.php)

I have the following codes which I've changed something in:

<?php

namespace App\Http\Controllers;

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

class PostsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $posts = Post::orderBy('urltitle', 'desc')->paginate(5);
        return view('posts.index')->with('posts', $posts);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

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

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($urltitle)
    {
        $post = Post::find($urltitle);
        return view('posts.show')->with('post', $post);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

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

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

-

@extends('pages.layouts.app')
@section('content')
    <h1 style="text-align: center">Posts</h1>
    @if(count($posts) > 0)
        @foreach($posts as $post)
                {{$post->urltitle}}
                <div class="card" style="padding-top: 8px">
                    <div class="card-body">
                    <h3><a href="/posts/{{$post->urltitle}}">{{$post->title}}</a></h3>
                    <small>Written on: {{$post->created_at}}</small>
                    </div>
                </div>
        @endforeach
        <div style="justify-content: center;  display: flex;">{{$posts->links()}}</div>
    @else
        <p>No posts found</p>
    @endif


  @endsection

- And here is the code which can't "recognise" $post

@extends('pages.layouts.app')

@section('content')
<div class="jumbotron text-center">
    <a href="/posts" class="btn btn-default">Go Back</a>
<h1>{{$post->title}}</h1>
<div>{{$post->body}}</div>
<hr>
<small>Written at {{$post->created_at}}</small>
</div>
@endsection
  • 写回答

1条回答 默认 最新

  • duanjiangzhi6851 2019-05-15 20:57
    关注

    This is where the error occurs:

    public function show($urltitle)
    {
        $post = Post::find($urltitle); // <----
    
        return view('posts.show')->with('post', $post);
    }
    

    The find() method will check for a primmary key (id by default) that hold the given value, so this both sentences are equal:

    $post = Post::find($id);
    // equals to:
    $post = Post::where('id', $id)->first();
    

    but given the fact that you are passing the urltitle instead of the $id, you should use a more generic approach:

    $post = Post::where('urltitle', $urltitle)->first();
    

    Also, as @devon said, you should check if a value is returned:

    $post = Post::where('urltitle', $urltitle)->first();
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    if( ! $post)
    {
        // do something
    }
    

    or throw an error if the query returns nothing:

    $post = Post::where('urltitle', $urltitle)->firstOrFail();
                                                ^^^^^^^^^^^^^
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?