简体   繁体   English

Laravel中数据库未更新

[英]Database not updating in laravel

I am using axios to post to my api middleware including parameters. 我正在使用axios发布到我的api中间件,包括参数。 But when I try to update the database by submitting the form I get the status 200 using the network tool in chrome but my database does not update. 但是,当我尝试通过提交表单来更新数据库时,使用chrome中的网络工具获得了状态200 ,但我的数据库没有更新。

Here is the Controller Code: 这是控制器代码:

public function update(Request $request, $id)
{
    //
    Mock::find($request['id']);
    $subject = $request['subject'];

    return Mock::where($subject, $subject)->update([
        $subject => $request['list'],
    ]);
}

Here is the vue method: 这是vue方法:

updateDatabase()
{
    // this goes to the api.php middleware in the routes directory 
    let myJSON = JSON.stringify(questionsList);
    axios.post('api/mock',
    {
        id: 1,
        subject: 'english',
        list: myJSON
    })
},

Check your update method: 检查您的update方法:

public function update(Request $request, $id)
{
    // ...
    return Mock::where($subject, $subject)->update([ // <---------
        $subject => $request['list'], // <-------
    ]);
    // ...
}

In where clauses, the first parameter is the column name, not a value. where子句中,第一个参数是列名,而不是值。 The same applies when updating attributes. 更新属性时也是如此。 Check this line: 检查这一行:

Mock::where($subject, $subject)->/** */

Do this instead: 改为这样做:

Mock::where('subject', $subject)->/** */

By the way, I haven't use your way to get request data, but in case it's returning null values, try this approach: 顺便说一句,我没有用您的方式来获取请求数据,但是如果它返回null值,请尝试以下方法:

$subject = $request->get('subject');

Finally, when doing an update operation, the result is a boolean , not the updated record. 最后,当执行update操作时,结果是boolean ,而不是更新的记录。 So, in case you expect to return the updated object, do this instead: 因此,如果您希望返回更新的对象,请执行以下操作:

public function update(Request $request, $id)
{
    Mock::find($id);
    $mock = Mock::where('subject', $subject)->first();
    $mock->update(['subject' => $request->get('list')]);

    return $mock->fresh();
}

All problem is update function 所有问题都是update功能

public function update(Request $request, $id)
{
    //
    $mock = Mock::find($request->id);
    return $mock->update([
        'subject' => $request->subject,
        'list' => $request->list
    ]);
}

Or maybe 或者可能

public function update(Request $request, $id)
{
    return Mock::where('subject', $request->subject)->update([
        'list' => $request->list,
    ]);
}

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

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