Migration file example

public function up()
    {
        Schema::defaultStringLength(191); 
        Schema::create('projects', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('description'); 
            $table->timestamps();
        });
    }

Read data with Models

create a new model (found models inside app directory.

php artisan make:model project

Start laravel playground

php artisan tinker

App\Project::all(); – show all records under project table (collection of items)

App\Project::first(); – show all records on the first project.

$project = new App\Project;

$project->title = ‘my first project’; – insert data to title column.

$project->description = ‘my description’; – insert data to description column.

$project; – show insert information.

$project->save();

*project name is project in this example.

More tinker db query Example

App\Project::first()->title; – pull first title from db.

App\Project::all()[0]; – show the first item (like an array).

App\Project::all()[1]->title; – show the second title.

App\Project::all()->map->title; – list of all projects title.

Use inside a Controller example – return as a Json

class ProjectsController extends Controller
{
    public function index() {
        $projects = Project::all();
        //return $projects;

        return view('projects.index', compact('projects')); //pass the projects information

    }
}

inside view/projects/index.blade.php


<body>
        <h1>Projects</h1>

        @foreach ($projects as $project)
                <li> {{ $project->title }} </li>
        @endforeach
</body>