Git Product home page Git Product logo

laravel-tutorial's Introduction

Laravel

Lesson 2: Installing Laravel

  1. Install XAMPP
  2. Install composer
  3. Open terminal and type composer -V to check composer version
  4. Type composer global require laravel/installer in terminal to install laravel
  5. To create a new project we use laravel new [project_name]. Example: To create pizzahouse project we use laravel new pizzahouse
  6. cd into pizzahouse dir and then type php artisan serve to host localhost server.

Lesson 3: Passing Data to Views

  1. We can pass the data to views using ['varibale' => ' '] in ./routes/web.php. Example below:
// .\routes\web.php
Route::get('/pizzas', function() {
    return view('pizzas', ['type' => 'vietnam']);
});
  1. In views file, to get infomation we can use {{$varibale}} in .blade.php file. Example bellow:
// .\resources\views\pizzas.blade.php
{{$type}}
  1. We can add multiple values and store it in variable then return it like this:
$pizzas = [
    'type' => 'Pizza Việt Nam',
    'ingredients' => 'Bột, đường, muối, ...',
    'price' => 80000
];

Lesson 4: Blade basic

1. Syntax If Statement

@if(<condition>)
    return
@elseif(<condition>)
    return
@else
    return
@endif()

2. Syntax unless

@unless(<condition>)
    return
@endunless()

3. Syntax php to write php code

@php
// write php code in here
@endphp

Lesson 5: Blade loops

1. For loop syntax

@for(<$temp = start>; <$temp condition>; <$temp++> )
    do
@endfor

2. For loop through list

If we have a list like this one

$pizzas = [
    ['type' => 'hawaiian', 'base' => 'cheesy crust'],
    ['type' => 'volcano', 'base' => 'garlic crust'],
    ['type' => 'veg supreme', 'base' => 'thin & crispy']
];

We can do a for loop through list by using for loop:

@for($i = 0; $i < count($pizzas); $i++)
    <p> {{$pizzas[$i]['type']}} - {{$pizzas[$i]['base']}}</p>
@endfor

Lesson 6: Layout files

The layout file is a template file so that we can re-use it (header, footer, js-scirpt, ...) for another view page. So how to use that?

  1. Create a new folder call layouts in resources/views/ and then we create layout.blade.php file in layouts folder.
  2. In welcome.blade.php use @extends('dir/to/layout/file') and then use the code below to create a section
@section('section-name')
    //write content here!
