我想从DB类切换到雄辩类,我有几个查询
这是第一个,我想在subscription中插入用户的用户id,并将硬核订阅id=1作为默认值。

    DB::table('subscription_user')->insert(
            ['user_id' => $user->id, 'subscription_id' => 1]
        );

其次,我想返回验证令牌
     $check = DB::table('user_verifications')->where('token',
     $verification_code)->first();

第三,我想更新接受域
       $res =   DB::table('offers')
        ->where('id', $id)
        ->update(['accepted' => 1]);

最佳答案

#1个

DB::table('subscription_user')->insert(
        ['user_id' => $user->id, 'subscription_id' => 1]
    );

雄辩的等价物(*):
$subscriptionUser = SubscriptionUser
                        ::create(['user_id' => $user->id, 'subscription_id' => 1]);

如果这来自于模型SubscriptionUser之间的多对多关系,您可以只做(**):
Subscription::find(1)->users()->attach($user->id);

#2个
$check = DB::table('user_verifications')->where('token',
$verification_code)->first();

雄辩的等价物(***):
$check = UserVerification::where('token', $verification_code)->first();

#3个
$res = DB::table('offers')
    ->where('id', $id)
    ->update(['accepted' => 1]);

雄辩的等价物:
$res = Offer::find($id)->update(['accepted' => 1]); // this will return a boolean.
$offer = Offer::find($id); // this will get the updated record.

(*)要使其工作,您需要一个名为SubscriptionUser的雄辩模型,其中table属性设置为:'subscription_user'
(**)要使其正常工作,您需要set the relationship in the models
(***)要使其工作,您需要一个名为UserVerification的雄辩模型,其中table属性设置为:'user_verifications'

关于php - 如何将代码从数据库类切换到 Eloquent 查询?拉拉韦尔,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55912251/

10-10 18:16