Reputation: 2893
I want to store a list of departments in my database. I do not create the departments in my system, I obtain them from Active Directory. In the view for departments, I display all the departments. I also have a button to update departments in the database.
This function at the moment looks like the following
public function updateDepartments()
{
$departments = Helper::returnDepartmentsFromLdap();
dd($departments);
}
The dd produces something like the following
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"
]
You have to remember though that this list is not being obtained from my database, it is being obtained from Active Directory. However, I now want to add them to my database. So in the same function I do the following
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.');
}
Now the problem with the above is that every time the button is pushed, the database will insert new rows. Say I push the button for the first time, my database will be populated with the list. Say I then push it a second time, I do not want the same list added again. If it already exist in the database, it should overwrite it or something. It should only perform a save if a new item is in the list.
Is something like this possible?
Thanks
Upvotes: 1
Views: 2968
Reputation: 21
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.');
}
Little more complex but should be faster
Upvotes: 1
Reputation: 6071
Try this approach:
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.');
}
This is basic idea. Now if this is not clear give me info what you want to update and I will change this impl according to your requirements. For example you could do this:
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.');
}
This is according to eloquent documentation
Other Creation Methods
There are two other methods you may use to create models by mass assigning attributes: firstOrCreate and firstOrNew.
The firstOrCreate method will attempt to locate a database record using the given column / value pairs. If the model can not be found in the database, a record will be inserted with the given attributes.
The firstOrNew method, like firstOrCreate will attempt to locate a record in the database matching the given attributes. However, if a model is not found, a new model instance will be returned. Note that the model returned by firstOrNew has not yet been persisted to the database. You will need to call save manually to persist it:
// 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']);
Upvotes: 2