zianwar
zianwar

Reputation: 3740

Laravel : send data to a controller method with a POST request

I set a nested resource like this in my routes.php file :

Route::resource('channels','ChannelsController');
Route::resource('channels.posts','PostsController');

and so when i want to show all posts on a given channel I would get the channel id form the URI : GET /channels/{channelId}/posts with the method :

// PostsController.php
/**
 * Display a listing of the resource.
 * GET channels/{channelId}/posts/
 * @return Response
 */
public function index($channelId)
{ 
      ...
}

but when i want to POST, the channel id will not get passed to the store method

// PostsController.php
/**
 * Store a newly created post whithin a channel
 * POST channels/{channelId}/posts/
 * @return Response
 */
public function store($channelId)
{
      ... // $channelId is not set
}

I know there's a solution, passing the data with a hidden field in the form, but it is not secure since anyone can edit it and post the wrong id. Please let me know, if you have any solution.

Upvotes: 0

Views: 3494

Answers (1)

zianwar
zianwar

Reputation: 3740

Actually, I found the solution in Laravel documentation itself, here is it so that everyone can take advantage from :

Form::open(array('action' => array('Controller@method', $user->id)))

  • the variable $user->id is passed as argument to the method method, also this last one should recieve an argument as well, like so : method($userId)

Upvotes: 1

Related Questions