简体   繁体   English

使用Laravel雄辩地更新数据库

[英]Update Database Using Laravel Eloquent

I have a problem updating my database, it is working fine using the general DB:: method but I think it would be better to use Eloquent. 我在更新数据库时遇到问题,使用常规的DB ::方法可以正常工作,但我认为使用Eloquent会更好。

The general DB method I used (which worked): 我使用的常规数据库方法(有效):

\DB::table('subscriptions')->update(array('state' => 'active'));

The Eloquent method I used (and it doesn't work): 我使用的雄辩方法(它不起作用):

    $user = $request->user();
    $user-> subscription-> state = 'active';
    $user-> save();

Thanks for helping me! 感谢您的帮助!

\DB::table('subscriptions')->update(array('state' => 'active'));

When you do that you're updating the state of every row. 当您这样做时,您将更新每一行的状态。 Assuming you have proper relationship setup, you do one of the below. 假设您有正确的关系设置,请执行以下操作之一。

$subscription = $request->user()->subscription;
$subscription->state = 'active';
$subscription->save();

$request->user()->subscription()->update(array('state' => 'active'));

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

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