BTemplates.com

Advertisement

YoGiTeCh....... Powered by Blogger.

About Me

My photo
Mumbai, Maharastra, India

How to login with twitter in laravel 5.4

In this Tutorial will show you how to user login with twitter using laravel/socialite packages. social authentication is very importan...

Contact Form

Name

Email *

Message *

Search This Blog

Subscribe

Translate

Recent Posts

Posts

Pages

Blogger templates

Popular Posts

Monday, 3 April 2017

How to login with twitter in laravel 5.4



In this Tutorial will show you how to user login with twitter using laravel/socialite packages. social authentication is very important because nowadays most of the users will connected with social network like twitter. so in this tutorial i will show you how to login with twitter in laravel 5.4.

1. Install Socialite Package:
In first step install socialite package. so, first open command prompt and run below command :

composer require laravel/socialite
2. Add Providers and aliases :
After install socialite package we should add providers and aliases in config file. so, open config/app.php file and add providers and aliases.
'providers' => [
....
Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
....
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],
3.  Create Google App :
n this step we need twitter Credentials like twitter client id, secret key and redirect URls. so, go to https://dev.twitter.com/rest/public and create twitter app.

4. Config/services.php :
After create app you can copy credentials like client id, secret key and redirect URls.
Now, go to config/services.php file and set twitter client id, secret and redirect URls.

return [
....
'twitter' => [
'client_id' => 'app id',
'client_secret' => 'add secret',
'redirect' => 'http://localhost:8000/auth/twitter/callback',
],
]
5. Add twitter_id column in your users table :

Schema::table('users', function ($table) {
$table->string('twitter_id');
});
6. Add route :
After adding twitter_id column in your users table first we have to add new route for twitter login. so, add below route in routes.php file.
Route::get('twitter', function () {
return view('twitterAuth');
});
Route::get('auth/twitter', 'Auth\AuthController@redirectToTwitter');
Route::get('auth/twitter/callback', 'Auth\AuthController@handleTwitterCallback');

7. RegisterController.php

app/Http/Controllers/Auth/RegisterController.php


<?php namespace App\Http\Controllers\Auth; use App\User; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers; use Socialite; use Auth; use Exception; class RegisterController extends Controller { /* |-------------------------------------------------------------------------- | Register Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users as well as their | validation and creation. By default this controller uses a trait to | provide this functionality without requiring any additional code. | */ use RegistersUsers; /** * Where to redirect users after registration. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return User */ protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); } public function redirectToTwitter() { return Socialite::driver('twitter')->redirect(); } public function handleTwitterCallback() { try { $user = Socialite::driver('twitter')->user(); $userModel = new User; $createdUser = $userModel->addNew($user); Auth::loginUsingId($createdUser->id); return redirect()->route('home'); } catch (Exception $e) { return redirect('auth/facebook'); } } }

8. Login.blade.php

resources/views/auth/login.blade.php

How to login with google+ in laravel 5.4



In this Tutorial will show you how to user login with google using laravel/socialite packages. social authentication is very important because nowadays most of the users will connected with social network like google. so in this tutorial i will show you how to login with google in laravel 5.4.

1. Install Socialite Package:
In first step install socialite package. so, first open command prompt and run below command :

composer require laravel/socialite
2. Add Providers and aliases :
After install socialite package we should add providers and aliases in config file. so, open config/app.php file and add providers and aliases.
'providers' => [
....
Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
....
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],
3.  Create Google App :
n this step we need google Credentials like google client id, secret key and redirect URls. so, go to https://console.developers.google.com/ and create google app.

4. Config/services.php :
After create app you can copy credentials like client id, secret key and redirect URls.
Now, go to config/services.php file and set google client id, secret and redirect URls.

return [
....
'google' => [
'client_id' => 'app id',
'client_secret' => 'add secret',
'redirect' => 'http://localhost:8000/auth/google/callback',
],
]
5. Add google_id column in your users table :

Schema::table('users', function ($table) {
$table->string('google_id');
});
6. Add route :
After adding google_id column in your users table first we have to add new route for google login. so, add below route in routes.php file.
Route::get('google', function () {
return view('googleAuth');
});
Route::get('auth/google', 'Auth\AuthController@redirectToGoogle');
Route::get('auth/google/callback', 'Auth\AuthController@handleGoogleCallback');

