2

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()}}: &nbsp;

            </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

1
  • first you may do this .. $data['previous'] = DB::table('posts')->where('id', '<', $post->id)->max('id'); same for next .. then return view('posts.show',$data); Commented Feb 12, 2018 at 9:50

2 Answers 2

1

use url() helper method:

    <li><a href="{{url('posts/' . $previous)}}"> Previous</a></li>

    <li><a href="{{url('posts/' . $next)}}"> Next</a></li>

Edit: remove this Route::get('/posts/{post}', 'PostsController@show'); line or change your code like move method in your show method.

For your new error, add this line at top-

use Illuminate\Support\Facades\DB;
Sign up to request clarification or add additional context in comments.

29 Comments

I tried but he still give me the same errors : Undefined variable: previous Undefined variable: next
Yes, I did. This is the error : Undefined variable: previous (View: C:\Users\michela\Desktop\cremascoli alessandro\PROGETTOHELLOGEST\SourceTree-Teo\resources\views\posts\show.blade.php)
what is the url you are trying that gives this error??
try dd($previous) before return statements in your controller code and see what it gives
make a fresh post with your updated code, that way may be others can help you
|
0

When you're going to post/1, you're executing the show method and not move.

Also, change the code to:

<li><a href="http://www.localhost.it/posts/{{ $previous }}"> Previous</a></li>
<li><a href="http://www.localhost.it/posts/{{ $next }}"> Next</a></li>

You've said, "and also about the www". I'm pretty sure you're not getting the error about the $previous or $next but you get the error about the www... because you're trying to use text as variable in your code.

4 Comments

Oh sorry. I'm very new and I'm still trying to learn as much as possible about this. I've correct my sintax but he give me two errors : Undefined variable: previous Undefined variable: next
Even if you fixed the www, it can't give you two errors but just one. Why are you making this up? It's hard to help you when you're doing this instead of telling what exactly is going on (showing the error etc).
He gives me this error : Undefined variable: previous (View: C:\Users\michela\Desktop\cremascoli alessandro\PROGETTOHELLOGEST\SourceTree-Teo\resources\views\posts\show.blade.php) I tried to cancel the "previous" part and he gives me the same error for the "next".
@alessandrocremascoli When you're going to post/1, you're executing the show method and not move. I've updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.