renegadson
renegadson

Reputation: 33

Eloquent looks for 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 store 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 recive this error

QueryException in Connection.php line 624: SQLSTATE[HY000]: General error: 1 table tickets has no column named user_id (SQL: insert into "tickets" ("subject", "body", "user_id", "updated_at", "created_at")

Where else i need to specify the FK except the relations?

Upvotes: 3

Views: 1857

Answers (1)

Hkan
Hkan

Reputation: 3383

You need to specify the desired column name in both of the relation methods.

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

Upvotes: 5

Related Questions