Laravel Basics

Install Composer

Install WAMP

c:\wamp64\www>composer create-project laravel/laravel helloworld

Views (C:\wamp64\www\helloworld\resources\views)

  • C:\wamp64\www\helloworld\resources\views\hi.blade.php

Routes (C:\wamp64\www\helloworld\routes)

C:\wamp64\www\helloworld\routes\web.php

Route::get(‘hii’, function () {
return view(‘hi’);
});


Create a new template file in a new subfolder views\layouts\app.blade.php

<html>
<head>
<title>App Name – @yield(‘title’)</title>
</head>
<body>
@section(‘sidebar’)
This is the master sidebar.
@show

<div class=”container”>
@yield(‘content’)
</div>
</body>
</html>


Create  a new Controller as Http\Controllers\MainController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class MainController extends Controller
{
/**
* Show the profile for the given user.
*
* @param int $id
* @return View
*/
public function show()
{
return view(‘layouts.app’);
}
}


Create the route

Route::get(‘start’, ‘MainController@show’);


Test by pointing location to http://localhost/helloworld/public/start

It will call the MainController.
MainController will load the view


For Authentication (Login Username)

C:\wamp64\www\helloworld1>php artisan make:auth

For Database Creation:

C:\wamp64\www\helloworld1>php artisan migrate


Some Common Errors:

SQLSTATE[HY000] [1045] access denied for the user: ‘homestead’@’@localhost’ (password: YES)

Solution:

Check .env file and make sure you have updated the mysql username and password:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_hello
DB_USERNAME=root
DB_PASSWORD=root

Format of the migration files should be as below:

2018_01_15_100000_create_users_table.php


Laravel Migration Error: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

Open database.php file insde config dir/folder.
Edit
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',

to

'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',


How to refer to site url in views
href=”{{ url(‘/tasks’) }}”


Validate

$validator = Validator::make($request->all(), [
‘title’ => ‘required|email’,
‘description’ => ‘required’,

]);


Fillable | Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
protected $fillable = [‘title’,’description’];
}


Check whether user is logged in or not in controller

use Auth;

if(Auth::check())
{
$tasks = Task::all();
return view(‘tasks.index’,compact(‘tasks’,$tasks));
}
else
{
return redirect(‘/login’);
}


App Name can be changed in .env

APP_NAME=’aaa aaa aaa’

In the template file it can be referenced as {{ config(‘app.name’) }}

Add a Comment

Your email address will not be published. Required fields are marked *