@endsection()
  1. We can re-write header, script, footer, ... in layout file, then add @yield('section-name) to use the from views folder

Lesson 7: CSS & Images

  1. To re-use css style file, we can create css folder in public folder and then create a css file.
  2. In view file, we add this code below in head tag to use css
<link rel="stylesheet" href="/css/main.css">
  1. We can create a new img folder in public folder to store image that we use in the website, and then use this code bellow to use the img
<img src="/img/pizza-house.png" alt="pizza-house-logo">

Note: We don't need to write href="/public/css/main.css" because everything in public folder is in the root level so that every view page can access that.

Lesson 8: Query Parameters

We can pass data through parameters in the URL like this one bellow

http://localhost:8000/?name=vinh

In the web.php file, we have to use request() function to recive the data. Example below:

$name = request('name');

Then we pass $name variable into return() function, or we can pass request() function direct into return() like this one below:

return view('pizzas', [
    'pizzas' => $pizzas,
    'name' => request('name'),
    'age' => request('age')
]);

Lesson 9: Route Parameters (Wildcard)

There is another way to get the data through parameters in URL like this one bellow

http://localhost:8000/pizzas/1

So how can we do that? We can use the code below

Route::get('/pizzas/{id}', function($id) {
    //Use the $id variable to query the db for the record
    return view('info', [
        'id' => $id
    ]);
});

Then we create a new views blade file call info.blade.php

Lesson 10: Controllers

Insted of create too much Route with a lot of code inside that, we can create a controller to manage a group of function and then we can call that function inside Route. So how to create a controller?

  1. To create controller (In this case we will create a new controller called PizzaController), we have to type this code bellow into a terminal
php artisan make:controller PizzaController

Then, a new controller file will be create in directory /app/Http/Controllers

  1. To call function from Controller, we have to referring to the Controller by using this code bellow
use App\Http\Controllers\PizzaController;

Then we can call function

Route::get('/pizzas', [PizzaController::class, 'function_name']);

Lesson 11: Connecting to MySQL

Create database

Go to terminal and type these code below to create new database

mysql -u root
MariaDB [(none)]> create database pizzahouse;

Set database in environment

Go to .env file and replace DB_DATABASE= match with database name.

Lesson 12: Migration Basics (Work with MySQL database)

Create migration file

Go to terminal and then type the code bellow:

php artisan make:migration create_pizzas_table

Create a column in migration file

We can create some column by add more $table->type('name') in public function up

Execute migration file

Go to terminal and enter this code

php artisan migrate

Lesson 13: More on migration

If we want to check migrate status, just using

php artisan migrate:status

If we want to rollback the last migration

php artisan migrate:rollback

Note: If you have excute migrate file and you want to add more column in that table, DO NOT add more column in old migrate file. Create new migrate file using

php artisan make:migration add_column_to_old_table

Then add new column and then run migrate bellow code again

php artisan migrate

Lesson 14: Eloquent Models

Using Eloquent models to access database

Create a model

php artisan make:model Pizza

Declare Pizza model

Go ahead into PizzaController.php file, then declare the Pizza model using this code below:

use App\Models\Pizza;

Get all data from database

$pizzas = Pizza::all();

Using order by

If you want to orderby ascending, using asc. Else, using desc

$pizzas = Pizza::orderBy('column', 'desc/asc') -> get();

Using where

$pizzas = Pizza::where('column', 'value') -> get();

Using latest

To get the latest value

$pizzas = Pizza::latest() -> get();

Lesson 15: MVC Naming Convention

We should following the naming convention table below image

Lesson 20: Saving Records

To save the records, we will have create a new object

$pizzas = new Pizza();

Then recive data from request web form

$pizzas -> name = request('name');

Finally, we save it into data base

$pizzas -> save();

So we can redirect to homepage using

return redirect('/')

How we can redirect to homepage with new message like "Thanks for your order"?

In controller file, we can return file like this

return redirect('/') -> with('mssg', 'Thanks for your order!');

Then in view file, we can write this code bellow. It will show message while redirect

{{session('mssg')}}

Lesson 21: Arrays & Json

Arrays

How to get array from a web form, we have to create a checkbox and then in name field, we have to pass it as array like this code bellow:

<input type="checkbox" name="toppings[]" value="mushrooms">Mushrooms<br />

Json

How can we convert array into json to pass it in database. First we have to go to Pizza model in app/Http/models/Pizza.php. Then add this code into class Pizza:

protected $casts = [
    'toppings' => 'array'
];

Lesson 22: Removing Records

We have to create a delete method

Route::delete('/pizzas/{id}', [PizzaController::class, 'destroy']);

Then in Controller, we have to find the $id and delete that

public function destroy($id){
    $pizzas = Pizza::findOrFail($id);
    $pizzas -> delete();
    return redirect('/pizzas');
}

Lesson 23: Using SASS

Install dependencies using npm

npm install

In webpack.mix.js file, we have to add sass path like this

mix.js('resources/js/app.js', 'public/js')
    .postCss('resources/css/app.css', 'public/css', [
        //
    ])
    .sass('resources/sass/main.scss', 'public/css');

Then create a new folder call sass in resources folder, then create a new file call main.scss. After writing some css code in that file, then we run this command in terminal

npm run dev

Lesson 26: Protecting Route

Add -> middleware('auth') to the route that we need to hide and force user login

laravel-tutorial's People

Contributors

vinhdevnguyen avatar iamshaunjp avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.