Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

manojvarma's avatar

Inserting if record not exist, updating if exist

HI i am searching for relevant solution from 2 days, i did not get any and i am writing here finally. I have 2 database tables with the columns table :1 "item, stock, used, created_at and updated_at" table:2 "item, brand, price, quantity, updated_at, created_at". In second table whenever we are inserting a record it should add to another row. But in first table if the record is already exist then it should update the stock, otherwise it should add one more new row. can anyone help me with that.

0 likes
22 replies
Flugg's avatar

You can also use the updateOrCreate method:

$user = User::updateOrCreate(['name' => request()->name], [ 
    'foo' => request()->foo
]);

Using the same example code as @tomi from above

11 likes
InaniELHoussain's avatar

for your Table1 model

$table1 = Table1::firstOrNew(['name' => Input::get('name')]); // your data
// make your affectation to the $table1
$table1 ->save();

for your Table2 model

$table2 = Table2::create($arrayOfData); // if the fields are fillables
manojvarma's avatar

I tried your code but i am getting this error now "QueryException in Connection.php line 761: SQLSTATE[HY000]: General error: 1364 Field 'item' doesn't have a default value (SQL: insert into stationary (available, updated_at, created_at) values (12345, 2016-09-30 12:16:52, 2016-09-30 12:16:52))" Here is my controller code

    {
        $item = stationary::firstOrNew(array('item' => Input::get('name'),'available' => Input::get('quantity')));
$item->save();
    }
InaniELHoussain's avatar

like this

$item = stationary::firstOrNew(array('item' => Input::get('name'),'available' => Input::get('quantity')));
$item ->save();
tomopongrac's avatar

Try

$item = Stationary::firstOrNew(array('item' => Input::get('name'),'available' => Input::get('quantity')));
$item->save();
manojvarma's avatar

I changed the code now i am getting this error "QueryException in Connection.php line 761: SQLSTATE[HY000]: General error: 1364 Field 'item' doesn't have a default value (SQL: insert into stationary (available, updated_at, created_at) values (12345, 2016-09-30 12:16:52, 2016-09-30 12:16:52))"

InaniELHoussain's avatar

@manojvarma here is it, make sure your item field is in the fillable list

$item = stationary::firstOrNew(array('available' => Input::get('quantity')));
$item ->save();
manojvarma's avatar

Yes it is fillable here is my form

<form action="stationary-add-item" method="POST">
  <div class="form-group">
    <input type="text" name="item" class="form-control" id="statnewname" placeholder="Enter item name">
  </div>
  <div class="form-group">
    <input type="number" name="available" class="form-control" id="compnewcount" placeholder="Enter count">
  </div>
  <input type="hidden" name="_token" value="{{csrf_token()}}">
  <button type="submit" class="btn btn-primary">Add</button>
</form>
tomopongrac's avatar

you have typo, try now code bellow

$item = Stationary::firstOrNew(array('item' => Input::get('item'),'available' => Input::get('available')));
$item->save();

your input fields is item and available

InaniELHoussain's avatar

@manojvarma in the model it should be something like that

protected $fillable = ['available', 'item'];

and then you can use it like that

$item = stationary::firstOrNew(array('item' => Input::get('item'),'available' => Input::get('quantity')));
$item->save();
manojvarma's avatar

here is my model code


namespace App;

use Illuminate\Database\Eloquent\Model;

class stationary extends Model
{
    protected $table = 'stationary';
     protected $fillable = ['item', 'available'];
}
manojvarma's avatar

Now one more issue. If both the available and item matches only it is updating. but i want it to be like " if item matches then it should update the count" instead it is adding one more row.

tomopongrac's avatar

Than you need this

$item = Stationary::firstOrNew(array('item' => Input::get('item')));
$item->count++;
$item->save();

manojvarma's avatar

@tomi you suggested to give count++ in my thing i have to give available ++. when i give available++ it is incrementing the value but i need it to be add the present entered value to previous value. can you help me in that?

tomopongrac's avatar
Level 51

Does this work

$item = Stationary::firstOrNew(array('item' => Input::get('item')));
$item->count += Input::get('available');
$item->save();
2 likes
yos_gani's avatar

I tried this, but the data won't be updated. Even thou the save() function returned a "true" value :/

Please or to participate in this conversation.