7. RegisterController.php

app/Http/Controllers/Auth/RegisterController.php


<?php namespace App\Http\Controllers\Auth; use App\User; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers; use Socialite; use Auth; use Exception; class RegisterController extends Controller { /* |-------------------------------------------------------------------------- | Register Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users as well as their | validation and creation. By default this controller uses a trait to | provide this functionality without requiring any additional code. | */ use RegistersUsers; /** * Where to redirect users after registration. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return User */ protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); } public function redirectToGoogle() { return Socialite::driver('google')->redirect(); } public function handleGoogleCallback() { try { $user = Socialite::driver('google')->user(); $userModel = new User; $createdUser = $userModel->addNew($user); Auth::loginUsingId($createdUser->id); return redirect()->route('home'); } catch (Exception $e) { return redirect('auth/facebook'); } } }

8. Login.blade.php

resources/views/auth/login.blade.php

Thursday, 30 March 2017

How to login with facebook in laravel 5.4



In this Tutorial will show you how to user login with facebook using laravel/socialite packages. social authentication is very important because nowadays most of the users will connected with social network like facebook. so in this tutorial i will show you how to login with facebook in laravel 5.4.

1. Install Socialite Package:
In first step install socialite package. so, first open command prompt and run below command :

composer require laravel/socialite
2. Add Providers and aliases :
After install socialite package we should add providers and aliases in config file. so, open config/app.php file and add providers and aliases.
'providers' => [
....
Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
....
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],
3.  Create Facebook App :
In this step we need facebook Credentials like facebook client id, secret key and redirect URls. so, go to https://developers.facebook.com/ and create facebook app.

4. Config/services.php :
After create app you can copy credentials like client id, secret key and redirect URls.
Now, go to config/services.php file and set facebook client id, secret and redirect URls.

return [
....
'facebook' => [
'client_id' => 'app id',
'client_secret' => 'add secret',
'redirect' => 'http://localhost:8000/auth/facebook/callback',
],
]
5. Add facebook_id column in your users table :

Schema::table('users', function ($table) {
$table->string('facebook_id');
});

6. Add route :
After adding facebook_id column in your users table first we have to add new route for facebook login. so, add below route in routes.php file.
Route::get('facebook', function () {
return view('facebookAuth');
});
Route::get('auth/facebook', 'Auth\RegisterController@redirectToFacebook');
Route::get('auth/facebook/callback', 'Auth\RegisterController@handleFacebookCallback');
7. RegisterController.php

app/Http/Controllers/Auth/RegisterController.php

