Laravel
How to install a virtual box homestead?
- Needs to be on solid drive like c: (not a reference) - Test out different Folders and mapping locations in homestead.yml - To run the command sometimes you need to provide the whole path and not the relative path - Need to download mysql and unzip it into the directory and add appropriate folder and mapping reference
Once you have installed virtualbox how to get in?
1) Start your Git Bash in the directory where your homestead files are 2) Type "vagrant up" into Git Command line to boot up the the virtual machine 3) Type vagrant ssh to get into laravel
How to create a new laravel project?
1) vagrant up and vagrant ssh 2) ls for listing and cd to where all your files are 3) laravel new nameOfProject 4) Open your hosts file as admin and link a name for the app to your 127.0.0.1 (localhost) 5) Also, need to link your app and phpMyAdmin to that virtual box: - map: searchApp.app to: /home/vagrant/code/searchApp/public - map: phpmyadmin.app to: /home/vagrant/code/phpmyadmin 6) Vagrant provision to take effect for the linking 7) Open browser to searchApp.app:8000
Write the following in blade's syntax: <?php foreach ($tasks as $task) : ?> <li><?php echo $task; ?></li> <?php endforeach; ?>
@foreach ($tasks as $taks) <li>{{ $tasks }}</li> @endforeach
How to you connect to model or class
App\Task::all() Route::get("/tasks", function(){ $tasks = App\Task::all(); return view("tasks.index", compact("tasks")); }); Note: its a different slash not like / App = Namespace Task = new class or model
What are the 3 things you need to make to view?
Create 3 things: 1) Controller 2) Model 3) Migration php artisan make:model Post -mc
Route Model Binding
Inside TasksController.php In case of changing variable /task/1 or /task/2: public function show ($is){ $task = Task::find($id); return view("tasks.show", compact("task")); } can be replaced with: public function show (Task $task){ return view("tasks.show", compact("task")); } Note: when you put the same name /resources/view and /app/controller in the argument then you can can rid of the line: $task = Task::find($id);
What does view("pages.about") translate to?
It means go to the following file: // resources/views/pages/about.blade.php
What is blade?
It's laravel's templating engine
What is artisan?
Its like the command part of laravel
How to create a query in laravel?
Route::get("/", function () { $tasks = DB::table("tasks")->get() return view("welcome", compact("tasks")); });
How to pass in variables into the view function?
Route::get('about', function () { return view("about", [ "name" => "Worlds" // this is the variable ]); }); Alternative way: Route::get("about", function(){ return view("about")->with("name", "Worlds") }); Alternative way: Route::get("about", function(){ $name = "Worlds"; return view("about", ["name" => $name]); }); Alternative way: Route::get("about", function(){ $name = "Worlds"; return view("about", compact("name")); });
What do you have to pay attention to when creating a model with a table together?
The name of the Model should be singular and it will automatically create the table in plural form For example: php artisan make:model User -m This is will create two things: 1) Model named User 2) Table named users
How to dump the autoload?
composer dump-autoload
How to use the helper function die & dump?
dd($id);
How to create a Model(class) and migrate (create a table) at the same time?
php artisan make:model Task -m 1) creates a Task as a class (model) 2) create a table for it
How to access through the command line the database data?
php artisan tinker
How to tell if its different folder in resources view?
tasks.index >>> means go to: resources/view/tasks/index.blade.php Route::get("/tasks", function () { $tasks = DB::table("tasks")->get() return view("tasks.index", compact("tasks")); });
How to make shortcut to a directory?
use App\Task; (on top of page) now instead of: $tasks = App\Task::all() you can use $tasks = Task::all()
How to use a wildcard?
{id} Route::get("/tasks/{id}"), function () { >>>Note: we could name it anything we like.