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?