<?php namespace App\Http\Controllers\Auth; use App\User; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Validator; use Illuminate\Foundation\Auth\RegistersUsers; use Socialite; use Auth; use Exception; class RegisterController extends Controller { /* |-------------------------------------------------------------------------- | Register Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users as well as their | validation and creation. By default this controller uses a trait to | provide this functionality without requiring any additional code. | */ use RegistersUsers; /** * Where to redirect users after registration. * * @var string */ protected $redirectTo = '/home'; /** * Create a new controller instance. * * @return void */ public function __construct() { $this->middleware('guest'); } /** * Get a validator for an incoming registration request. * * @param array $data * @return \Illuminate\Contracts\Validation\Validator */ protected function validator(array $data) { return Validator::make($data, [ 'name' => 'required|max:255', 'email' => 'required|email|max:255|unique:users', 'password' => 'required|min:6|confirmed', ]); } /** * Create a new user instance after a valid registration. * * @param array $data * @return User */ protected function create(array $data) { return User::create([ 'name' => $data['name'], 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); } public function redirectToFacebook() { return Socialite::driver('facebook')->redirect(); } public function handleFacebookCallback() { try { $user = Socialite::driver('facebook')->user(); $userModel = new User; $createdUser = $userModel->addNew($user); Auth::loginUsingId($createdUser->id); return redirect()->route('home'); } catch (Exception $e) { return redirect('auth/google'); } } }
8. Login.blade.php

resources/views/auth/login.blade.php

Tuesday, 28 March 2017

How to create CRUD(create, read, update, delete) operation in laravel



This tutorial will show you how to create CRUD (create, read, update, delete) operation in laravel 5.4. in this CRUD operation you can create post then if you want to edit particular post you can edit post and update it. suppose you don't want particular post you can also delete that post.

1. Create new Project:
we can create project using following command :
composer create-project --prefer-dist laravel/laravel Projectname
2. Create PostController :
now we should create new resource controller as PostController.
php artisan make:controller PostController --resource
app/Http/Controllers/PostController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use App\Post; class PostController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $search = \Request::get('search'); $posts = post::where('title','like','%'.$search.'%')->orderby('id')->paginate(5); return view('Posts.Index')->withPosts($posts); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('Posts.Create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // validate the data $this->validate($request, array( 'title' => 'required|max:255', 'body' => 'required' )); // store in the database $post = new Post; $post->title = $request->title; $post->body = $request->body; $post->save(); //Session::flash('success','The blog post was successfully save!'); return redirect()->route('posts.show' , $post->id); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { $post = Post::find($id); return view('Posts.Show')->withPost($post); } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { $post = Post::find($id); return view('Posts.Edit')->withPost($post); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $this->validate($request, array( 'title' => 'required|max:255', 'body' => 'required' )); // store in the database $post = Post::find($id); $post->title = $request->title; $post->body = $request->body; $post->save(); //Session::flash('success','The blog post was successfully save!'); return redirect()->route('posts.show' , $post->id); } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $post = Post::find($id); $post->delete(); Session::flash('success','The Post was successfully deleted.'); return redirect()->route('posts.index'); } }
3. Create Post table and model :
Now we have to create one table which is Post table and one model so we can create table and model using following command :
php artisan make:model Post --migration
database/migration/CreatePostsTable

<?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('body'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } }
Now Go to app/Post
app/Post

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { // }
4. Add Route :
Route::resource('posts', 'PostController');
5. Create Layouts/app file :
Ok, now we have to create layouts directory and create app.blade.php file inside that folder.

resources/views/layouts/app.blade.php

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel CRUD operation</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>
6. Create index.blade.php :
Ok, now we have to create posts directory and create index.blade.php file inside that folder.

resources/views/posts/index.blade.php

@extends('layouts.app') @section('title','| Show') @section('content') <style type="text/css"> .row { padding-top: 10px; } .icon { background: transparent; border: 0px; padding: 0; outline: 0; font-size: 20px; } .black { color: black; } .blue { color: blue; } .red { color: red; } .jumbotron { margin-bottom: 0px; background-color: skyblue; background-position: 0% 25%; background-size: cover; background-repeat: no-repeat; color: white; } .well{ margin-bottom: 0px; background-color: #ff7256; background-position: 0% 25%; background-size: cover; background-repeat: no-repeat; color: white; } </style> <script> $(document).ready(function(){ $('[data-toggle="tooltip"]').tooltip(); }); </script> <div class="row"> <div class="well"> <h2>My Posts <button type="button" class="btn btn-primary pull-right" data-toggle="modal" data-target="#myModal">Create New Post</button></h2> </div> <br> <div class="jumbotron"> <div class="col-md-offset-4 col-md-4 col-md-offset-4"> {!! Form::open(['method' => 'GET','url' =>'posts', 'class' =>'navbar-form navbar-left','role' =>'search']) !!} <div class="input-group custom-search-form"> <input type="text" name="search" class="form-control" placeholder="Search"> <span class="input-group-btn"> <button type="submit" class="btn btn-default"> <i class="glyphicon glyphicon-search icon"></i> </button> </span> </div> {!! Form::close() !!} </div> <table class="table table-bordered"> <tr> <th>#</th> <th>Title</th> <th>Body</th> <th>Created At</th> <th>Updated At</th> <th>Action</th> </tr> @foreach ($posts as $post) <tr> <td>{{ $post->id }}</td> <td>{{ $post->title }}</td> <td>{{ substr($post->body,0,50) }}{{ strlen($post->body) > 50 ?"..." : '' }}</td> <td>{{ $post->created_at }}</td> <td>{{ $post->updated_at }}</td> <td> {!! Form::open(['route' => ['posts.destroy',$post->id], 'method' => 'DELETE'])!!} <a href="{{ route('posts.edit', $post->id)}}" ><span class="glyphicon glyphicon-pencil black icon" data-toggle="tooltip" title="Edit Post"></span></a> &nbsp; &nbsp; <a href="{{ route('posts.show', $post->id)}}" ><span class="glyphicon glyphicon-eye-open icon blue" data-toggle="tooltip" title="View Post" data-placement="left"></span></a> &nbsp; &nbsp; <!-- <td align="right"><a href="{{ route('posts.destroy', $post->id)}}" class="btn btn-danger">Delete</a></td> --> {!! Form::button('<span class="glyphicon glyphicon-trash red " data-toggle="tooltip" title="Delete Post"></span>', ['type' => 'submit' ,'class' => 'icon' ]) !!} {!! Form::close() !!} </td> </tr> @endforeach </table> <div class="text-center"> {!! $posts->links(); !!} </div> </div> </div> @endsection <div class="modal fade" id="myModal" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title" style="text-align: center;">Create New Post</h4> </div> <div class="modal-body"> {!! Form::open(['route' => 'posts.store','data-parsley-validate' => '']) !!} {{ Form:: label('title','Title:')}} {{ Form::text('title',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: label('body','Body:')}} {{ Form:: textarea('body',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: submit('Create Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }} {!! Form::close() !!} </div> </div> </div> </div>
create second file inside posts folder

7. Create create.blade.php :
resources/views/posts/create.blade.php

@extends('layouts.app') @section('title','| Create New Post') <!-- @section('stylesheets') {!! Html::style('css/parsley.css')!!} @endsection --> @section('content') <style type="text/css"> .row { padding-top: 50px; } </style> <body style="background-image: url(bg.jpg);"> <div class="row"> <div class="col-md-offset-3 col-md-6"> <h1 style="text-align: center;">Create New Post</h1> <hr> {!! Form::open(['route' => 'posts.store','data-parsley-validate' => '']) !!} {{ Form:: label('title','Title:')}} {{ Form::text('title',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: label('body','Body:')}} {{ Form:: textarea('body',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: submit('Create Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }} {!! Form::close() !!} </div> </div><br> </body> @endsection <!-- @section('scripts') {!! Html::script('js/parsley.min.js')!!} @endsection -->

8. Create edit.blade.php :
resources/views/posts/edit.blade.php

@extends('layouts.app') @section('title','| Create New Post') @section('stylesheets') {!! Html::style('css/parsley.css')!!} @endsection @section('content') <style type="text/css"> .row { padding-top: 50px; } </style> <div class="row"> <div class="col-md-offset-3 col-md-6"> <h1 style="text-align: center;">Update Post</h1> <hr> {!! Form::model($post,['route' => ['posts.update',$post->id], 'data-parsley-validate' => '', 'method' => 'put']) !!} {{ Form:: label('title','Title:')}} {{ Form::text('title',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: label('body','Body:')}} {{ Form:: textarea('body',null,array('class' => 'form-control', 'required' => ''))}} {{ Form:: submit('Update Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }} {!! Form::close() !!} </div> </div><br> @endsection @section('scripts') {!! Html::script('js/parsley.min.js')!!} @endsection

9. Create show.blade.php :
resources/views/posts/show.blade.php

@extends('layouts.app') @section('title','| View Post') @section('content') <style type="text/css"> .row { padding-top: 50px; } </style> <div class="row"> <div class="jumbotron"> <a href="{{ route('posts.index')}}" class="btn btn-default">Back</a> <h1>{{ $post->title }}</h1> <p class="load" style="text-align: justify;">{{ $post->body }}</p> <table class="table"> <tr> <td>Created At:{{ $post->created_at }}</td> <td align="right">Updated At:{{ $post->updated_at }}</td> </tr> <tr> <td><a href="{{ route('posts.edit', $post->id)}}" class="btn btn-primary">Edit</a></td> <td align="right"> {!! Form::open(['route' => ['posts.destroy',$post->id], 'method' => 'DELETE'])!!} <!-- <td align="right"><a href="{{ route('posts.destroy', $post->id)}}" class="btn btn-danger">Delete</a></td> --> {!! Form::submit('Delete', ['class' => 'btn btn-danger ' ]) !!} {!! Form::close() !!} </td> </tr> </table> </div> </div> @endsection
Now you can run your project