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

prima's avatar
Level 2

How to add records to related Model

Hello all.

The title might be a bit misleading. I am doing a personal project for my work and decided to make it with Laravel 9. I am learning it still so my terminology knowledge is not the best.

My idea is to create a simple app that would store the records for my inventory. My inventory is mostly drinks or food, so the products have expiration date. Each product can also have one or more EAN codes. What I am basically trying to do is store the expiration date on my products into database, so I know which inventory should be pushed to sell.

I understand how to do the basic CRUD functionality and storing new data is not a problem. However, I already populated my database with the products we have in the inventory (as those products most of the time won't change). My wish is that I add expiring date to the product and the location where it's stored. To do this, I would scan the barcode and based on the scanned EAN the appropriate product would be selected from the database.

Models I have so far:

Product:

public function barcodes() {
    return $this->hasMany('App\Models\Barcode');
}


protected $fillable = [
    'prod_id',
    'prod_name',
];

Migration:

    Schema::create('products', function (Blueprint $table) {
        $table->id('id', 5);
        $table->string('prod_id', 5)->unique();
        $table->string('prod_name', 50);
        $table->timestamps();
    });

Barcode:

public function product() {
    return $this->belongsTo('App\Models\Product');
}

protected $fillable = [
    'prod_id',
    'ean',
];

Migration:

    Schema::create('barcodes', function (Blueprint $table) {
        $table->id();
        $table->string('prod_id', 5);
        $table->string('ean');
        $table->timestamps();

        $table->foreign('prod_id')->references('prod_id')->on('products')->onDelete('cascade');
    });

ExpirationDate:

protected $fillable = [
     'prod_id',
    'location',
    'expiration_date',
    'stock',
];

Migration:

    Schema::create('expiration_date', function (Blueprint $table) {
        $table->id();
        $table->string('prod_id', 5);
        $table->string('location');
        $table->date('exp_date');
        $table->string('stock');

        $table->timestamps();

        $table->foreign('prod_id')->references('prod_id')->on('products');
    });

So what I tried so far is actually adding 'Expiration Date' records thru form with following fields:

  • input field 'Product'
  • input field 'Location'
  • date field 'Expiration Date'
  • input field 'Stock'

The problem I face now is that in the field 'Product' I want to enter the barcode and based on that barcode appropriate product should be selected as we are storing expiration date on Product and not Barcode.

0 likes
6 replies
bobbybouwmann's avatar
Level 88

Your title is a little bit misleading.

Anyway, I would like to give you the advice to stick with the defaults of Laravel. So for you, that means replacing the prod_id field with product_id. This way the relationship will work automatically.

I also noticed that the products table has an id but also a prod_id. What exactly is that? And why are you using prod_id as a string in all the other tables? Normally you would link tables by using the id because that is a lot faster for a database.

For your problem itself. You can retrieve the product through the barcode if you use the relationship.

$barcode = Barcode::with('product')->where('ean', $barcode)->first();
$product = $barcode->product;

Documentation: https://laravel.com/docs/9.x/eloquent-relationships#eager-loading

Let me know if you need more information. I can work out the database schema for you if that makes sense to you.

prima's avatar
Level 2

@bobbybouwmann Thank you for replying, will try it out today.

Our IDs have 5 places, so I couldn't use the default 1,2,3... From what I read on this matter I then decided to make another ID which can be 00001 instead of 1. So my thinking was I can use the default ID for 'laravel stuff' but our actual product_id for things I need. Forgot to add that I also have this in Product model:

protected $primaryKey = 'prod_id';
protected $keyType = 'string';

Sorry about the title, did not know how to title it otherwise.

bobbybouwmann's avatar

@prima The title was a joke ;) Because you started your question with it haha

Let me know what you're findings are ;)

prima's avatar
Level 2

@bobbybouwmann sorry for the late reply, decided to redo the database tables and migrations as you recommended.

Thank you for pointing me into the right direction, ended up doing it slightly different. Added this to my ExpirationDate controller:

    $ean = $request['ean']; 
    $barcode = Barcode::with('product')->where('ean', $ean)->first();
    $product = $barcode->product_id;

I would just have one more follow up question on this. Is there any way outside using javascript to show the product_id under the 'ean' input field? When I scan the barcode the appropriate product gets selected. I know how to add the message after storing data that would show which product was added.

Please or to participate in this conversation.