Search

Laravel4's Tags

Laravel 5.6 - Google Firebase Notification In Android
Today, We are going to share with you guys how to send push notification in android with google fire-base using laravel. Sometime ago we have worked on one android application application managed in laravel. so, we need to fire a push notification on every android device when admin insert any new product from laravel application. so, we are done this push notification task with google firebase and google firebase is awesome for send push notification and other more functionality. today we are share with you how to work with google firebase for send push notification in android devise. Simply foolow this step and done your push notification in android with google firebase using laravel backend. [ADDCODE] What we done in this article: We are create one simple post table and when admin insert any post then automatic fire notication on register user's mobile by firebase token. so first you must be also get google firebase token from users android mobile by your android app. Create New Project In Firebase: First we need to create one fresh project in google firebase. you can create your project click by this link Google Firebase Console How to crete your first project in google firebase please visite our this link How to create google firebase project. After create your google firebase project you must be get following things from your created project. 1.) api_key 2.) auth_domain 3.) database_url 4.) secret 5.) Legacy server key This all things you got from your google firebase project easily Create post table migration: Now, we are create one post table migration run by following command php artisan make:migration create_post_table After run this command in your database/migration folder. so, open it and copy past following code in this file. use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * @return void */ public function up() { Schema::create('post', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('description'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('post'); } } Then run your post table migration by run following command. php artisan migrate Create Route: Now create folloeing resource route in routes/web.php file. Route::resource('post', 'PostController'); Create PostController: Now create PostController.php in app/Http/Controllers/ folder and write following code in store method so when your post create then Notification automaticly fire of users mobile by google firebase namespace App\Http\Controllers; use App\Http\Requests; use Illuminate\Http\Request; use App\Post; class PostController extends HomeController { public function __construct() { parent::__construct(); $this->Post = new Post; } public function store(Request $request) { $this->validate($request, [ 'title' => 'required|max:255', 'description' => 'required', ]); $input = array_except($request->all(),array('_token')); $this->Post->AddData($input); $notification = \DB::table('api_users')->get(); foreach ($notification as $key => $value) { $this->notification($value->token, $request->get('title')); } \Session::put('success','Post store and send notification successfully!!'); return redirect()->route('post.index'); } public function notification($token, $title) { $fcmUrl = 'https://fcm.googleapis.com/fcm/send'; $token=$token; $notification = [ 'title' => $title, 'sound' => true, ]; $extraNotificationData = ["message" => $notification,"moredata" =>'dd']; $fcmNotification = [ //'registration_ids' => $tokenList, //multple token array 'to' => $token, //single token 'notification' => $notification, 'data' => $extraNotificationData ]; $headers = [ 'Authorization: key=Legacy server key', 'Content-Type: application/json' ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$fcmUrl); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification)); $result = curl_exec($ch); curl_close($ch); return true; } } In this store method you cann look we are get token from api_users table so we should be first get google firebase token from your android side app and that token store in your database. so, you can fire easily notification for all user. If you can test notification manualy then you can use following code for testing for one mobile devise. public function test() { $fcmUrl = 'https://fcm.googleapis.com/fcm/send'; $token='diWhHpEdy1k:APA91bHfaE_zy4FUJ_GGDmO3XuJNz5qshyMeyjbIvvdLKI-DkR5rzhS00k9Hwc49yKzJLUraUPbu9-H-XOv8hbT-q-omtzXa8-uAv8Ewej52zO1gH0maKoGP4FLCu9FwVlLSpwBDC_3T'; $notification = [ 'body' => 'this is test', 'sound' => true, ]; $extraNotificationData = ["message" => $notification,"moredata" =>'dd']; $fcmNotification = [ //'registration_ids' => $tokenList, //multple token array 'to' => $token, //single token 'notification' => $notification, 'data' => $extraNotificationData ]; $headers = [ 'Authorization: key=Legacy server key', 'Content-Type: application/json' ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$fcmUrl); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fcmNotification)); $result = curl_exec($ch); curl_close($ch); dd($result); } Legacy server key you can seen in your firebase setting then go in secound cloud messaging tab We are hope you like this tutorials, if any question regarding any query please post your question in our forums click on bellow link Laravelcode's Forums
Excel and csv import export using maatwebsite in laravel example
We are share with you in this tutorials aboute excel/csv file export and import in laravel usign one of the best laravel package maatwebsite/excel. maatwebsite/excel is provide large functionality of excel or csv file import and exort in laravel. When you are working on very larg ERP and e-commerce related product in laravel framwork then you are need to some time excel import and export functionality for reduce data entry in database and easyly any databe data export from database table to excel file and csv file for backup purpose. So, how to implement excel import export functionality in your laravel application usinf maatwebsite/excel package. we are write here some basice step for implement and integrate excel import and export functionality in laravel. In this demo example e are create one post table and we are export this table data into excel or csv file and also import excel file data into this posst table. our table look like this.. +----+-------+-------------+ | id | title | description | +----+-------+-------------+ | | | | +----+-------+-------------+ | | | | +----+-------+-------------+ Follow Bellow Few Step: 1) Install Required Packages 2) Configuration app.php file 3) Create route 4) Create controller 5) Create view file Step : 1 Install Required Packages First we are need to install maatwebsite/excel package in your laravel application. here is to way for install package in laravel First way : Open your composer.json file and simply put into it package name and package version like that "require": { "php": ">=5.6.4", "laravel/framework": "5.4.*", "laravel/tinker": "~1.0", "maatwebsite/excel": "~2.1.0" }, After put package into composer.json file then after save it and run following command and your required package install in your laravel application. composer update Second way : In second methos for install any package it very easy. open terminal and run following command then you can install your any required package. composer require maatwebsite/excel Step : 2 Configuration app.php file After installing package we are need to configure php.php file. open your confige/app.php file and changes some following 'providers' => [ .... 'Maatwebsite\Excel\ExcelServiceProvider', ], 'aliases' => [ .... 'Excel' => 'Maatwebsite\Excel\Facades\Excel', ], Done above configuration then run following command : php artisan vendor:publish Step : 3 Create route now we are create following route // Route for view/blade file. Route::get('importExport', 'MaatwebsiteController@importExport'); // Route for export/download tabledata to .csv, .xls or .xlsx Route::get('downloadExcel/{type}', 'MaatwebsiteController@downloadExcel'); // Route for import excel data to database. Route::post('importExcel', 'MaatwebsiteController@importExcel'); Step : 4 Create controller Now create MaatwebsiteController.php file in app/Http/Controllers folders look like. <?php namespace App\Http\Controllers; use App\Http\Requests; use Illuminate\Http\Request; use Input; use App\Post; use DB; use Session; use Excel; class MaatwebsiteController extends Controller { public function importExport() { return view('importExport'); } public function downloadExcel($type) { $data = Post::get()->toArray(); return Excel::create('laravelcode', function($excel) use ($data) { $excel->sheet('mySheet', function($sheet) use ($data) { $sheet->fromArray($data); }); })->download($type); } public function importExcel(Request $request) { if($request->hasFile('import_file')){ Excel::load($request->file('import_file')->getRealPath(), function ($reader) { foreach ($reader->toArray() as $key => $row) { $data['title'] = $row['title']; $data['description'] = $row['description']; if(!empty($data)) { DB::table('post')->insert($data); } } }); } Session::put('success', 'Youe file successfully import in database!!!'); return back(); } } Step : 5 Create view/blade file [ADDCODE] After done create above controlle then after we need to crerate one view file for layout. so create importExport.blade.php file in your laravel application's resources/views folders look like. @extends('layouts.app') @section('content') <div class="container"> @if($message = Session::get('success')) <div class="alert alert-info alert-dismissible fade in" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span> </button> <strong>Success!</strong> {{ $message }} </div> @endif {!! Session::forget('success') !!} <br /> <a href="{{ URL::to('downloadExcel/xls') }}"><button class="btn btn-success">Download Excel xls</button></a> <a href="{{ URL::to('downloadExcel/xlsx') }}"><button class="btn btn-success">Download Excel xlsx</button></a> <a href="{{ URL::to('downloadExcel/csv') }}"><button class="btn btn-success">Download CSV</button></a> <form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;" action="{{ URL::to('importExcel') }}" class="form-horizontal" method="post" enctype="multipart/form-data"> {{ csrf_field() }} <input type="file" name="import_file" /> <button class="btn btn-primary">Import File</button> </form> </div> @endsection Now we are ready to run our example so run bellow command ro quick run: php artisan serve Now you can open bellow URL on your browser: http://localhost:8000/importExport We are hope it can help you...