Laravel - Basic


What is the composer command used to create a Laravel project?

composer create-project laravel/laravel --prefer-dist applicationName
composer require jenssegers/mongodb

How can one PHP file include another PHP file?

Use include() or require() with the name of the file inside the parentheses.

What does PSR abbreviate for?

PHP Standard Recommendation.

What is Composer?

It is a package manager for PHP.

What are the overall steps that we need to do in order to use Laravel?

  1. Install Laravel and create a project: composer create-project larval/laravel applicationName
  2. Configure Apache VirtualHost so that the DocumentRoot point to the public folder inside your application folder.

CLI commands:

php artisan serve
php artisan make:auth // Creates the migration file for the user table if we wish to use the default authentication
php artisan queue:table // Generate the migration file for the queue functionality.
php artisan queue:failed-table // Generate the migration file for the queue functionality for when it fails.
php artisan queue:listen
php artisan queue:work
php artisan make:model ModelName
php artisan make:model ModelName -m
php artisan make:controller ControllerName
php artisan migrate

How can we query the database?

$post = Post::all();
$post = Post::find($post_id);
$post = Post::where('id', $post_id)->first();
$post = Post::where('id', '>', $post_id)->first()


{{ URL::to('src/css/main.css') }}
{{ route('signup') }}
{{ Session::token() }}
{{ Request::old('email') }}
{{ $errors->has('email') ? 'has-error' : '' }}

return redirect()->route('dashboard');
return redirect()->back();
return response()->json(['new_body' => $post->body], 200);

$this->validate($request, [
  'email' => 'required|email|unique:users',
  'first_name' => 'required|max:120',
  'password' => 'required|min:4'

@if(count($errors) > 0)
  <div class="row">
    <div class="col-md-6">
        @foreach($errors->all() as $error)

return view('account', ['user' => Auth::user()]);
<input type="text" name="first_name" class="form-control" value="{{ $user->first_name }}" id="first_name">

return redirect()->route('dashboard')->with(['message' => $message]);
    <div class="row">
        <div class="col-md-4 col-md-offset-4 success">

return view('dashboard', ['posts' => $posts]);
@foreach($posts as $post)
    <article class="post" data-postid="{{ $post->id }}">
        <p>{{ $post->body }}</p>
        <div class="info">
            Posted by {{ $post->user->first_name }} on {{ $post->created_at }}
        <div class="interaction">
            <a href="#" class="like">{{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 1 ? 'You like this post' : 'Like' : 'Like'  }}</a> |
            <a href="#" class="like">{{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 0 ? 'You don\'t like this post' : 'Dislike' : 'Dislike'  }}</a>
            @if(Auth::user() == $post->user)
                <a href="#" class="edit">Edit</a> |
                <a href="{{ route('post.delete', ['post_id' => $post->id]) }}">Delete</a>

@if(count($errors) > 0)
    <div class="row">
        <div class="col-md-4 col-md-offset-4 error">
                @foreach($errors->all() as $error)

<input class="form-control" type="text" name="email" id="email" value="{{ Request::old('email') }}">
<input type="hidden" value="{{ Session::token() }}" name="_token">

@if (Storage::disk('local')->has($user->first_name . '-' . $user->id . '.jpg'))
    <section class="row new-post">
        <div class="col-md-6 col-md-offset-3">
            <img src="{{ route('account.image', ['filename' => $user->first_name . '-' . $user->id . '.jpg']) }}" alt="" class="img-responsive">

$user = Auth::user();

$file = $request->file('image');
$filename = $request['first_name'] . '-' . $user->id . '.jpg';
$old_filename = $old_name . '-' . $user->id . '.jpg';
$update = false;
if (Storage::disk('local')->has($old_filename)) {
    $old_file = Storage::disk('local')->get($old_filename);
    Storage::disk('local')->put($filename, $old_file);
    $update = true;
if ($file) {
    Storage::disk('local')->put($filename, File::get($file));
if ($update && $old_filename !== $filename) {

$file = Storage::disk('local')->get($filename);
return new Response($file, 200);

Where should we keep the controllers?

We should keep the controllers inside app->Http->Controllers

Where should we keeps our views?

resources -> views

Where does Laravel keep the routes file?

It is under the app->Http folder

What does a routes file look like?

Route::group(['middleware' => ['web']], function() {
  Router::get('/', function() {
    return view('welcome');

  Router::post('/signup', [
    'uses' => 'UserController@postSignup',
    'as' => 'signup'

Notice the ::get method and the ::post method. These are HTTP methods.

Route::get('/', function () {
    return view('welcome');
Route::post('/signup', [
    'uses' => 'UserController@postSignUp',
    'as' => 'signup'
Route::post('/signin', [
    'uses' => 'UserController@postSignIn',
    'as' => 'signin'
Route::get('/logout', [
    'uses' => 'UserController@getLogout',
    'as' => 'logout'
Route::get('/account', [
    'uses' => 'UserController@getAccount',
    'as' => 'account'
Route::post('/upateaccount', [
    'uses' => 'UserController@postSaveAccount',
    'as' => ''
Route::get('/userimage/{filename}', [
    'uses' => 'UserController@getUserImage',
    'as' => 'account.image'
Route::get('/dashboard', [
    'uses' => 'PostController@getDashboard',
    'as' => 'dashboard',
    'middleware' => 'auth'
Route::post('/createpost', [
    'uses' => 'PostController@postCreatePost',
    'as' => 'post.create',
    'middleware' => 'auth'
Route::get('/delete-post/{post_id}', [
    'uses' => 'PostController@getDeletePost',
    'as' => 'post.delete',
    'middleware' => 'auth'
Route::post('/edit', [
    'uses' => 'PostController@postEditPost',
    'as' => 'edit'
Route::post('/like', [
    'uses' => 'PostController@postLikePost',
    'as' => 'like'

How can we make a model?

php artisan make:model User

Add a -m flag to the above command to create a database migration script automatically.

How can we tell Laravel to create a migration file when we create a model?

php artisan make:model User

The above command create a model. Add a -m flag to the above command to create a database migration script automatically.

What is the purpose of the vendors folder?

That is where Laravel keeps all of its dependencies.

What is the purpose of the .env file?

This is where we keep our configuration settings.

Where does Laravel keep the model files?

Laravel keeps the model files inside the app folder.

What kind of code can we keep inside the model file?

The model file is for application logic.

How should we approach developing an MVC application?

Of course, we should have models, views, and controllers, but before that we should think about the objects (nouns) that can exist in our applications, and what each of those objects can do (action). Typical objects:

  1. Users
  2. Posts
  4. Like
  5. Dislike

What does Laravel uses for its ORM capability?


How can we link to our own CSS file?

Create our main.css file under the public->src->css folder. We first have to create the src->css folder under the public folder first. And the link to our CSS file from our main layout:

<link rel="stylesheet" href="{{ URL::to('src/css/main.css') }}"/>
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License