Laravel Cheat Sheet

Setup Laravel Environment

  1. Download Bitnami 5.6 WAMP stack
  2. uncomment C:\Bitnami\wampstack-5.6.30–1\apache2\conf\bitnami\bitnami-apps-prefix.conf and uncomment Include “C:/Bitnami/wampstack-5.6.30–1/frameworks/laravel/conf/httpd-prefix.conf”
  3. Restart the servers
  4. add C:\Bitnami\wampstack-5.6.30–1\frameworks\laravel to your windows path
  5. Run the bitnami bash shell by executing ‘use_wampstack.bat’ from the bitnami application root folder.
  6. test your laravel installation by typing php artisan serve [ — port=8000] . Will serve by default on port 8000.

Creating a new project

php artisan key:generate

Working with Databases

mysql -uroot -p
create database databasename;
use databasename;
show tables;
php artisan migratephp artisan make: These are your file generators
// creates the migration stub and the database table as well.
// Creates a migration stub under the /project/database/migrations folder
artisan make:model create_database_name --create=tasks
php artisan migratephp artisan help make:model [Artisan Help Feature]
php artisan migrate:refresh // rolls back and reruns the migrations

Fetching database records ###

$tasks = DB::table('tasks')->get(); //Laravel Query Builderfrom the view get the content from the database:
{{ $task->body }}
dd($id) // die and dump [get more info on this $task = DB::table('tasks')->find($id);
Route::get('/tasks/{task}', function ($id) {
<li><a href="/tasks/{{ $task->id}}">{{ $task->body }}</a></li>
  • If you ever return a database result from a view, Laravel returns it as JSON.

Creating a new database table

$table->integer('post_id');
class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}

Eloquent

php artisan key:generate
php artisan make:model Task -m // make model and a migration filecomposer dump-autoload // in case you get an error message when creating the model and migration.

Blade Templating Language

php artisan make:model task

Tinker

php artisan tinker // boots up Laravel's Shell

Using Controllers

Route::get('/tasks', 'TasksController@index' ); //@index is the index method
php artisan make:controller TasksController

registering routes ###

Route::get('/', 'PostsController@index');
Route::get('/posts/{post}', 'PostsController@show'); // Corresponds to /posts/show.blade.php

Layouts

@yield('content')

Assets

Order of Things

$table->increments('id'); // default
$table->string('title'); // added this one
$table->timestamps(); // default
php artisan migrate
class PostsController extends Controller
{
//add methods
public function index()
{
return view('posts.index')
}
}
@extends('layout')@section('content')
<p>This is my content section, this is only a test.</p>
@endsection
test your creation: php artisan serve

Folder Structure

@extends('layouts.master')
@section('content') // starts all of the content pieces.

Working with Forms

<form method="POST" action="/posts">  GET /posts - view all of the posts
GET /posts/create - create a post
GET /posts/{id}/edit - edit a specific post
GET /posts/{id} - displays a single post specified by the ID in the URL
PATCH /posts - send to database
DELETE /posts/{id}
// @store is the method in the PostController
Route::post('/posts', 'PostsController@store');
//creates a resourceful controller, a boilerplate with all of the types of responses.
artisan make:controller TasksController -r

CSRF — Cross Site Request Forgery

{{ csrf_field() }}
public function store()
{
// dd(request()->all());
// create a new post using the request database
$post = new \App\Post;
$post->title=request('title');
$post->body=request('body');
// save it to the database
$post->save();
// and redirect to the home page
return redirect('/');
}
An alternative to the above is to simply use:Post::create(request(['title', 'body']));

Form Validation

  • At the minimum use HTML 5 form validation
<input type="text" required>
  • Setup server side validation in controller (PostController)
$this->validate(request(), [
'title' => 'required',
'body' => 'required'
]);
@if(count($errors))
<div class="form-group">
<div class="alert alert-danger">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}
</li>
@endforeach
</ul> </div>
</div>
@endif

Autentication

  1. in /routes/web.php a new route:
Auth::routes();
  1. Migration files, which need to be migrated.
  2. Point to the database you want to use. If SQLite, change your .env file and create a database.sqlite file in the /databases folder.
```composer require doctrine/dbal```

is a prerequisite to using SQLite.

You also have to uncomment this line in the php.ini file:


```extension=php_pdo_sqlite.dll```




## Artisan ##

## Blade ##
## Composer ##

## Eloquent ##

##Carbon##

## Github Primer ##
Assume that you have a GitHub account, and that you have created a new repo.

1. Create local directory
2. Initialize Git

```git init```


3. Check status

```git status ```


4. Add files

```git add .```


5. Commit your files

```git commit -m "My commit message" ```


6. Add remote host

```git remote add origin https://github.com/username/myrepo.git```


7. Check remote host info

```git remote -v```


8. Push to remote repo

```git push origin master ```



NOTE: if you wish to override the remote changes with your local use


```git push --force ```



#### Pulling from remote repo ####

1.

```git pull origin master ```



## Linux Notes ##

View processes

```top```

or

```ps aux```



Kill processes

```kill <pid>```



Start Apache

``` sudo apachectl [start | stop | restart]```





```apachectl configtest ```




```httpd -v ```



### Installing MySQL on Mac using Homebrew ###


```brew install mysql```



login to MySQL

```mysql -uroot```



configuration

```/usr/local/etc/opt/phpmyadin.config/inc.php```





```brew services start mysql```




```brew install phpmydmin```


NOTE: you may need to create a symbolic link for PHPMyAdmin

### Installing PHP with Homebrew ###


```brew install php71 | php56 ```


NOTE: you need to change the default PHP path in the Mac OS.


```export PATH=/usr/local/php5/bin:$PATH```



#### Making composer executable ####


```mv composer.phar /usr/local/php/bin/composer```



### Changing PATH ###


``` export PATH=/usr/local/php5/bin:$PATH```




```php --version```



### ATOM Setup ###

c:\ProgramData\ComposerSetup\bin\composer.phar
C:\Bitnami\wampstack-5.6.30-1\php\php.exe

1. Create route
2. Create controller
```apachectl configtest ```
```httpd -v ```### Installing MySQL on Mac using Homebrew ###
```brew install mysql```login to MySQL ```mysql -uroot```configuration ```/usr/local/etc/opt/phpmyadin.config/inc.php``````brew services start mysql```
```brew install phpmydmin```
NOTE: you may need to create a symbolic link for PHPMyAdmin### Installing PHP with Homebrew ###
```brew install php71 | php56 ```
NOTE: you need to change the default PHP path in the Mac OS.
```export PATH=/usr/local/php5/bin:$PATH```#### Making composer executable ####
```mv composer.phar /usr/local/php/bin/composer```### Changing PATH ###
``` export PATH=/usr/local/php5/bin:$PATH```
```php --version```### ATOM Setup ###c:\ProgramData\ComposerSetup\bin\composer.phar
C:\Bitnami\wampstack-5.6.30-1\php\php.exe
1. Create route
2. Create controller
  1. add required method to controller class

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store