01.Create a laravel 5.4 projects composer create-project laravel/laravel AuthenticationLaravel “5.4.*” We can use larave 5.5 Details…
Category: Laravel
MVC pattern is most popular plan to manage our small to big projects .As a Laravel is follower of MVC pattern ,lets do some blogging on it.
Enable Cors in laravel 5
01.Create a laravel project
1 |
composer create-project laravel/laravel myapp |
02.Create middleware
1 |
php artisan make:middleware Cors |
03.Cors file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public function handle($request, Closure $next) { // return $next($request); // ALLOW OPTIONS METHOD $headers = [ 'Access-Control-Allow-Origin' => '*', 'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE', 'Access-Control-Allow-Headers' => 'Content-Type, X-Auth-Token, Origin, Authorization' ]; if ($request->getMethod() == "OPTIONS") { // The client-side application can set only headers allowed in Access-Control-Allow-Headers return \Response::make('OK', 200, $headers); } $response = $next($request); foreach ($headers as $key => $value) $response->header($key, $value); return $response; } |
04.In kernel.php add in Details…
Generic Repository Pattern implementation with laravel
Create a database in mysql
1 2 3 4 5 |
drop table test; create table test( id int(11) AUTO_INCREMENT PRIMARY KEY, name text ); |
or create model by artisan command
1 |
php artisan make:model Test |
or create Details…
Ajax Form in Laravel with validation
//http://code4fun.io/post/laravel-ajax-register-and-login #view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
<html lang="en"> <head> <title>Custom Validation Rule Laravel 5</title> <meta name="_token" content="{{ csrf_token() }}" /> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" > <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script> $.ajaxSetup({ headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }, }) </script> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">Custom Validation Rule Laravel 5</a> </div> </div> </nav> <div class="container"> <form id='sign-up' action="{{ URL::to('customValiPost') }}" class="form-horizontal" method="post" novalidate> {{csrf_token()}} @if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <!--<input type="text" name="title" class="form-control" style="width:30%" placeholder="Add Odd String" />--> <div class="form-group" id="email-div"> <label class="control-label" for="title">title</label> <input id="title" type="text" placeholder="example@gmail.com" title="Please enter you email" required value="" name="title" class="form-control"> <span class="help-block"> <strong id="form-errors-title"></strong> </span> <span class="help-block small">Your email</span> </div> </span> @if ($errors->has('title')) <span class="help-block"> <strong>{{ $errors->first('title') }}</strong> </span> @endif <br/> <button class="btn btn-primary">Save</button> </form> <script> $(function(){ $('#sign-up').on('submit',function(e){ $.ajaxSetup({ headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }, }) e.preventDefault(e); $("#title-div").removeClass("has-error"); $.ajax({ type:"POST", url:'/laravel1/public/ajax', data:$(this).serialize(), //dataType: 'json', success: function(data){ console.log(data); }, error: function(data){ console.log(data); var obj = JSON.parse(data.responseText); if (obj.title) { $("#title-div").addClass("has-error"); $('#form-errors-title').html(obj.title); } } }) }); }); </script> </div> </body> </html> |
#route Route::get(‘/’,’CustomValDemoController@customVali’); Route::post(‘/ajax’,’CustomValDemoController@ajax’); #conroller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php /** * Created by IntelliJ IDEA. * User: Akash * Date: 1/25/2017 * Time: 9:37 PM */ namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Input; use Illuminate\Validation\Validator; use App\Http\Requests\TestRequest; class CustomValDemoController extends Controller { public function customVali() { return view('customVali'); } public function customValiPost(Request $request) { /* $this->validate($request, [ 'title' => 'required|min:2|max:5|validateFoo', ]);*/ print_r('done'); } public function ajax(Request $request){ $this->validate($request, [ 'title' => 'required', ]); if($request->ajax()){ return response()->json(['responseText' => 'Success!'], 200); } return 'hola'; } } |
Request Validation and Custom Validation with Image File Input in Laravel 5
01.Create a controller
1 |
php artisan make:controller CustomValDemoController |
02.Route
1 2 |
Route::get('/', 'CustomValDemoController@customVali'); Route::post('customValiPost', 'CustomValDemoController@customValiPost'); |
03.Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\URL; class CustomValDemoController extends Controller { // public function customVali() { return view('customVali'); } public function customValiPost(Request $request) { $this->validate($request, [ 'title' => 'required', 'picture' => 'required |image ', ]); $avarta = Input::file('picture'); if(strpos($avarta->getClientMimeType(),'image') !== FALSE){ $upload_folder = '/assets/uploads/'; //$file_name = str_random(). '.' . $avarta->getClientOriginalExtension(); $file_name = $avarta->getClientOriginalName(); $avarta->move(public_path() . $upload_folder, $file_name); echo URL::asset($upload_folder . $file_name); // get upload file url } print_r($request->title); } } |
04.and finally the view uploadfile.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<html lang="en"> <head> <title>Custom Validation Rule Laravel 5</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" > </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="#">Custom Validation Rule Laravel 5</a> </div> </div> </nav> <div class="container"> <form action="{{ URL::to('customValiPost') }}" class="form-horizontal" method="post" enctype="multipart/form-data"> <input name="_token" type="hidden" value="{{csrf_token()}}"/> @if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <input type="text" name="title" class="form-control" style="width:30%" placeholder="Add Odd String" /> @if ($errors->has('title')) <span class="help-block"> <strong>{{ $errors->first('title') }}</strong> </span> @endif <br/> <input type="file" class="form-control" name="picture" id="picture"> <button class="btn btn-primary">Save</button> </form> </div> </body> </html> |
05.Create Details…
Upload file in laravel
#view Select image to upload: #disable CSRF token @Kernel.php #route use Illuminate\Support\Facades\Input; or aliases array Details…
Dependency Injection with laravel
This i called table data gateway pattern where a tables responsibility is handled by a Details…
PHP debug with xdebug with laravel
01.Just download the extension from https://xdebug.org and if you are not sure which .dll to Details…
Random with Laravel
*Command
1 |
php artisan make:command UserGeneratorCommand --command = userGeneratorCommand |
in UserGenerator in
1 |
protected $signature = 'userGeneratorCommand'; and in handle() $this->line('Welcome to the user generator.'); |
add app/Console/Kernel.php
1 2 3 4 5 |
protected $commands = [ // 'App\Console\Commands\QuizStart', 'App\Console\Commands\UserGeneratorCommand', ]; |
and run in console
1 |
php artisan userGeneratorCommand |
https://code.tutsplus.com/tutorials/your-one-stop-guide-to-laravel-commands–net-30349 Details…
Laravel 5.2 basic installation with XAMPP
hello laravel Some demo site may interest http://scriptunited.com/demo/metroskin/ To update LARAVEL 5.2 /install laravel 5.2 Details…