Laravel Relation One to Many (Polymorphic)

Posted 1 month ago by ravipw1801

Am making a laravel project, Admins & Executives an assign jobs to designers.

Below is the Admin model: Admin.php

public function jobs()
    {
        return $this->hasMany('App\Job');
    }

    public function assignable()
    {
        return $this->morphTo();
    }

Job.php

public function assignor()
    {
        return $this->belongsTo('App\Assignor','assignable');
    }

    public function designer()
    {
        return $this->belongsTo('App\Designer');
    }

    public function jobStatus()
    {
        return $this->hasOne('App\JobStatus');
    }

    public function client()
    {
        return $this->belongsTo('App\Client');
    }

Client.php

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

    public function Executive()
    {
        return $this->morphOne('App\Executive','executable');
    }

Jobs Table

Schema::create('jobs', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('client_id');
            $table->unsignedInteger('designer_id');
            $table->unsignedInteger('jobstatus_id');
            $table->string('title',100);
            $table->text('brief');
            $table->string('references');
            $table->dateTime('deadline');
            $table->string('dimensions')->nullable();
            $table->morphs('assignable');
            $table->timestamps();

            $table->foreign('client_id')->references('id')->on('clients');
            $table->foreign('designer_id')->references('id')->on('designers');
            $table->foreign('jobstatus_id')->references('id')->on('jobstatus');
        });

When i tinker the query $admin->jobs

It shows me this error Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'jobs.admin_id' in 'where clause' (SQL: select * fromjobswherejobs.admin_id= 1 andjo bs.admin_idis not null)'

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.