lundi 23 avril 2018

Have form in Laravel that adds multiple records with single field (allows nulls). How can I add additional fields?

My current form only inputs an ISBN and the logged in user's id. It also allows for blank rows in the form. I'm trying to figure out how to also add a field for the title, but I can't get anything to work. I'm guessing that it has to be done as an array, but all the examples that I find online aren't working for me and each example seems to be using different methods. This is my current form without the title field that I'd like to add. Any help or input would be seriously appreciated

Form:

<form action='wanted' method='POST'>
                <div class='form-group'>
                    <table>
                        <tr>
                            <td style="padding-bottom: 3px">
                               <input type="text" name="isbn[]" placeholder="enter isbn...">
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 3px">
                               <input type="text" name="isbn[]" placeholder="enter isbn...">
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 3px">
                               <input type="text" name="isbn[]" placeholder="enter isbn...">
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 3px">
                                <input type="text" name="isbn[]" placeholder="enter isbn...">
                            </td>
                        </tr>
                        <tr>
                            <td style="padding-bottom: 3px">
                                <input type="text" name="isbn[]" placeholder="enter isbn...">
                            </td>
                        </tr>

                        <input type="hidden" name="_token" value="">

                    </table>
                    <button type="submit" id="submit" class="btn btn-default">Add books</button>
                </div>

Here's my controller:

public function wanted(Request $request)
{
    $uid = Auth::user()->id;
    $booklist = Input::get('isbn');
        foreach($booklist as $onebook) {
            if(is_null($onebook)) continue;
                DB::table('wantedbooks')->insert(['isbn' => $onebook, 'user_id' => $uid]);
        }
    return view("bookswantedsubmitted");
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2FcA9NM
via IFTTT

Aucun commentaire:

Enregistrer un commentaire