通过post方法提交表单时如何跟踪用户ID

时间:2019-09-02 15:13:07

标签: laravel laravel-5 laravel-5.8

我正在制作待办事项Web应用程序。当用户单击“新建任务”模态时,当他们提交任务时,如何通过用户ID跟踪所有用户及其任务

我已经创建了route(task.store)用户的所有详细信息都存储在数据库中,但没有存储user_id

我的待办事项列表                 新任务                 建立活动                 所有任务

        </div>
        <span style="font-size:30px;cursor:pointer; padding-left:40px" onclick="openNav()">&#9776; open</span>
        <hr>
        <script>
            function openNav() {
                document.getElementById("mySidenav").style.width = "250px";
            }

            function closeNav() {
                document.getElementById("mySidenav").style.width = "0";
            }
        </script>
    </div>
    <div class = "col-9">
        @yield('content')
    </div>
    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-body">
                    <form action = "{{ route('task.store') }}" method = "post">
                        @csrf
                        <div class = "form-group">
                            <label for="task">Task</label>
                            <input type ="text" name ="task" placeholder="I will complete" class = "form-control form-control-lg" id="task">
                        </div>
                        <div class = "form-group">
                            <label for="notes">
                                <textarea class = "form-control form-control-lg" id = "notes" name = "notes" rows = "8" cols = "30">Notes</textarea>
                            </label>
                        </div>
                        <h5>Remind Me</h5>
                        <div class = "form-check-inline">
                            <label class = "form-check-label">
                                <input type = "radio" class = "form-check-input form-check-input-lg" name="remind" value="today">Today
                            </label>
                        </div>
                        <div class = "form-check-inline">
                            <label class = "form-check-label">
                                <input type = "radio" class = "form-check-input form-check-input-lg" name="remind" value ="tomorrow">Tomorrow
                            </label>
                        </div>
                        <div class = "form-check-inline">
                            <label class = "form-check-label">
                                <input type = "radio" class = "form-check-input form-check-input-lg" name="remind" value="week">Week
                            </label>
                        </div>
                        <div class = "form-check-inline">
                            <label class = "form-check-label">
                                <input type = "radio" class = "form-check-input form-check-input-lg" name="remind" value="month">Month
                        </label>
                        </div>
                        <div class = "form-check-inline">
                            <label class = "form-check-label">
                                <input type = "radio" class = "form-check-input form-check-input-lg" name="remind" value ="someday" >Someday
                            </label>
                        </div>
                        <br>
                        <br>
                        priority
                        <h5>Priority</h5>
                        <div class = "form-check-inline">
                            <label class = "form-check-label">
                                <input type = "radio" class = "form-check-input form-check-input-lg" name="priority" value="high">High
                            </label>
                        </div>
                        <div class = "form-check-inline">
                            <label class = "form-check-label">
                                <input type = "radio" class = "form-check-input form-check-input-lg" name="priority" value="medium">Medium
                            </label>
                        </div>
                        <div class = "form-check-inline">
                            <label class = "form-check-label">
                                <input type = "radio" class = "form-check-input form-check-input-lg" name="priority" value="low">Low
                            </label>
                        </div>
                        <div class = "form-group">
                            <button type ="submit" class="btn btn-lg btn-success">Add Task</button>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>
</div>

用户填写新任务时,应根据user_id保存该任务。

1 个答案:

答案 0 :(得分:0)

您需要创建例如具有隐藏类型的输入字段,并将其值设置为user_id

<input type="hidden" name="user_id" value="{{ Auth::user()->id }}">

或直接在添加待办事项的方法中使用此外观

或使用事件,例如

class Todo extends Model {
    public static function boot()
    {
        static::creating(function ($todo) {
            $todo->user_id = Auth::user()->id;
        });
    }
}

https://laravel.com/docs/5.8/eloquent#events