php artisan make:migration create_tasks_table
php artisan make:migration create_tasks_table --create=tasks
$table->increments('id');
$table->integer('user_id');
$table->text('body');
$table->timestamps();
If a mistake happened in a schema of a migration file, just edit it and issue:
`php artisan migrate:refresh`
Slide 2
Laravel query builder: https://laravel.com/docs/5.5/queries
This line reads all the records from the table "tasks" from the database.$tasks = DB::table('tasks')->get();
- - - - - - - - - -
If you return the result in a route function:
$tasks = DB::table('tasks')->get();
return $tasks;
You will get a JSON formate of the table data printed into the browser.
Can help in inspecting the structure of the table.
Install JSON formatter extension into Google chrome.
- - - - - - - - - -
If you return it to the view:
$tasks = DB::table('tasks')->get();
return view('welcome', compact('tasks'));
You will need to read it something like this:
Note that this time $tasks is a collection of objects (not an array)
so to access it, you need this syntax $task->body
<ul>
@foreach ($tasks as $task)
<li>{{$task->body}}</li>
@endforeach
</ul>
- - - - - - - - - -
Now try this:
Route::get('/tasks/{id}', function ($id) {
$task = DB::table('tasks')->where('id', '=', $id)->get();
dd($id); // dump and die
});
You access this route this way: http://localhost:8000/tasks/1
Slide 3
Intoduction to views that are created into folders inside the views folder and how to refer to them in the route.
For example; to access this view file: "views/tasks/show.blade.php"
Inside the route, you return:return view('tasks.show', ['task' => $task]);
You can use dot 'tasks.show' or slash 'tasks/show' 'tasks\show'
- - - - - - - - - -
// This corresponds to "views/tasks/show.index.php"Route::get('/tasks', function () {
$tasks = DB::table('tasks')->get();
return view('tasks.index', ['tasks' => $tasks]);
});
Inside the view "views/tasks/show.ndex.php":
<ul>
@foreach ($tasks as $task)
<li><a href="tasks/{{$task->id}}">{{$task->body}}</a></li>
@endforeach
</ul>
- - - - - - - - - -
// This corresponds to "views/tasks/show.blade.php"Route::get('/tasks/{id}', function ($id) {
$task = DB::table('tasks')->find($id);
return view('tasks.show', ['task' => $task]);
});
Inside the view:
{{ $task->body }}