Michael Kirkegaard
Michael Kirkegaard

Reputation: 379

Laravel redirect route with id variable won't work

I have made an update function to my laravel 4.2 website which work. It updates the table in the database with the right infomation but when i want to redirect to the same view as i came from i get a problem.

the url shows:

http://localhost:8000/admin/page/%7BpageID%7D

when it schould say:

http://localhost:8000/admin/page/1

Here is a link to an image showing the error in my browser:

https://i.sstatic.net/Cy6mO.png

this is from my view ( admin.pages )

@section('content')

    <div class='box box-info'>

        <form method="post" action="/admin/page/{{$page->id}}">
    <div class='box-header'>
            <div class='box-body pad'>
                <input type="text" name="title" value="{{$page->title}}" class="form-control">
            </div>
    </div>
        <div class='box-body pad'>
            <textarea id="editor1" name="text" rows="2" cols="80">
                {{ $page->text }}
            </textarea>
            <br>
            <input type="submit" class="btn btn-primary" value="Opdater">
        </div>
        </form>

    </div>

@stop

This is from my controller ( PageController ) where i have the redirect in the update function.

<?php

class PageController extends BaseController {

    public function index($pageID)
    {
        $data['pageID'] = $pageID;
        $page = Page::find($pageID);
        return View::make('admin.pages', $data)
            ->with(compact('page'));
    }

    public function update($pageID)
    {
        DB::table('pages')
            ->where('id', $pageID)
            ->update(array(
                'title' => Input::get('title'),
                'text' => Input::get('text')));

        return Redirect::route('page')->with('succes', 'Du har sendt en besked');
    }

}

This is the two routes which i use in this case

Route::get('/admin/page/{pageID}', array('as'=>'page', 'uses'=>'PageController@index'));

Route::post('/admin/page/{pageID}', array('as'=>'page', 'uses'=>'PageController@update'));

If you need anymore information just let me know.

Upvotes: 3

Views: 2013

Answers (1)

Bogdan
Bogdan

Reputation: 44586

The page route requires a parameter named {pageID}, and you need to pass that when redirecting:

return Redirect::route('page', ['pageID' => $pageID])->with('succes', 'Du har sendt en besked');

You can read more about this in the Laravel Redirects Documentation.

Upvotes: 4

Related Questions