【问题标题】:How correctly store data with laravel vue axios使用 laravel vue axios 如何正确存储数据
【发布时间】:2020-04-04 14:58:03
【问题描述】:

我正在尝试从 vue 组件向数据库发送数据。

vue 组件

                ...<form>
                    <div class="form-group">
                        <label for="name">Nom de la tache</label>
                        <textarea name="name" id="name" rows="4" class="form-control" v-model="name"></textarea>
                    </div>
                </form>...


<script>
export default {
    data() {
        return {
            name: ''
        }
    },

    methods: {
        taskStore() {
            axios.post('http://127.0.0.1:8000/taskStore', {
                name: this.name,
            })
                .then(response => console.log(response))
                .catch(error => console.log('une erreur '+error));
        }
    }
}
</script>

道路

Route::post('/taskStore', 'TaskController@store');

和控制器

public function store(Request $request)
    {
        Task::create($request);
    }

请有人看到我做错了什么? 我得到了这个错误,我暂时找不到原因。 提前致谢

【问题讨论】:

  • “我收到这个错误”什么错误,它说什么?

标签: laravel vue.js axios


【解决方案1】:

添加到上述 AWEMA Agence Web Marketing 的答案(因为我还不能添加 cmets)

您还需要确保 Task 模型上的 name 属性是可填充的

class Task extends Model {

   protected $fillable = ['name'];

}

【讨论】:

    【解决方案2】:

    我只是忘记在我的 store 函数中的 $request 之后添加 ->all()...

    public function store(Request $request)
        {
            Task::create($request->all());
        }
    

    【讨论】:

      猜你喜欢
      • 2019-12-17
      • 2021-01-04
      • 1970-01-01
      • 2020-01-23
      • 2022-11-28
      • 2021-07-22
      • 2017-09-27
      • 1970-01-01
      • 2021-02-24
      相关资源
      最近更新 更多