myh34d
myh34d

Reputation: 311

Form inside a foreach loop not working properly

I'm creating a testing blog site in laravel 5.2 and trying to create a page to manage all the categories. So far everything is working fine, but I'd like to have a table with a list of the categories and edit each one inline. Usually I create a table and a foreach loop inside it with a form for every row, but this time it's not working properly. I read around that the form inside a table is a bad practice, so I tried to make it just with divs, but it doesn't work either.

Here is the code of the view:

@foreach($categories as $category)
 <form action="/blog/categories/edit/{{ $category->id }}" method="post">
  <div class="row">
   <input type="hidden" name="_token" value="{{ csrf_token() }}">
   <div class="col-md-2"><input type="text" value="{{ $category->id }}" name="id" class="category-input-id" readonly="readonly"></div>
   <div class="col-md-2">{{ $category->created_at }}</div>
   <div class="col-md-2">{{ $category->updated_at }}</div>
   <div class="col-md-2">
    <label for"edit" class="btn btn-primary"><i class="fa fa-pancil"></i></label>
    <input type="submit" id="edit" name="edit" class="hidden" />
    <label for="delete" class="btn btn-danger"><i class="fa fa-trash"></i></label>
    <input type="submit" id="delete" name="delete" class="hidden" />
   </div>
 </div>
</form>
@endforeach

In the controller:

public function editcategory (Request $request, $id)
{

    dd($request->all());
}

I didn't paste all the code to get the datas because I don't receive the right datas here.

The output is only the first row in the loop! Whenever I click, it only gets the first element.

array:5 [▼
  "_token" => "wUhnsw8AoAuUDsuElALg8nM91bFG4EkUHTEAkkjp"
  "id" => "16"
  "name" => "Category Name"
  "description" => "desc test"
  "edit" => "Send"
]

Where I go wrong ? I also tried to put the form outside the loop and use arrays for the elements (like name="description[]" etc.. ) but I don't know how to get the right one once I pass all the arrays !

Thanks

Upvotes: 4

Views: 1431

Answers (1)

Gouda Elalfy
Gouda Elalfy

Reputation: 7023

in your case you don't need the form, you can do this easily by <a> tag, and also you can output your categories in a table like this:

@foreach($categories as $category)
        <tr>
            <td style="text-align:center">{{$category->field1}}</td>
            <td style="text-align:center">{{$category->field2}}</td>
            <td style="text-align:center">{{$category->field3}}</td>
            <td style="text-align:center">{{$category->field4}}</td>

            <td style="text-align:center"><a href="{{URL::to('/blog/categories/edit/'.$category->id)}}">{{ Lang::get('global.edit')}}</a></td>
            <td style="text-align:center"><a href="{{URL::to('/blog/categories/delete/'.$category->id)}}">{{ Lang::get('global.delete')}}</a></td>
        </tr>
@endforeach

you need to pass the token <input type="hidden" name="_token" value="{{ csrf_token() }}"> when you send the post for security issues, but in your case you don't need post just get by <a> tag

Upvotes: 2

Related Questions