dimanche 25 décembre 2016

Laravel 5.3 Eloquent Column not found

I have been strugling with understanding the eloquent relationships and after strugling with these for the whole day i decided to post my issue here. I'm getting an error when trying to save an entry to the database.

Column not found: 1054 Unknown column 'address' in 'field list' (SQL: insert into invoices (address, invoice, user_id, updated_at, created_at) values (3, 2, 1, 2016-12-25 23:34:44, 2016-12-25 23:34:44))

InvoiceController

public function postInvoiceDetails(Request $request)
{
    $invoice = new Invoice();
    $invoice->address = $request['customer-address'];
    $invoice->invoice = $request['invoice-number'];
    $request->user()->invoices()->save($invoice);
}

User-model

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

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

Address-model

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

Invoice-model

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

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

Invoices-table

Schema::create('invoices', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id');
    $table->integer('address_id');
    $table->integer('invoice_number');
    $table->timestamps();

Addresses-table

$table->increments('id');
   $table->integer('user_id');
   $table->text('address');
   $table->timestamps();

What i am trying to do is:

  1. An user can have many invoices
  2. User can have many addresses (customer addresses)
  3. Invoice has one address is assigned one address_id (id from addresses-table)

What i found out: Changing address function in Invoice-model to address_id() solves the issue, however this shouldn't be needed if i understand the relationships correctly and what happens next is that i get the same error but now with the "invoice" field and at this point i suspect bad defined relationships. Thanks for any help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire