Mick79
168
2
General

Posting to database (form validation 101 video)

Posted 1 year ago by Mick79

Hi, in this video Jeffrey shows us various ways to post data from a form to the database. He finally lands on this way:

Post::create(request(['title','body']));

and here is the form

<form method='POST' action='/posts'>
                    {{csrf_field()}}
                    <div class="form-group">
                        <label for="exampleInputEmail1">Title</label>
                        <input type="text" class="form-control" name='title' placeholder="Title">
                    </div>
                    <div class="form-group">
                        <textarea class='form-control' title='body' placeholder='Body' name='body'></textarea>
                    </div>
                    <button type="submit"
                            class="btn btn-primary">
                        Publish
                    </button>
                </form>

I'm struggling to work out how the controller knows what to post where. I have come to the conclusion that for this way to work, you need the "name" fields in your form to match the column names in your database.

Would that be correct?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.