Skip to content

Instantly share code, notes, and snippets.

@prodeveloper
Last active June 14, 2016 08:48
Show Gist options
  • Save prodeveloper/b4610cf8b5ed4181d74315c442d418b0 to your computer and use it in GitHub Desktop.
Save prodeveloper/b4610cf8b5ed4181d74315c442d418b0 to your computer and use it in GitHub Desktop.
php artisan make:migration create_students_table --create=students

Navigate to the database/migrations folder and select your file

Add the following lines to the code

            $table->string('name');
            $table->string('email');
            $table->string('course');

Now that we have our DB structure setup let us run it by using the commands

php artisan migrate

In case of any problems ensure to have setup your DB as instructed here https://community.c9.io/t/getting-started-with-laravel/1608

Now that the DB is setup lets go ahead and create the corresponding model.

php artisan make:model Student

Go to app folder. You should see a new file with the class of the model you just created.

Our model can now be used to save and retrieve data.

Let us use jump into the laravel interactive mode.

php artisan tinker

A new console should appear. Start adding items

$student = new App\Student();
$student->name = "Jacob Chencha";
$student->email="jacob@chenchatech.com";
$student->course="php";
$student->save();

The student object is now saved in the database.

Exit the terminal by typing exit to the prompt

exit()

To see all students simply go to the terminal once more

App\Student::all();

See more documentation at https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Builder.html

Try out all the other methods available to you via the collection interface

http://daylerees.com/codebright-eloquent-collections/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment