Saturday, March 17, 2018

How to pass multiple variables to view in Laravel 5

What is the best way to pass multiple variables to view in Laravel 5?  I will give you my structure to pass any data to the blade in laravel 5. in the last post, we learn how to Create Simple CRUD Operations in Laravel 5  you can see before to start, let's start.
anyway some developer can send every variable in PHP compact method I will give you some ways and end you can use any structure stable for you

The first way to pass variables to a blade in laravel

public function index()
{
    $name' = 'Ibrahim Samir';
    $age = '30';
    return view('layouts.home', compact('name', 'age')); 
}
In blade file home.blade.php just add this code to get varibale {{ $name }} and {{ $age }}

The second way to pass variables to blade in laravel


send variable with function in laravel .in controller i set variable out function protected $data = array(); then every function i create 
public function index()
{
    $this->data['name'] = 'Ibrahim Smair'; 
   $this->data['age']= '30'; 
    return view('layouts.home', $this->data);
}
In blade file home.blade.php just add this code to get variable {{ $name }} and {{ $age }}
but I use second way because of it easy and collect many many variables at one time. 
thank you for sharing my post, if you need any help you can leave a comment here.

Thursday, March 8, 2018

Create Simple CRUD Operations in Laravel 5

CRUD definition to create, read, update and delete a record, welcome to focus on simple crud system in Laravel 5  by creating the controllers, database, basic routes and simple views. Every application you need to make crud operations and in this article, we help to find the best way to make crud application. In our previous PHP laravel tutorial, we have learned how to install xampp and composer. ok all right let go.
Create Simple CRUD Operations in Laravel 5
Create Simple CRUD Operations in Laravel 5

Step 1: Install Laravel 5.4(crud)

the important thing to note here before the start is that I’ve specified the route on which we will use resourceful routes list and controller resource.

we need to install fresh Laravel 5.4  version application using a command, So open your terminal OR command prompt and run below command in laravel crud application:

composer create-project --prefer-dist laravel/laravel crud

Step 2:  add database configuration and Eloquent model 

add database configuration (database name, username, password ) for our crud. open the .env file and fill all details like as below:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=add your database here
DB_USERNAME=add user name for databse
DB_PASSWORD=and password (if use mysql defult dont add password)


Step 3: Install package (Html Laravel Collective) from here

we will install laravelcollective/html composer package for form builder in CRUD System, now open your terminal then run below command:
composer require laravelcollective/html
After install package, you have to open config/app.php file and add service provider and alias.

'providers' => [

.........
Collective\Html\HtmlServiceProvider::class,
],
'aliases' => [
..........
// html Collective
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
],

Step 4: Create posts Table and Model (migration)

we are going to create crud application for the post. so we have to create a migration for posts table using Laravel PHP artisan command, so first fire bellow command:
php artisan make:migration create_posts_table

<?php

use
Illuminate\Support\Facades\Schema;
use
Illuminate\Database\Schema\Blueprint;
use
Illuminate\Database\Migrations\Migration;

class
CreatePostsTable extends Migration
{
   
/**
     * Run the migrations.
     *
     * @return void
     */
   
public function up()
    {
        Schema::create(
'posts', function (Blueprint $table) {
           
$table->increments('id');
           
$table->string('title');
            
$table->text('content');
           
$table->timestamps();
       
});
   
}

   
/**
     * Reverse the migrations.
     *
     * @return void
     */
   
public function down()
    {
       
//
   
}
}


After creating the table we have to create a model for "posts" table so just run below command and create a new model:
php artisan migrate
php artisan make:model Posts
Ok, so after running below command you will find app/Posts.php and put bellow content in Posts.php file:

file app/Posts.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model{    protected $table = 'posts';
    protected $fillable = [
        'title', 'content'
    ];}

Step 5: Add Resource Route

In this is a step of crud operations we need to add resource route for post crud application. so open your routes/web.php file and add the following route.

Step 6: Create PostsController

Here, now we should create new controller as PostsController resource from the offical site or use this tutorial. So run below command and create the new controller. bellow controller for creating resource controller.

php artisan make:controller PostsController --resource

After bellow command you will find the new file in this
In this controller will create seven methods by default as bellow methods  in file app/Http/Controllers/PostsController.php

Step 7: Create Blade Files

In this step we have to create blade files:
resources/views/app.blade.php
list item with crud system in Laravel 5

<!DOCTYPE html><html><head>    <title>Laravel 5.4 CRUD Application</title>    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet"></head><body>

