简体   繁体   English

Laravel,附加请求数据

[英]Laravel, additional request data

Im sending from my form 2 values - title and content.我从我的表单 2 值发送 - 标题和内容。 MySQL table has fields: id, title, content, userID, created_at, updated_at. MySQL 表有字段:id、title、content、userID、created_at、updated_at。 Table was created with laravel migration.表是使用 laravel 迁移创建的。 I dont have to send id, created_at and updated_at.我不必发送 id、created_at 和 updated_at。 Only title, content and userID which I get from Auth::class.只有我从 Auth::class 获得的标题、内容和用户 ID。

public function store(Request $request) {
    //some validation
    $request->request->add(['userID' => Auth::id()]);
    Article::create($request->all());

    // testing purpose
    return $request->all();
}

I know I can do it another way directly in create function using array_merge or simply adding two arrays, but I thought this is better way (?) to do this, but Im getting such error:我知道我可以使用 array_merge 或简单地添加两个 arrays 直接创建 function 以另一种方式,但我认为这是更好的方法(?)这样做,但我得到这样的错误:

SQLSTATE[HY000]: General error: 1364 Field 'userID' doesn't have a default value (SQL: insert into `articles` (`title`, `content`, `updated_at`, `created_at`) values ...

Looks like function create doesnt't see userID value.看起来userID创建没有看到用户 ID 值。 What is funny, if I comment that create line, by return I can see in my brower everything:有趣的是,如果我评论create行,通过返回,我可以在我的浏览器中看到所有内容:

{"_token":"...","title":"Test","content":"Test","userID":1}

Why is it returned correctly and in same time function create doesn't see userID value?为什么它返回正确,同时userID create没有看到用户 ID 值?

Try passing the request body instead: Article::create($request->request->all);尝试传递请求正文: Article::create($request->request->all);

It looks like there might be an error with parsing that added value.看起来解析该附加值可能存在错误。

Also, when you call $request->request->all() , does it have your added userID ?另外,当您调用$request->request->all()时,它是否添加了您的userID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM