我是一名新开发者,我正在尝试将工作作为一个非常简单的博客。 我想在博客中设置上一篇和下一篇文章的上一页和下一篇链接。这是我目前的代码。
POSTS CONTROLLER
public function move($id)
{
$post = DB::table('posts')->find($id);
$previous = DB::table('posts')->where('id', '<', $post->id)->max('id');
$next = DB::table('posts')->where('id', '>', $post->id)->min('id');
return view('posts.show')->with('previous', $previous)->with('next', $next);
}
WEB.PHP
<?php
Route::get('/', 'PostsController@index')->name('home');
Route::get('/posts/create', 'PostsController@create');
Route::post('/posts', 'PostsController@store');
//Route::get('/posts/{post}', 'PostsController@show');
Route::get('/posts/tags/{tag}', 'TagsController@index');
Route::post('/posts/{post}/comments','CommentsController@store');
Route::get('/posts/{id}/edit', 'PostsController@edit');
Route::get('/edit/{post}', 'PostsController@update');
Route::patch('/post/{post}', 'PostsController@update');
Route::get('/register', 'RegistrationController@create');
Route::post('/register', 'RegistrationController@store');
Route::get('/login', 'SessionsController@create');
Route::post('/login', 'SessionsController@store');
Route::get('/logout', 'SessionsController@destroy');
Route::get('/posts/{id}', 'PostsController@move');
SHOW.BLADE
@extends ('layouts.master')
@section ('content')
<div class="col-sm-8 blog-main">
<h1> {{$post->title}}</h1>
@if (count($post->tags))
<ul>
@foreach($post->tags as $tag)
<li>
<a href="/posts/tags/{{ $tag->name}}">
{{ $tag->name }}
</a>
</li>
@endforeach
</ul>
@endif
{{$post->body}}
<hr>
<a href="/posts/{id}/edit">Modifica</a>
<hr>
<div class='comments'>
<ul class="list-group">
@foreach ($post->comments as $comment)
<li class="lista-commenti">
<strong>
{{$comment->created_at->diffForHumans()}}:
</strong>
{{ $comment -> body}}
</li>
@endforeach
</ul>
</div>
<hr>
<div>
<div>
<form method="POST" action="/posts/{{$post->id}}/comments">
{{csrf_field()}}
<div>
<textarea name="body" placeholder="Il tuo commento" class="form-control" required></textarea>
</div>
<div>
<button type="submit" class="bottone">Invia Commento</button>
</div>
</form>
@include('layouts.errors')
</div>
<div class="row">
<ul>
<li><a href="http://www.localhost.it/posts/{{ $previous }}"> Previous</a></li>
<li><a href="http://www.localhost.it/posts/{{ $next }}"> Next</a>
</li>
</ul>
</div>
</div>
</div>
@endsection
post.php中
<?php
namespace App;
use Carbon\Carbon;
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function addComment($body)
{
$user_id= auth()->id();
$this->comments()->create(compact('user_id','body'));
}
public function scopeFilter($query, $filters)
{
if(!$filters)
{
return $query;
}
if ($month = $filters['month'])
{
$query->whereMonth('created_at', Carbon::parse($month)->month);
}
if ($year = $filters['year']) {
$query->whereYear('created_at', $year);
}
}
public static function archives()
{
return static::selectRaw('year(created_at) year, monthname(created_at) month, count(*) published')
->groupBy('year','month')
->orderByRaw('min(created_at) desc')
->get()
->toArray();
}
public function tags(){
return $this->belongsToMany(Tag::class);
}
}
这给我一个关于前一个和下一个未定义变量以及www的错误。
很抱歉,这是我的第一篇文章,我无法上传任何图片。希望有人可以帮助我。
由于
亚历山德罗
答案 0 :(得分:1)
使用url()
辅助方法:
<li><a href="{{url('posts/' . $previous)}}"> Previous</a></li>
<li><a href="{{url('posts/' . $next)}}"> Next</a></li>
修改:删除此Route::get('/posts/{post}', 'PostsController@show');
行或更改move
方法中的show
方法代码。
对于您的新错误,请在顶部添加此行 -
use Illuminate\Support\Facades\DB;
答案 1 :(得分:0)
当您前往post/1
时,您正在执行show
方法,而不是move
。
另外,将代码更改为:
<li><a href="http://www.localhost.it/posts/{{ $previous }}"> Previous</a></li>
<li><a href="http://www.localhost.it/posts/{{ $next }}"> Next</a></li>
你说过,“还有关于www”。我很确定您没有收到有关$previous
或$next
的错误,但您收到有关www...
的错误,因为您尝试在代码中使用文本作为变量