In this post, we gonna create custom functions and load it in the Laravel project. These functions can be accessible all over the project. Loading custom helper functions in Laravel is pretty straightforward. Let’s follow the below steps, First, create a file called TextHelper.php the filename can be anything, and place your custom functions in… Continue reading Custom Helper functions in Laravel
Category: Laravel
Eloquent – A better way to increment or decrements an int
Laravel has in-built increment() and decrement() functions to increase or decrease a value of the column, by 1 or with the given number. Below are the examples:
1 |
User::find($user_id)->increment('post_count'); |
That’s it – it will actually run update column + 1 under the hood. You can specify a second parameter to this function, the amount of incrementing.… Continue reading Eloquent – A better way to increment or decrements an int
How to Upload, download, remove Files to Amazon S3 Using Laravel
Create a controller called UploadController.php with following code.
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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class UploadController extends Controller { public function index() { $files = Storage::disk('s3')->files('files'); $data = []; foreach($files as $file) { $data[] = [ 'name' => basename($file), 'downloadUrl' => url('/download/'.base64_encode($file)), 'removeUrl' => url('/remove/'.base64_encode($file)), ]; } return view('upload', ['files' => $data]); } public function store(Request $request) { $this->validate($request, [ 'file' => 'required|max:2048' ]); if ($request->hasFile('file')) { $file = $request->file('file'); $name = time() . $file->getClientOriginalName(); $filePath = 'files/' . $name; Storage::disk('s3')->put($filePath, file_get_contents($file)); } return back()->withSuccess('File uploaded successfully'); } public function destroy($file) { $file = base64_decode($file); Storage::disk('s3')->delete($file); return back()->withSuccess('File was deleted successfully'); } public function download($file) { $file = base64_decode($file); $name = basename($file); Storage::disk('s3')->download($file, $name); return back()->withSuccess('File downloaded successfully'); } } |
Create a view file Create a file called upload.blade.php in the resources/views directory and place below inside it.
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 |
<div class="row"> <h3>Upload File</h3> <div class="card col-sm-12"> <form action="{{ url('/store') }}" method="POST" enctype="multipart/form-data" class="form-inline"> {{ csrf_field() }} <div class="form-group"> <label for="file">Select File</label> <input class="form-control-file" type="file" name="file" id="file"> </div> <button type="submit" class="btn btn-primary mb-2">Upload</button> </form> </div> </div> <div class="row"> <h3>Files</h3> <div class="card col-sm-12"> @if (count($files) > 0) @foreach ($files as $file) <a href="{{ url($file['downloadUrl']) }}">{{ $file['name'] }}</a> <form action="{{ url($file['removeUrl']) }}" method="POST"> {{ csrf_field() }} {{ method_field('DELETE') }} <button type="submit" class="btn btn-default">Remove</button> </form> @endforeach @else <p>Nothing found</p> @endif </div> </div> |
Define Routes Open your routes/web.php directory and add following route definitions to it.
1 2 3 4 |
How to use Datatables in Laravel 5.8
In this tutorial, I will show you how to use the Datatables in the Laravel based project withyajra/laravel-datatables-oracle package. For this tutorial, we gonna use Laravel 5.8 version but the Datatable package usage steps will be pretty simpler with other versions of Laravel framework. As a first step lets setup a brand new Laravel project… Continue reading How to use Datatables in Laravel 5.8
How to create RESTful API in Laravel 5.7
In this tutorial, I will show you creating RESTful apis using Laravel 5.7 php framework. We gonna use the Laravel’s API resources to build RESTful APIs. Laravel’s API Resource class is a way to transform data from one format to another. For example, if you have a comment model and you want to manipulate the… Continue reading How to create RESTful API in Laravel 5.7
Laravel Eloquent Increments and Decrements value
In this post, I would like to show you the hidden and useful functions of Laravel’s Eloquent ORM to Increment and Decrement value of the fields in the database column. In my recent project, I used increment() and decrement() methods to increase and decrease the numbers respectively. How to use increment() and decrement()
1 2 3 4 5 |
Question::find($QuestionID)->increment('view_count'); // +1 Question::find($QuestionID)->decrement('view_count'); // -1 Question::find($QuestionID)->increment('view_count', 10); // +10 Question::find($QuestionID)->decrement('view_count',10); // -10 |
Laravel 5.6 Socialite Bitbucket Login
In this tutorial, we’re going to explain how to integrate Bitbucket login in Laravel based application using Socialite. The Socialite is Laravel’s official package, which is making is easy to authenticate with external services with OAuth providers. Create Project Create a brand new Laravel 5.6 project with Composer create-project command:
1 |
$ composer create-project laravel/laravel arjunphp-bitbucket-login 5.6 --prefer-dist |
Database connection settings After… Continue reading Laravel 5.6 Socialite Bitbucket Login
Laravel 5.6 Socialite GitHub Login
In this tutorial, we’re going to explain how to integrate GitHub login in Laravel based application using Socialite. The Socialite is Laravel’s official package, which is making is easy to authenticate with external services with OAuth providers. Create Project Create a brand new Laravel 5.6 project with Composer create-project command:
1 |
$ composer create-project laravel/laravel arjunphp-github-login 5.6 --prefer-dist |
Database connection settings After… Continue reading Laravel 5.6 Socialite GitHub Login
Laravel 5.6 Socialite LinkedIn Login
In this tutorial, we’re going to explain how to integrate LinkedIn login in Laravel based application using Socialite. The Socialite is Laravel’s official package, which is making is easy to authenticate with external services with OAuth providers. Create Project Create a brand new Laravel 5.6 project with Composer create-project command:
1 |
$ composer create-project laravel/laravel arjunphp-linkedIn-login 5.6 --prefer-dist |
Database connection settings After… Continue reading Laravel 5.6 Socialite LinkedIn Login
Laravel 5.6 Socialite Google Login
In this tutorial, we’re going to explain how to integrate Google login in Laravel based application using Socialite. The Socialite is Laravel’s official package, which is making is easy to authenticate with external services with OAuth providers. Create Project Create a brand new Laravel 5.6 project with Composer create-project command:
1 |
$ composer create-project laravel/laravel arjunphp-google-login 5.6 --prefer-dist |
Database connection settings After… Continue reading Laravel 5.6 Socialite Google Login