Let's use the Insert function to save a new record in a table. In this function, data is saved in the array format. The field we allow is the data insert in those fields.
public function insert_data()
{
$data = [
'name' => 'dhiru bhai',
'age' => '22',
'salary'=> '15600',
'created_at' => date('Y-m-d H:i:s')
];
$result = $this->userModel->insert($data);
if($result){
echo "insert data";
}
else{
echo "insert fail";
}
}
To update a record in a table, use the update function. It checks the argument on the primary key.
public function update_data()
{
$data = [
'name' => 'update user',
'age' => 29,
'salary'=> 12600,
'updated_at' => date('Y-m-d H:i:s')
];
$id = 9;
$result = $this->userModel->update($id,$data);
if($result){
echo "update record";
}
}
public function update_data()
{
$data = [
'age' => 20,
'updated_at' => date('Y-m-d H:i:s')
];
$id = array(1,3,5,7);
$result = $this->userModel->update($id,$data);
if($result){
echo "update record";
}
}
public function update_data()
{
$data = [
'age' => 22,
'updated_at' => date('Y-m-d H:i:s')
];
$id = array(1,3,5,7);
$result = $this->userModel
->whereIn('id',$id)
->set($data)
->update();
if($result){
echo "update record";
}
}
With the save function, we can insert a new record and also update the record. It checks the value on the primary key.
public function save_data()
{
$data = [
'age' => 32,
'updated_at' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s')
];
$result = $this->userModel->save($data);
if($result == true){
echo "save data";
}
else{
echo "save fail";
}
}
public function save_data()
{
$data = [
'id' => 11,
'age' => 26,
'name' => 'dell laptop',
'updated_at' => date('Y-m-d H:i:s'),
'created_at' => date('Y-m-d H:i:s')
];
$result = $this->userModel->save($data);
if($result == true){
echo "save data";
}
else{
echo "save fail";
}
}
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