我正在尝试使用最新版本的laravel构建一个类似应用程序的博客。我正在试图找出如何从每个文章的数据库中拉出一个slug然后将其正确地路由到所有工作。
我有它的工作,但如果你使用slug来查看它,内容将不会显示在文章上。
localhost / articles / 1 - 工作正常,页面上的内容显示(标题等)
localhost / articles / installing-example - 这可以工作,但内容错误
当您尝试使用数据库中的slug导航到页面时会发生这种情况:Trying to get property 'title' of non-object (View: C:\xampp\htdocs\blogtest\resources\views\articles\show.blade.php)
这一行出错:<h1><?php echo e($articles->title); ?></h1>
应用程序/ HTTP /控制器/ ArticlesController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Article;
class ArticlesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$articles = Article::all();
return view('articles.index')->with('articles', $articles);
}
/**
* 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($id)
{
$articles = Article::find($id);
return view('articles.show')->with('articles', $articles);
}
/**
* 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)
{
//
}
}
应用程序/ Article.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $table = 'articles';
public $primaryKey = 'id';
public $timestamps = true;
}
路线/ web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::resource('articles', 'ArticlesController');
资源\意见\文章\ show.blade.php
@extends('layouts.master')
@section('content')
<h1>{{$articles->title}}</h1>
@endsection
数据库
任何帮助和建议将不胜感激。
Trying to get property 'title' of non-object (View: C:\xampp\htdocs\blogtest\resources\views\articles\show.blade.php)
意味着$articles
不是一个对象,如果你转储它,它应该输出null
- 非常正确。
find
函数用于使用主键查找行,而主键不是slug
列,因此无法找到记录。
您需要设置一个接受{slug}
的路线,然后根据您需要进行以下更改:
$articles = Article::find($id);
至,
$articles = Article::where('slug', $slug)->first();
并确保$slug
是实际的slug
而不是id
列值。
您需要在模型中设置路线键
// Article model
public function getRouteKeyName(){
return 'slug';
}
试试这个
没有slu ..
路线
Route::resource('articles', 'ArticlesController');
/条/ {ID}
调节器
public function show($id)
{
$articles = Article::first($id);
return view('articles.show', compact('articles'));
}
刀
@extends('layouts.master')
@section('content')
@if(!is_null($articles))
<h1>{{$articles->title}}</h1>
@endif
@endsection
随着Slug
路线
Route::get('/articles/{slug}', ['as'=>'articles.by.slug', 'uses'=> 'ArticlesController@showArticles']);
or
Route::get('/articles/{slug}', 'ArticlesController@showArticles')->name('articles.by.slug');
/条/ {段塞}
调节器
public function showArticles($slug)
{
$articles = Article::where('slug', $slug)->first();
return view('articles.show', compact('articles'));
}
刀
@extends('layouts.master')
@section('content')
@if(!is_null($articles)) //or @if($articles)
<h1>{{$articles->title}}</h1>
@endif
@endsection
这是因为您从数据库获取空数据。
原因是,你在id字段中传递slug所以
localhost/articles/installing-example
你传递id = Installing-example,因此找不到id,所以没有数据。
所以为了把你的slud转换成原始形式并且不用id搜索,用你的slug字段搜索。
让我们从标题中假设你做了slug。标题是installing example
所以当你得到slug,把它变成原始形式然后用原始的搜索
$articles = Article::where('titile',$original)->first();