我今天开始学习 Laravel,但遇到了一个问题。有3个单独的帖子。每个都有标题,我也做了另一个标题,应该在帖子页面的网址中,例如:something.com/posts/post_three 而不是 posts/3 使用 id 版本 (posts/3) 就可以了完美,但我试图更改代码,使其转到posts/post_three,但我不断收到以下错误:
试图获取非对象的属性“created_at”(视图:C:\xampp\htdocs\lsapp\resources\views\posts\show.blade.php)
我有以下代码,我已经改变了一些东西:
<?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)
{
//
}
}