一个功能中有两个插入

时间:2018-06-10 15:26:25

标签: php mysql laravel eloquent

我使用两个插入语句构建一个函数。第一个只在表中插入两个名称字段,第二个从第一个中读取一个id字段,该字段是通过insert语句自动在数据库上递增的。

这是一种正确的方法吗?

public function submit(Request $request)
{
    $this->validate($request, [
        'groupname' => 'required|unique:groups,group_name'
    ]);

    $groups_create = new Group();
    $groups_create->group_name = $request->input('groupname');
    $groups_create->group_admin = Auth::id();
    $groups_create->save();

    $groupid = Group::where('group_name', $request->input('groupname'))->select('idgroups')->first();

    $groups_create_subscription = new GroupSubscription();
    $groups_create_subscription->idgroups = $groupid->idgroups;
    $groups_create_subscription->subscriber = Auth::id();
    $groups_create_subscription->save();

    return redirect('home')->with('success', 'Ihre Gruppe wurde angelegt!');
}   

3 个答案:

答案 0 :(得分:0)

我会改变你当前的代码:

$group = Group::create([
    'group_name' => $request->input('groupname'),
    'group_admin' => Auth::id()
]);

GroupSubscription::create([
    'idgroups' => $group->idgroups,
    'subscriber' => Auth::id()
]);

因为现在我们可以删除完整的Group::where查询,我们可以保证您获得正确的递增ID。

您也可以使用transactions

答案 1 :(得分:0)

如果在db中保存了多个项目,则使用数据库事务。

public function submit(Request $request)
{
    $this->validate($request, [
        'groupname' => 'required|unique:groups,group_name'
    ]);

    DB::beginTransaction();
    $groups_create = new Group();
    $groups_create->group_name = $request->input('groupname');
    $groups_create->group_admin = Auth::id();
    $groups_create->save();

    if (!$groups_create->save()) {
        DB::rollBack();
        return redirect('home')->with('danger', 'Group not saved!');
    }

    $groupid = Group::where('group_name', $request->input('groupname'))->select('idgroups')->first();

    $groups_create_subscription = new GroupSubscription();
    $groups_create_subscription->idgroups = $groupid->idgroups;
    $groups_create_subscription->subscriber = Auth::id();

    if (!$groups_create_subscription->save()) {
        DB::rollBack();
        return redirect('home')->with('danger', 'Group not saved!');
    }

    DB::commit();
    return redirect('home')->with('success', 'Ihre Gruppe wurde angelegt!');
}

您也可以用来保存这个结构。请参阅官方文档https://laravel.com/docs/5.5/eloquent#mass-assignment

$data = [
    'group_name' => $request->input('groupname'),
    'group_admin' => Auth::id()
];

$groups_create Group::craete($data);

答案 2 :(得分:0)

#1(简单)

无需进行新查询即可找到$groupid。你可以这样得到$groupid

$groups_create              = new Group();
$groups_create->group_name  = $request->input('groupname');
$groups_create->group_admin = Auth::id();
$groups_create->save();

$groups_create_subscription             = new GroupSubscription();
$groups_create_subscription->idgroups   = $group_create->idgroups;
$groups_create_subscription->subscriber = Auth::id();

#2(数据库事务)

DB::beginTransaction();

try {
    $groups_create              = new Group();
    $groups_create->group_name  = $request->input('groupname');
    $groups_create->group_admin = Auth::id();
    $groups_create->save();

    $groups_create_subscription             = new GroupSubscription();
    $groups_create_subscription->idgroups   = $group_create->idgroups;
    $groups_create_subscription->subscriber = Auth::id();

    DB::commit();

    return redirect('home')->with('success', 'Ihre Gruppe wurde angelegt!');
} catch (\Exception $e) {
    DB::rollBack();

    return redirect('home')->with('danger', 'Group not saved!');
}

#3(定义关系)

  

Eloquent关系被定义为您的Eloquent模型类的方法。因为,就像Eloquent模型本身一样,关系也可以作为强大的查询构建器,定义关系作为方法提供了强大的方法链接和查询功能

群组模型

class Group extends Model
{
    public function subscriptions()
    {
        return $this->belongsTo('App\GroupSubscription', 'idgroups', 'idgroups');
    }
}

GroupSubscription模型

class GroupSubscription extends Model
{
    public function group()
    {
        return $this->hasOne('App\Group', 'idgroups', 'idgroups');
    }
}

<强> 控制器

$groups_create              = new Group();
$groups_create->group_name  = $request->input('groupname');
$groups_create->group_admin = Auth::id();
$groups_create->save();

$groups_create->subscriptions()->create([
    'subscriber' => Auth::id(),
]);

// It will automaticaly set idgroups

#4(命名约定)

关注PSR standards。另外,请遵循Laravel社区接受的naming conventions