0

I have been following the https://laracasts.com series on Laravel-5 fundamentals, I am little new to all this , MySQL and frameworks..

I used 'php artisan tinker' command to change the attributes for my column. But I made a typo and typed 'event_desciption' instead of 'event_description' Now whenever I try to save it using $events->save(); It gives an Error.

The Columns name I gave :

                        $table->increments('id');
                        $table->string('event_name');
                        $table->integer('cost');
                        $table->text('event_description');
                        $table->text('terms_and_condition');
                        $table->string('organized_by');
                        $table->timestamps();
                        $table->timestamp('published_at');

the typo I made while creating an events object.. $events;

 $events;
=> App\event {#627
     event_name: "My first evenr name",
     cost: 20,
 >event_desciption: " the event description",
 terms_and_condition: " our terms and condition",
 organized_by: "organized by us",
 published_at: Carbon\Carbon {#634
   +"date": "2016-06-07 20:17:44.000000",
   +"timezone_type": 3,
   +"timezone": "UTC",
 },
 updated_at: "2016-06-07 20:19:54",
 created_at: "2016-06-07 20:19:54,

The error it gives :

$events->save(); Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'event_desciption' in 'field list' (SQL: insert into events (event_name, cost, event_desciption, terms_and_condition, organized_by, published_at, updated_at, created_at) values (My first evenr name, 20, the event description, our terms and condition, organized by us, 2016-06-07 20:17:44, 2016-06-07 20:19:54, 2016-06-07 20:19:54))'

How can one Change the column name without without creating an another object to do the same thing?

Thank You :)

1 Answer 1

1

To rename a column, you may use the renameColumn method on the Schema builder. Before renaming a column, be sure to add the doctrine/dbal dependency to your composer.json file.

Schema::table('users', function($table)
{
    $table->renameColumn('from', 'to');
});

This was a quote from the following page:

https://laravel.com/docs/5.0/schema#renaming-columns

So, in your case, you are going to change that line of code to read:

$table->renameColumn('event_desciption', 'event_description');

EDIT:

You may have put the wrong name in your object if your table schema is correct. Change the property of your $events object entitled event_desciption to event_description.

One way you can accomplish this is by deleting and rebuilding the object again in php artisan tinker:

$events->delete();
Sign up to request clarification or add additional context in comments.

3 Comments

I believe OP has the correct column name in the table schema, but assigned the wrong one to the object.
Thank You for your time in understanding my problem and answering it@Kenny, but yes I have assigned the correct column name in the schema, but whilst giving them values I typed the wrong column name ( to the 'events' Object as @Birdman pointed out ) how do I change that without making another object and redoing all the things again.
@SanyamPhirani I recommend deleting the object with the code above and starting over.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.