Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel 4 Redirect to route with 2 parameters

I'm currently trying to implement redirects using

public function store($username,$courseId)
{       
        if (Auth::user()->username == $username && Auth::user()->courses()->where('id', '=', $courseId)->first() != null){
        $course = Course::find($courseId);
        $forum = new Forum();
        $forum->description = Input::get('description');
        $forum->course_id   = Input::get('course_id');
        $forum->save();
        return Redirect::to(route('users.courses.forums.index',Auth::user()->username,$course->id));
        }
    return Redirect::to('/');
}

The parameters in Redirect aren't working. Store is a POST method in ForumController. The parameters that Store received are OK because I don't have problems with validation 'if'. I've could created a forum and save it, but when I try to redirect I have this error

Trying to get property of non-object

And users.courses.forums.index is the name of my URI with Action ForumController@index. This last method needs 2 parameters ($username,$courseid). Like this

public function index($username,$courseId)
{       
        $course = Course::find($courseId);
        $forum = DB::table('forums')->where('course_id',$course->id)->get();
        return View::make('forums.index',compact('course','forum'));    
}
like image 764
user3359775 Avatar asked Apr 24 '14 16:04

user3359775


2 Answers

Why not use Redirect::route() directly and pass your variables as an array?

Something like this should work...

    return Redirect::route('users.courses.forums.index', 
                            array(Auth::user()->username, $course->id));
like image 81
msturdy Avatar answered Nov 14 '22 22:11

msturdy


There two ways

1] you can use Redirect::route() like @msturdy answer

EX:

return Redirect::route('users.courses.forums.index',array(Auth::user()->username, $course->id));

2] you can also use Redirect::action()

EX:

return Redirect::action('ForumController@index',array(Auth::user()->username, $course->id));

Like lavarel Documentation for redirects

like image 33
ahmed hamdy Avatar answered Nov 14 '22 23:11

ahmed hamdy