Home > OS >  Laravel variable not getting passed to route
Laravel variable not getting passed to route

Time:10-21

In my page, I have the following anchor:

<a href="{{ route('blogtitles.edit', $blogTitle) }}">Edit</a>

which links to the following function in the controller:

public function edit(BlogTitle $blogTitle)
{
    return view('blogtitles.edit')->with('blogTitle', $blogTitle);
}

and then the blogtitles/edit.blade.php file simply contains:

{{ $blogTitle }}

However, in the edit.blade.php, the variable '$blogTitle` is empty, even though I know that it isn't empty in the original page where it is passed from. Any idea at all what is going wrong? I have other anchors that do exactly the same thing and work fine, so no clue what the problem is.

The url to get to the edit.blade.php is http://localhost:8000/blogtitles/1/edit and the routes are:

PUT|PATCH | blogtitles/{blogtitle}      | blogtitles.update  | App\Http\Controllers\BlogTitlesController@update                   | web                                                |
GET|HEAD  | blogtitles/{blogtitle}/edit | blogtitles.edit    | App\Http\Controllers\BlogTitlesController@edit                     | web                                             

CodePudding user response:

The problem was actually in the function in the controller autogenerated by php artisan. The $blogTitle needed to be $blogtitle so the function became:

public function edit(BlogTitle $blogtitle)
{
    return view('blogtitles.edit')->with('blogTitle', $blogtitle);
}

Thanks to user lagbox for prompting me to try this!

CodePudding user response:

you need to send only the id, like this

<a href="{{ route('blogtitles.edit', $blogTitle->id) }}">Edit</a> 

CodePudding user response:

See if in your web.php the url scheme. It's probably

Route::get('http://localhost:8000/blogtitles/{id}/edit')->name(blogtitles.edit)

so you would generate it as

route('blogtitles.edit', ['id' => $blogTitle->id] )

In the controller you should do the logic necessary to get the $blogTitle object and send it to the view.

  • Related