Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将代码从db类转换为雄辩的查询?拉维尔_Php_Mysql_Laravel_Eloquent - Fatal编程技术网

Php 如何将代码从db类转换为雄辩的查询?拉维尔

Php 如何将代码从db类转换为雄辩的查询?拉维尔,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,我想从DB类切换到Eloquent,我有一些疑问 这是第一个,我想在订阅中插入用户的用户id,而硬核订阅的用户id=1作为默认值 DB::table('subscription_user')->insert( ['user_id' => $user->id, 'subscription_id' => 1] ); 第二,我想返回验证令牌 $check = DB::table('user_verifications'

我想从DB类切换到Eloquent,我有一些疑问

这是第一个,我想在订阅中插入用户的用户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 雄辩的等价物(*):

如果这是由于模型
订阅
用户
之间的多对多关系造成的,您只需执行(**):

#2 雄辩的等价物(***):

#3 雄辩的等价物:

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

(*)为了使其工作,您需要一个名为
SubscriptionUser
的雄辩模型,其表属性设置为:
'subscription\u user'

(**)要使其正常工作,您需要


(***)为了使这项工作正常进行,您需要一个名为
UserVerification
的雄辩模型,其表属性设置为:
'user\u verifications'

能否请您重新表述您的问题?不清楚你现在在问什么。如果您需要帮助,请查看这本有用的指南:。哦,我想我知道了,您想使用Eloquent来执行这些操作,对吗?@hck是的,我想将其转换为Eloquent代码。我已链接了所有内容,现在一切正常。现在我要切换到eloquent,让我知道它是否适用于medo我必须更改模型的名称才能进行此eloquent?@AlexAl不,这些只是模型,我从您的代码中了解到,您可以使用任何模型名称,只要确保配置
$table
以及
$primmaryKey
属性(如果您不遵循雄辩的约定)。检查文档以了解更多的模型约定。我的所有用户都在数据库中注册,他们返回我的错误“找不到基表或视图:1146表'devcars.subscription_users'不存在”模型的默认约定(单数)是查找一个表,该表的名称为模型的复数版本。例如,如果模型
角色
,它将查找
角色
表。如果要使用其他表名,则需要指定它。PK也是如此。Laravel将假定您的primmary键是列
id
,但如果您使用不同的列(例如
role\u id
),则需要指定它。模型中有很多参数需要配置,Laravel非常灵活。
$subscriptionUser = SubscriptionUser
                        ::create(['user_id' => $user->id, 'subscription_id' => 1]);
Subscription::find(1)->users()->attach($user->id);
$check = DB::table('user_verifications')->where('token', 
$verification_code)->first();
$check = UserVerification::where('token', $verification_code)->first();
$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.