Update multiple rows | updateBatch function in codeigniter 4 - deesoft service

Update multiple rows | updateBatch function in codeigniter 4

Deepak Tailor Image
Deepak Tailor - Nov 25 2020
Update multiple rows | updateBatch function in codeigniter 4

Simplify updates for multiple records in CodeIgniter 4 with our efficient update batch function, streamlining your web development workflow. To change the data of any row in the database, we use the update function. With this we can update all the rows simultaneously and can also update with unique id.

Replace data by default id

Replace() function primaries. Which ever we set as primary. Its base is updated.

public function update_record()
{
	$data = [
		'id'	=>	'43',
		'name'	=>	'deepak tailor',
		'age'	=>	201,
		'salary'=>	5400
	];

	// $this->seller database connection
	
	$query = $this->seller->table('users')
		     ->replace($data);
	if($query == true){
		echo "data replace";
	}
	else{
		echo "data fail";
	}
}
Set() function

We use the set function to set the value in a column like this: we have a column of salary to add an instant 500 to it, then we will set it to the set method.

public function update_record()
{
	$query = $this->seller->table('users')
				->set('salary','salary + 500',FALSE)
				->update();
	if($query == true){
		echo "data update";
	}
	else{
		echo "data fail";
	}
}
Set() function with where arguments
public function update_record()
{
	$query = $this->seller->table('users')
				->set('salary','salary + 5500',FALSE)
				->where('id',29)
				->update();
	if($query == true){
		echo "data update";
	}
	else{
		echo "data fail";
	}
}
Update function without argument
public function update_record()
{
	$data = [
		'name'	=>	'DEEPAK TAILOR'
	];
	$query = $this->seller->table('users')
				->where('id',29)
				->update($data);
	if($query == true){
		echo "data update";
	}
	else{
		echo "data fail";
	}
}
updateBatch function

With updateBatch function, we can update multiple rows. The column to which we set the index in updateBatch. Data is updated on the same column base.

public function update_record()
{
	$data = [
		[
			'id'	=>	29,
			'name'	=>	'DEEPAK',
			'salary'=>	5000
		],
		[
			'id'	=>	28,
			'name'	=>	'ARJUN',
			'salary'=>	8000
		]
	];
	$query = $this->seller->table('users')
			 ->updateBatch($data,'id');
	if($query == true){
		echo "data update";
	}
	else{
		echo "data fail";
	}
}
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in