1

i am making alarms for my website and i decided to use Alarmable model as the bearing one, instead of it being just a helper table.

Alarm looks like this

Schema::create('alarms', function (Blueprint $table) {
  $table->id();
  $table->string('name');
  $table->integer('common_interval')->nullable();
  $table->timestamps();
});

and Alarmables like this

Schema::create('alarmables', function (Blueprint $table) {
    $table->id();
    $table->foreignId('alarm_id');
    $table->nullableMorphs('alarmable');
    $table->date('date');
    $table->foreignId('machine_counter_id')->nullable();
    $table->date('next_date')->nullable();
    $table->integer('next_counter')->nullable();
    $table->text('note')->nullable();
    $table->nullableMorphs('executed_by');
    $table->foreignId('submitted_by');
    $table->timestamps();

    $table->foreign('alarm_id')
        ->references('id')
        ->on('alarms');

    $table->foreign('submitted_by')
        ->references('id')
        ->on('users')
        ->onDelete('set null');

    $table->foreign('machine_counter_id')
        ->references('id')
        ->on('machine_counters')
        ->onDelete('set null');
});

everything worked just fine up until now.

when i get my collection

    public function index()
    {
        $alarmables = Alarmable::orderByDesc('date')->take(2000)->get();
        $alarms = Alarm::orderBy('name')->get();

        $alarmables = $alarmables->unique(function ($aa) {
            return $aa['alarm_id'].$aa['alarmable_type'].$aa['alarmable_id'];
        });

        $alarmables = $alarmables->sortBy('date');

        return view('alarmables.index', [
            'alarmables' => $alarmables,
            'alarms' => $alarms
        ]);
    }

i can't call alarmable relation

class Alarmable extends Model
{
    public function alarm()
    {
        return $this->belongsTo('App\Alarm');
    }

    public function alarmable()
    {
        return $this->morphToMany('App\Alarmable', 'alarmable');
        // return $this->morphedByMany('App\Alarmable', 'alarmable');
    }
}

from my blade

<td>{{ $aa->alarmable ? $aa->alarmable->name : '' }}</td>

it gives this error

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'alarmables' (SQL: select `alarmables`.*, `alarmables`.`alarmable_id` as `pivot_alarmable_id`, `alarmables`.`alarmable_type` as `pivot_alarmable_type` from `alarmables` inner join `alarmables` on `alarmables`.`id` = `alarmables`.`alarmable_id` where `alarmables`.`alarmable_id` = 2 and `alarmables`.`alarmable_type` = App\Alarmable) (View: D:\xampp\htdocs\apk\resources\views\alarmables\index.blade.php)

is there an easy way to fix? do i need to use one more model, instead of just Alarmable for everything?

narrei
  • 516
  • 2
  • 6
  • 19
  • 2
    As I'm not a Laravel person - does https://stackoverflow.com/questions/51551812/laravel-defining-a-many-to-many-relationship-with-the-same-table solve this? – Nigel Ren Feb 03 '21 at 09:29
  • im trying to use knowledge from there, but polymorphic tables are a bit more complicated, so no luck so far. – narrei Feb 03 '21 at 09:34

1 Answers1

2

alarmable() method should look like that:

class Alarmable extends Model
{
    public function alarm()
    {
        return $this->belongsTo('App\Alarm');
    }

    public function alarmable()
    {
        return $this->morphTo();
    }
}
IndianCoding
  • 2,602
  • 1
  • 6
  • 12
  • thank you so much! i was goin thru the documentation and now i see where you took this knowledge from, but i was not able to deduct it. – narrei Feb 03 '21 at 10:08