6. Working With the Query Builder

Description

Laravel 5.4 From Scratch (Laravel) Slide Set on 6. Working With the Query Builder, created by mohammed al-obadi on 08/10/2017.
mohammed al-obadi
Slide Set by mohammed al-obadi, updated more than 1 year ago
mohammed al-obadi
Created by mohammed al-obadi over 6 years ago
9
0

Resource summary

Slide 1

    6. Working With the Query Builder
    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 }}  
Show full summary Hide full summary

Similar

2. Basic Routing and Views
mohammed al-obadi
1. Laravel Installation and Composer
mohammed al-obadi
PHP/Laravel
Ahmed Salem
PHP/Laravel
Ahmed Salem
PHP/Laravel
BERBAGI ILMU
4. Database Setup and Sequel Pro
mohammed al-obadi
3. Laravel Valet is Your Best Friend
mohammed al-obadi
French Intermediate
PatrickNoonan
Cell Structure
daniel.praecox
Key Biology Definitions/Terms
courtneypitt4119
Forms of Business Ownership Quiz
Noah Swanson