-1

i have a little question on laravel Forms, supposing that i have a 1:n relation, like one post can have many comments, and many comments are for one post only.

I want to have an single edit page for all the comments of one post.

I have in CommentsController@edit :

$post = Post::findOrFail($id);
$comments = $post->comments;
return view('comments.edit', compact('comments'));

Is it possible to make a single form for all those comments ? I mean that i have one button 'Save' for all the comments.

im using laravel 5

thanks

Samir B
  • 152
  • 1
  • 2
  • 13

2 Answers2

0

It is sort of possible. The entire concept does not make much sense to me, but here's what I would do:

  • Make an API controller callable by AJAX;
  • Have an onchange event handler for each comment that sets an onblur event handler that makes an AJAX call to the API endpoint.

A non-AJAX method would be to use HTML form arrays to actually submit dozens/hundreds/thousands of comments with each form submission (possibly going past the php_value post_max_size set in your .htaccess), loop through them on the server, and have PHP possibly spit out Fatal error: Maximum execution time of [xx] seconds exceeded. (I recommend not doing this.)

If you need JavaScript/jQuery/PHP/HTML examples, let me know. The original question was "is it possible", to which the answer is "sort of".

Happy coding.

Community
  • 1
  • 1
Jonathan Voss
  • 1,126
  • 1
  • 9
  • 24
  • Yes I thought about that too, (but the post/comments is an example for the concept, In reality there will be at most 10 records to edit in one page.) If you have an example or an article im interested yes. I think that ajax is the solution if i want one page for all things, even for validate them with the Request – Samir B Jun 07 '15 at 11:39
  • You could use something like `name="comment{{$id}}"` for form generation and then foreach loop through `Input::all()` using `stristr($key, 'comment')`. I won't be able to give a full api/ajax example until sometime tonight or tomorrow, but look at ApiGuard if you're interested in a relatively easy to use api library. – Jonathan Voss Jun 07 '15 at 14:39
0

I think you are trying to use partial forms. if i'm not mistaken, you want to have a form for editing the comments and than just call that form everytime that you need to edit them. i would suggest you to watch this

xhulio
  • 1,093
  • 1
  • 13
  • 32