0

For some reason I can't get this to work, I've been racking my brain for two days now trying to figure it out and it's just not happening.

This is my route group I have setup...

// Admin Panel Routes Group
Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function() {
    Route::get('/', ['uses' => 'Admin\AdminController@index'])->name('admin');
    Route::get('posts', ['uses' => 'PostController@index'])->name('posts-list');
    Route::delete('post/delete/{id}', ['uses' => 'PostController@delete'])->name('post-delete');
    Route::get('posts/new', ['uses' => 'PostController@newPost'])->name('post-new');
    Route::post('posts/create', ['uses' => 'PostController@createPost'])->name('post-create');
    Route::post('posts/upload', ['uses' => 'PostController@fileUpload'])->name('post-upload');
    Route::get('post/{slug}/edit', ['uses' => 'PostController@editPost'])->name('post-edit');
});

As you can see, I have managed to use a parameter in the DELETE request for posts perfectly fine. The function for the DELETE request in the PostController works as expected and reads the parameter from the URL and deletes the record from the database.

The issue may lie within the PostController 'editPost' function, this is a function I intend to use to fetch the post from the database via a slug, using that to determine which post it is and then loading the form with the data.

public function editPost($slug) {
    $post = Post::whereSlug($slug)->firstOrFail();
    return view('posts.edit')->withPost($post);
}

I am toying with using the ID instead, although I don't think that's where my issue lies. The only difference between the delete function and this new edit function I have is that it uses a string to get the post as opposed to the ID.

The issue I am hitting is when trying to access the edit form for a specific post...

(1/2) UrlGenerationException Missing required parameters for [Route: post-edit] [URI: admin/post/{slug}/edit].

Feel free to offer suggestions of ways to make my code better, I have only recently started using Laravel and this is my first project.

Just in case anyone requests it, here is the 'edit.blade.php' I am passing the $post variable to...

@extends('layouts.panel')

@section('content')
    <form class="new-post" method="POST" action="">
        {{ csrf_field() }}
        <div class="new-post__seperator">
            <input type="text" class="new-post__input" id="title" name="title" value="{{ $post->title }}" placeholder="Post Title..." autofocus>
        </div>
        <div class="new-post__upload"></div>
        <div class="new-post__editor">
            <textarea id="wysiwyg" value="{{ $post->content }}"></textarea>
        </div>
        <div class="new-post__checkboxes">
            <input type="checkbox" id="publish-checkbox" name="publish-checkbox" class="publish-checkbox" checked>
            <label for="publish-checkbox">By checking this box, you will be publishing the post, making it viewable via the home page.</label>
        </div>
    </form>
@endsection
  • Please provide where you put **edit** link in view file – AddWeb Solution Pvt Ltd Aug 23 '17 at 12:13
  • @AddWebSolutionPvtLtd Currently, I have been just linking to the page via the URL and testing it that way before adding an actual link - as this is a GET request I figured it could be done that way... am I wrong? – ThatGeekyGinger Aug 23 '17 at 12:14
  • please provide me which link are you used for testing – AddWeb Solution Pvt Ltd Aug 23 '17 at 12:16
  • Because your link like: localhost:8000/admin/post/sd1/edit – AddWeb Solution Pvt Ltd Aug 23 '17 at 12:17
  • @AddWebSolutionPvtLtd I have been using this link for now 'http://localhost:8000/admin/post/this-is-a-test-post/edit/' - this post exists in the database and can be viewed fine from the showPost view. – ThatGeekyGinger Aug 23 '17 at 12:18
  • This work perfectly? – AddWeb Solution Pvt Ltd Aug 23 '17 at 12:20
  • @AddWebSolutionPvtLtd No, this is what i was doing originally. – ThatGeekyGinger Aug 23 '17 at 12:20
  • Show how you're generating the new link in your page. I'm guessing you're not passing the slug into the generator, so it can't form the link correctly. – aynber Aug 23 '17 at 12:21
  • Wait i will come in 10 minute – AddWeb Solution Pvt Ltd Aug 23 '17 at 12:21
  • @aynber I'm not currently using a link to access it, I am simply typing it into the browser, which should work fine as this is a GET method right? – ThatGeekyGinger Aug 23 '17 at 12:25
  • Btw, find a common syntax for declaring url of resources. like: List -> /posts Get -> /posts/{slug} Delete -> /posts/{slug}/delete Edit -> /posts/{slug}/edit and so on... – Desh901 Aug 23 '17 at 12:33
  • It should be fine accessing the link, yes. However, from what I've seen, that error comes when generating a link on the page using `route()`. [Link the first](https://stackoverflow.com/questions/35259948/laravel-5-2-missing-required-parameters-for-route-user-profile-uri-user-ni), [Link the second](https://stackoverflow.com/questions/38177872/urlgenerationexception-missing-required-parameters-for-route), [Link the third](https://stackoverflow.com/questions/42551730/urlgenerationexception-in-urlgenerationexception-php-line-17) – aynber Aug 23 '17 at 12:33
  • @aynber I noticed I had put a link in the initial main blade that I was extending from and I was passing no argument into that, must have happened while I was testing on the first day and rushing it. Thanks for your help! – ThatGeekyGinger Aug 23 '17 at 12:37

3 Answers3

0

Try outputting the slug variable and see what it returns

Hugo
  • 170
  • 1
  • 11
  • I've outputted the slug variable and it returns exactly what is in the URL so it seems to be working if I do it this way... `Route::get('post/{slug}/edit', function($slug) { return 'Slug - '.$slug; });` – ThatGeekyGinger Aug 23 '17 at 12:24
0

The problem should be that you are using route('post-edit') instead of route('post-edit', $slug) somewhere in your code or in your views. Could you check that?

Desh901
  • 2,633
  • 17
  • 24
  • You're welcome :) I didn't noticed that @aynber posted that comment! Anyway thanks for the mark! – Desh901 Aug 23 '17 at 12:45
0

I've managed to find the answer, in my 'layouts/panel.blade.php' I had a link to my 'posts-edit' route that passed in no variable at all to it so was hitting the error.

Thanks to @aynber for the comment that fixed my problem..

It should be fine accessing the link, yes. However, from what I've seen, that error comes when generating a link on the page using route(). Link the first, Link the second, Link the third