dimanche 29 novembre 2015

'modelName->save' doesn't reference the name of the tabel in the database correctly in laravel

I am trying to learn laravel, and am following a bunch of tutorials

I am trying to save a object I created inside my model to my database table-contact,but when I do the modelObject->save command in tinker it replaces my table name by contacts instead of contact

Now I know about the snake case plural name system in laravel so I explicitly rename my table in the model as follows :

 protected $table='contact';

But still I get the same error as

`'base table or view not found **laravel.contacts**'` 

Here is my migration :

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateContactTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('contact', function (Blueprint $table) {
            $table->increments('id');
            $table->text('address');
            $table->string('email');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('contact');
    }
}

My model which I create like this:

php artisan make:model contact

The model that was created:

  <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class contact extends Model
{
    protected $table='contact';
}

note :that protected $table='contact' was manually added by me later

Now I create object in tinker as :

$contact=new App\contact
    $contact->address='myaddress'
    $contact->email='myemail'

And then try to save the object to the database using

 $contact->save

But like I've said before laravel tries to save it to contacts instead of contact table

Also,The object '$contact' doesn't reference the default values of timestamp and id in the model as it does in the tutorial may be someone can hint me why..



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire