Eloquent looking for the wrong column

My migration

Schema::create('tickets', function(Blueprint $table)
    {
        $table->increments('id');
        $table->text('subject');
        $table->text('body');
        $table->integer('author_id')->unsigned();
        $table->timestamps();

    });

    Schema::table('tickets', function($table)
    {
        $table->foreign('author_id')->references('id')->on('users');
    });

      

Relations

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


 public function author(){
    return $this->belongsTo('App\User', 'author_id');
}

      

And save the method

    public function store(TicketRequest $request)
{
    $ticket = new Ticket($request->all());
    Auth::user()->createdTickets()->save($ticket);

    return redirect('tickets');
}

      

When I try to save a new ticket I get this error

QueryException on line Connection 6: SQLSTATE [HY000]: general error: 1 table does not have a column named user_id (SQL: insert into "tickets" ("subject", "body", "user_id", "updated_at", "created_at" )

Where else do I need to specify FK โ€‹โ€‹besides relationships?

+3


source to share


1 answer


You need to provide the desired column name in both relationship methods.



public function createdTickets()
{
    return $this->hasMany('App\Ticket', 'author_id');
}

      

+2


source







All Articles