Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jbowman99's avatar

Pass an Array of Objects to Controller

I am adding a select all function to my API, the user will have the ability to either select 1, 2, 3, etc items or select all and submit them as a form to a controller method that will then publish each item which creates text files and so on.

The old way i had written each item had a button attached to it by pressing the button on each individual item, would fire the publish method.

<a class="btn btn-default btn-success" href = "{{action('ObitController@publish', [$obit->id])}}">Publish</a></td>

I would like to be able to use the same publish method, and pass it an array of objects, i have added a checkbox to each table row item, and a script that selects all. when I hit submit i have the publish method dd($request->all());

the dump is showing the proper checkbox amount has been checked but no other information is being passed to the method.

here is the view blade table

<tbody>
        <?php $date = new DateTime('tomorrow') ?>
        {!! Form::open(['URL' => '/publish', 'class' => 'obit-form']) !!}
        @foreach ($obits as $obit)
            @if ($obit->published_at == $date)
                    <tr>
                        <td>{!! Form::checkbox('publish', $obit->id, false) !!}</td>
                        <td>{{$obit->first_name}} {{$obit->last_name}}
                            @if($obit->veteran_flag == true)
                                <span class="glyphicon glyphicon-flag"></span>
                            @endif
                            @if($obit->photo != null )
                                <span class = "glyphicon glyphicon-picture"></span>
                            @endif
                        </td>
                        <td>{{ $obit->created_at->format('M d Y - g:i A')}}</td>
                        <td>{{$obit->published_at->format('M d Y')}}</td>
                        {{--<td><a class="btn btn-default  btn-success" href = "{{action('ObitController@publish', [$obit->id])}}">Publish</a></td>--}}
                    </tr>
            @endif
        @endforeach
        </tbody>
    </table>
            <button class="btn btn-success btn btn-success">Submit</button>
        {!! Form::close() !!}

Controller Method I am trying to hit


public function publish(Request $request)
    {
        dd($request->all());
        $obit = Obit::findOrFail($request->all());
        $obit->publishObitFile($obit);
        $obit->status = 'published';
        $obit ->update($request->all());
        return redirect('dashboard');
    }

once i can pass the publish method an array of objects that have been selected I want to loop over each object and run the publishObitFile() method on each object

0 likes
3 replies
jlrdw's avatar

Well when you post a form there will be a request array, I don't know if this is what you meant or not.

jekinney's avatar
Level 47

With what your describing I generally set up an array (which you did) that has an id or unique slug, id, something that establishes what the check box is for.

Pass that array to your backend and loop through it and do your logic. It's not the best idea to pass a full object from the front end in your html generally. It's easy to start passing data around that shouldn't be and it may seem easier but it really isn't. Your views should only have the nessesary data and nothing more. It helps with security and speed/performance. Both are very important.

1 like
jbowman99's avatar

@jekinney

I ended up figuring this out exactly as you spelled it out for me, I was passing the view just the id and getting what i needed from the model on the back end. Just took me a minute to realize what data i was looking at, then it dawned on me what i had.

Thanks

1 like

Please or to participate in this conversation.