I'm a new developer and I'm trying to make work my very simple blog. I want to set a previous and a next link to my previous and next articles in the blog. This is my current code.
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);
}
}
This gives me an error about the undefined variables previous and next and also about the www.
Sorry but this is my first post and I can't upload any images. Hope someone can help me.
Thanks
Alessandro
$data['previous'] = DB::table('posts')->where('id', '<', $post->id)->max('id');same for next .. thenreturn view('posts.show',$data);