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

pi.mont's avatar

Eloquent relationships with data that is not seeded into database

Hey guys, I've been developing a Laravel 5 application that takes RFID tag reads from a hardware reader and inserts them into an sqlite database through HTTP Post. This will be used in a convention environment to track attendees.

I've created a model & migration for my Attendees as well as my Tags with some Eloquent relationships to associate certain db columns like the attendees id (employee_id) and the hex value of the RFID tag (epc) they have attached to their badge. These both worked great with test data that I was seeding, but now that I have the reader inserting data into the database, I'm running into a few issues.

Here are my Models and Migrations which use "employee_id" to associate the Attendee with a specific Tag

class Attendee extends Model
{
    protected $primaryKey = 'employee_id';

    protected $fillable = [
      'tag_id',
      'epc',
      'employee_id',
      'first_name',
      'last_name',
      'company',
      'group',
      'position'
    ];

    public function events() {
      return $this->belongsToMany('App\Event', 'attendee_events');
    }
    public function tags(){
      return $this->hasMany('App\Tag', 'employee_id', 'employee_id');
    }
    public function getEventIdList(){
      return $this->events->lists('id');
    }

}
class Tag extends Model
{
    protected $dates = ['created_at', 'updated_at', 'deleted_at', 'first_seen_timestamp'];

    public function attendee(){
      return $this->belongsTo('App\Attendee', 'employee_id', 'employee_id');
    }
}

Migrations:

        Schema::create('attendees', function (Blueprint $table) {
          $table->increments('id');
          $table->integer('tag_id');
          $table->string('epc');
          $table->integer('employee_id');
          $table->string('first_name');
          $table->string('last_name');
          $table->string('company');
          $table->string('group');
          $table->string('position');
          $table->timestamps();
        });
    Schema::create('tags', function (Blueprint $table) {
      $table->increments('id');
      $table->string('reader_name');
      $table->string('mac_address');
      $table->integer('antenna_port');
      $table->string('epc');
      $table->integer('first_seen_timestamp');
      $table->integer('tag_id')->nullable();
      $table->string('employee_id')->nullable();
      $table->nullableTimestamps();
    });

Since the the reader isn't sending the tag data with an employee_id it's throwing an error when trying to access it. I'm trying to figure out if their is a way to force laravel to check for a relationship based on the epc_id and assign an employee_id after the fact. Everything I've been reading about has been related to the database being seeded by artisan, so I'm a bit lost as to how to get relationships working with a constantly updating database.

Any help would be much appreciated, Thanks!

0 likes
0 replies

Please or to participate in this conversation.