Laravel 5 - 保存或更新

时间:2016-02-04 11:46:53

标签: laravel laravel-5 laravel-5.1

我想在我的数据库中存储一个部门列表。我不在我的系统中创建部门,我从Active Directory获取它们。在部门视图中,我显示所有部门。我还有一个按钮来更新数据库中的部门。

此功能目前如下所示

public function updateDepartments()
{
    $departments = Helper::returnDepartmentsFromLdap();

    dd($departments);
}

dd产生类似下面的内容

array:16 [▼
  0  => "Department 1"
  8  => "Department 2"
  21 => "Department 3"
  22 => "Department 4"
  29 => "Department 5"
  43 => "Department 6"
  47 => "Department 7"
  48 => "Department 8"
]

您必须记住,虽然此列表未从我的数据库中获取,但它是从Active Directory获取的。但是,我现在想将它们添加到我的数据库中。所以在同一个函数中我做了以下

public function updateDepartments()
{
    $departments = Helper::returnDepartmentsFromLdap();

    foreach($departments as $departmentID => $departmentName) {
        $department = new Department();
        $department->departmentID = $departmentID;
        $department->departmentName = $departmentName;
        $department->save();
    }

    return Redirect::route('departments.index')->with('message', 'Departments updated.');
}

现在问题是,每次按下按钮,数据库都会插入新行。假设我第一次按下按钮,我的数据库将填充列表。说我然后第二次推它,我不想再添加相同的列表。如果它已存在于数据库中,则应覆盖它或其他内容。如果列表中有新项目,它应该只执行保存。

这样的事情可能吗?

由于

2 个答案:

答案 0 :(得分:2)

尝试这种方法:

public function updateDepartments()
{
    $departments = Helper::returnDepartmentsFromLdap();

    foreach($departments as $departmentID => $departmentName) {
        $department = Department::firstOrCreate(['departmentID' => $departmentID, 'departmentName' => $departmentName]);
    }

    return Redirect::route('departments.index')->with('message', 'Departments updated.');
}

这是基本的想法。现在,如果不清楚,请告诉我您要更新的内容,我将根据您的要求更改此impl。例如,你可以这样做:

public function updateDepartments()
{
    $departments = Helper::returnDepartmentsFromLdap();

    foreach($departments as $departmentID => $departmentName) {
        $department = Department::firstOrNew(['departmentID' => $departmentID]);
        $department->departmentName = $departmentName;
        $department->save();
    }

    return Redirect::route('departments.index')->with('message', 'Departments updated.');
}

根据eloquent documentation

其他创作方法

您可以使用另外两种方法通过批量分配属性来创建模型:firstOrCreate和firstOrNew。

firstOrCreate 方法将尝试使用给定的列/值对定位数据库记录。如果在数据库中找不到该模型,则将插入带有给定属性的记录。

firstOrNew 方法,与firstOrCreate一样,将尝试在与给定属性匹配的数据库中查找记录。但是,如果未找到模型,则将返回新的模型实例。请注意,firstOrNew返回的模型尚未保留到数据库中。您需要手动调用save来保存它:

// Retrieve the flight by the attributes, or create it if it doesn't exist...
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);

// Retrieve the flight by the attributes, or instantiate a new instance...
$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);

答案 1 :(得分:1)

 public function updateDepartments()
{
    $departments = Helper::returnDepartmentsFromLdap();
    $savedDepartments = Department::select('departmentID')->get();

    foreach ($departments as $departmentID => $departmentName) {
        $alreadySaved = false;
        foreach ($savedDepartments as $item) {
            if ($item->departmentID == $departmentID) {
                $alreadySaved = true;
                break;
            }
        }

        if (!$alreadySaved) {
            $department = new Department();
            $department->departmentID = $departmentID;
            $department->departmentName = $departmentName;
            $department->save();
        }
    }

    return Redirect::route('departments.index')->with('message', 'Departments updated.');
}

稍微复杂但应该更快