<div class="container">    @yield('content')
</div>

</body></html>
resources/views/posts/index.blade.php

@extends('app')


@section('content')
    <div class="row">        <div class="col-lg-12 margin-tb">            <div class="pull-left">                <h2>Laravel 5.4 CRUD Example from scratch</h2>            </div>            <div class="pull-right">                <a class="btn btn-success" href="{{ route('posts.create') }}"> Create New Post</a>            </div>        </div>    </div>

    @if ($message = Session::get('success'))
        <div class="alert alert-success">            <p>{{ $message }}</p>        </div>    @endif


    <table class="table table-bordered">        <tr>            <th>No</th>            <th>Title</th>            <th>Content</th>            <th width="280px">Action</th>        </tr>        @foreach ($posts as $post)
            <tr>                <td>{{ ++$i }}</td>                <td>{{ $post->title}}</td>                <td>{{ $post->content}}</td>                <td>                    <a class="btn btn-info" href="{{ route('posts.show',$post->id) }}">Show</a>                    <a class="btn btn-primary" href="{{ route('posts.edit',$post->id) }}">Edit</a>                    {!! Form::open(['method' => 'DELETE','route' => ['posts.destroy', $post->id],'style'=>'display:inline']) !!}
                    {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
                    {!! Form::close() !!}
                </td>            </tr>        @endforeach
    </table>

    {!! $posts->links() !!}
@endsection
resources/views/posts/show.blade.php
show item with crud system in Laravel 5

@extends('app')


@section('content')
    <div class="row">        <div class="col-lg-12 margin-tb">            <div class="pull-left">                <h2> Show Post</h2>            </div>            <div class="pull-right">                <a class="btn btn-primary" href="{{ route('posts.index') }}"> Back</a>            </div>        </div>    </div>

    <div class="row">        <div class="col-xs-12 col-sm-12 col-md-12">            <div class="form-group">                <strong>Title:</strong>                {{ $post->title}}
            </div>        </div>        <div class="col-xs-12 col-sm-12 col-md-12">            <div class="form-group">                <strong>Body:</strong>                {{ $post->content}}
            </div>        </div>    </div>@endsection
resources/views/posts/create.blade.php
edit item with crud system in Laravel 5

@extends('app')
@section('content')
    <div class="row">        <div class="col-lg-12 margin-tb">            <div class="pull-left">                <h2>Add New Post</h2>            </div>            <div class="pull-right">                <a class="btn btn-primary" href="{{ route('posts.index') }}"> Back</a>            </div>        </div>    </div>

    @if (count($errors) < 0)
        <div class="alert alert-danger">            <strong>Whoops!</strong> There were some problems with your input.<br><br>            <ul>                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>                @endforeach
            </ul>        </div>    @endif


    {!! Form::open(array('route' => 'posts.store','method'=>'POST')) !!}
    <div class="row">        <div class="col-xs-12 col-sm-12 col-md-12">            <div class="form-group">                <strong>Title:</strong>                {!! Form::text('title', null, array('placeholder' => 'Title','class' => 'form-control')) !!}
            </div>        </div>        <div class="col-xs-12 col-sm-12 col-md-12">            <div class="form-group">                <strong>Content:</strong>                {!! Form::textarea('content', null, array('placeholder' => 'Content','class' => 'form-control','style'=>'height:150px')) !!}
            </div>        </div>        <div class="col-xs-12 col-sm-12 col-md-12 text-center">            <button type="submit" class="btn btn-primary">Submit</button>        </div>    </div>    {!! Form::close() !!}


@endsection
resources/views/posts/edit.blade.php
delete item with crud system in Laravel 5

@extends('app')


@section('content')
    <div class="row">        <div class="col-lg-12 margin-tb">            <div class="pull-left">                <h2>Edit Post</h2>            </div>            <div class="pull-right">                <a class="btn btn-primary" href="{{ route('posts.index') }}"> Back</a>            </div>        </div>    </div>

    @if (count($errors) > 0)
        <div class="alert alert-danger">            <strong>Whoops!</strong> There were some problems with your input.<br><br>            <ul>                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>                @endforeach
            </ul>        </div>    @endif


    {!! Form::model($posts, ['method' => 'PATCH','route' => ['posts.update', $posts->id]]) !!}
    @include('posts.form')
    {!! Form::close() !!}


@endsection

we finished the code next step add this command line to run crud system
php artisan serve
go to the browser and open url: http://localhost:8000/posts
your project now run successfully, I hope all that clear and if you have any question please add here, I will check your question and will replay
Create Simple CRUD Operations in Laravel 5 will available on GitHub from this link: Laravel CRUD and available to download from this link: download Laravel CRUD
thank you

Friday, March 2, 2018

Guide to Install Local Server and Composer to Create Laravel 5.4 Application

Guide to Install Local Server and Composer to Create Laravel 5.4 Application


Welcome to my first post in my blog PHP Laravel Tutorial for beginners. And this post we will explain how to install xampp server and composer to create first  Application by Laravel 5.4.We will talk about this subject in 3 steps as follows, Let's go:


Install Xampp in your Localhost

Install Composer in your Localhost

Install Laravel 5.4 on Localhost

First step: Install Xampp in your Localhost

if  need to work on Laravel 5.5 or 5.6 you must have PHP 7 version and lower version 5.5.9 PHP, next

  • Download Xampp from here and then install it 
  • After install check path Like that image 
  • Click on Apache and MySQL (start)

Install Xampp in your Localhost

Second step: Install Composer in your Localhost

  • Download composer from here and then install it 
  • After install you need to check if correctly install by click right on your desktop and choice use composer here 
  • Enter command line "composer" 
  • You must see a screen like that


Install Composer in your Localhost


Third step: Install Laravel 5.4 on Localhost

  • Run this command by cmd
composer create-project --prefer-dist laravel/laravel blog "5.4.*"


After install files you have a list of files, don't worry we will explain that everyone:
  • Folder App: This directory is the meat of the application and contains the main core code.
  1. Folder Console: This directory contains all the custom Artisan commands created
  2. Folder  Exceptions: This directory contains the application’s exception handler and is a good place to add custom exception classes to handle different exceptions thrown by your application
  3. Folder Http: This directory contains all your controllers, middleware and requests
  4. Folder  Providers: This directory contains all your service providers for the application. You can know more about service providers here
  • Folder Bootstrap: This directory contains framework bootstrap as well as configuration files. It also contains Cache directory which contains framework generated cache files
  • Folder Config: This directory contains all your application’s configuration files.
  • Folder Database: This directory contains all database migrations and seeds. You can also store SQLite database file here
  • Folder Public: This directory contains assets like images, js files, and CSS.
  • Folder Resources: This directory contains all view files and LESS or SASS files. It also contains lang directory to store language files.
  • Folder Routes: This directory contains all routes definitions for the application. PHP is the file which receives all the requests to your application and here you can redirect the requests to their respective controller methods.
  • Folder Storage: This directory contains blade templates, session files, cache files and other.
  • Folder Tests: This directory contains all the test files
  • Folder Vendor: This directory contains all composer dependencies
In next session will explain how to create CRUD system by laravel 5.4 and make Authentication and more.

What is Laravel Framework

What is Laravel Framework

What is Laravel Framework
Laravel Framework

Laravel is an easy and free, open-source PHP framework, by Taylor Otwell, meant for the event of net applications following the model–view–controller (MVC) field pattern.
Component of MVC
Component of MVC


Nonetheless powerful framework. Since March 2015, Laravel is considered one of the foremost common PHP frameworks, besides Symfony, Zend, CodeIgniter, Yii2. Its syntax is concise. introduction to Laravel would be even as sharp and easy, serving to developers quickly learn how to create their applications with the framework.

Laravel Version and History

·         Laravel 1 discovered on June 9, 2011.
·         Laravel 2 discovered in September 2011.
·         Laravel 3 discovered in February 2012.
·         Laravel 4 discovered in May 2013.
·         Laravel 5 discovered in February 2015.
·         Laravel 5.1 discovered in June 2015.
·         Laravel 5.2 discovered on December 21, 2015.
·         Laravel 5.3 discovered on August 23, 2016.
·         Laravel 5.4 discovered on January 24, 2017.
·         Laravel 5.5 discovered on August 30, 2017.
·         Laravel 5.6 discovered on February 7, 2018.

Laravel Features

·         Query builder.
·         Eloquent ORM that means advanced PHP implementation of the active record pattern.
·         Restful controllers provide.
·         Bundles provide a modular.
·         uses Composer as a dependency manager to add framework-agnostic and Laravel-specific PHP packages.
·         Class autoloading provides automated loading of PHP classes without the needed to load.
·         Automatic pagination and more.

How to pass multiple variables to view in Laravel 5

What is the best way to pass multiple variables to view in Laravel 5?  I will give you my structure to pass any data to the blade in larave...