Laravel 4重定向Slug

时间:2015-01-29 04:28:32

标签: redirect laravel laravel-4 laravel-routing

我有一条带有slug酒店名称的酒店景观路线。

Route::get('/hotel/{slug}', function($slug){
    $id  = Hotel::whereSlug($slug)->firstOrFail()->id;
    $hotel = Hotel::find($id);
    return View::make('hotel')
    ->with('hotel', $hotel);
});

我有一个带有postStore方法()

的RoomsController
   <?php

class RoomsController extends BaseController {

public function postStore(){
    $validator = Validator::make(Input::all(), Room::$rules);
    if($validator->passes()){
        $room = new Room;
        $room->name = Input::get('name');
        $room->description = Input::get('description');
        $room->facilities = Input::get('facilities');
        $room->info = Input::get('info');
        $room->price = Input::get('price');
        $room->beds = Input::get('beds');
        $room->no_of_rooms = Input::get('no_of_rooms');
        $room->hotel_id = Input::get('hotel_id');
        $room->save();
        return Redirect::to('hotel/')
        ->with('success', 'Room successfully created!');
    }
    return Redirect::back()
    ->with('error', 'Something went wrong!')
    ->withErrors($validator)
    ->withInput();
  }
}

我的疑问是如何在保存房间后重定向到酒店视图页面(上一条路线)。

1 个答案:

答案 0 :(得分:1)

除非你有什么东西没有告诉我们,否则你有酒店ID,所以只需加载酒店记录,获取slug,然后重定向到网址:

// ...
$room->save();
$hotel = Hotel::find(Input::get('hotel_id'));
return Redirect::to(URL::to('hotel', array($hotel->slug)))
    ->with('success', 'Room successfully created!');
// ...