In this artical, I will show you how to check current date between two dates in php. current date between two date check in using php.
You can check date between two dates using php .You can get current date then start date and end date between date then return true otherwise false return.
Today,I will learn you how to use dropzone file upload in php.we will help you to give example of dropzone add download link example. We will use php dropzone add download button. We will use add download button in dropzone js php. Follow bellow tutorial step of dropzone download file on click.
If you need to add download uploaded file in dropzone then We will show you how to add download button on uploaded file with ajax request. we will add download link and it will download file from server in dropzone.js.dropzone provide success function and we will append a tag with uploaded file path. here we will use php with dropzone to download file from server.
Here, I will give you full example for simply dropzone add download button using php as bellow.
Step 1: Create index.php file
In this step,we have to create index.php file in our root folder and copy bellow code and put on that file. In this file i use cdn for bootstrap, jquery, dropzone css and js.
I will write click event for button and when you click on that button then and then images will upload to server.
Here in this step , I have to just create "upload" folder for store images. You can also give different name from uploads, But make sure also change on upload.php file.
Now I am ready to run this example, so just run bellow command on root folder for run your project.
Hi Guys
In this tutorial, I will learn how to generate dummy records in your database table using tinker factory of Laravel 8. As I know testing is very important part of any web development project. Sometime we may require to add hundreds records in your users table, OR maybe thousands of records. Also think about if you require to check pagination. then you have to add some records for testing. So what you will do it at that that moment, You will add manually thousands of records ? What you do ?. If you add manually thousands of records then it can be take more time.
Here,Laravel 8 have composer package "laravel/tinker" that we will provide you to generate dummy records by using their command. So Laravel 8 by default take "laravel/tinker" package for project. Also they provide by default one factory for user table. You can check path here :
database/factories/. In this folder you can add your different factory for different model.
Generate Dummy Products:
Here, I will give you full example for simply create dummy data using tinker factory using laravel 8 as bellow.
Step 1 : Install Laravel 8 Application
we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Model and Migration
Here this step, we will create one model and migration name Product. Use the below following command and create it
php artisan make:model Product -m
next,open products migration file and put the below code.
Path: /database/migrations/2020_02_03_095534_create_products_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->text('detail');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
Next, go to app/Product.php and open Product model file and put the below code.
here following path of model fille
Path:/app/Models/Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'detail'
];
}
Step 3: Create Factory
In this setp,I will create ProductFactory in following command As you see above command for user, But you can not do same for other model. If you want to do this then you have to add factory for that model. So i am going to give you full example of factory from scratch.
I have Product model with products table. Now we want to add dummy records for Product model. So we have to add factory like as bellow :
database/factories/ProductFactory.php
<?php
namespace Database\Factories;
use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ProductFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Product::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => $this->faker->name,
'detail' => $this->faker->text,
];
}
}
Today,I will learn you how to create custom command in laravel .It's better if we make our own artisan command for like project setup, create new admin Student etc in our laravel application. If we create custom command then we can make project setup like create one admin Student, run migration, run seeder and etc. In this post i give you example, how to create console commands in laravel project.
In this example I will create custom command for create new admin Student. This custom command will ask your name, email and password.
Custom command
php artisan students:create
This command through we will create new admins. So first fire bellow command and create console class file.
Next this command you can find one file StudentCommand class in console directory. so one that file and put bellow code.
app/Console/Commands/StudentCommand.php
<php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\Student;
use Hash;
class StudentCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'students:create';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$input['name'] = $this->ask('Your name?');
$input['email'] = $this->ask('Your email?');
$input['password'] = $this->secret('What is the password?');
$input['password'] = Hash::make($input['password']);
Student::create($input);
$this->info('Student Create Successfully.');
}
}
After, Now I need to register this command class in Kernel.php file, so open file and add class this way:
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\StudentCommand::class,
];
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
}
}
Now we are ready to use or custom command, you fire bellow command and check you can find command in the list this way "students:create".
Hi Guys,
Today,I will learn you how to create Image Upload in laravel 8. We will show example of image upload in laravel 8. you can easyliy create
Image Upload in laravel 8 I am going to show you about image upload in laravel 8. this example will help you laravel 8 upload image to database. This article goes in detailed on how to upload and display image in laravel 8. Here, Creating a basic example of laravel 8 image upload with preview.
I created simple form with file input. So you have to simple select image and then it will upload in "images" directory of public folder. So you have to simple follow bellow step and get image upload in laravel 8 application.
Here, I will give you full example for simply Image Upload using Laravel 8 as bellow.
Step 1 : Install Laravel 8 Application
we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:
composer create-project --prefer-dist laravel/laravel blogImageUpload
cd blogImageUpload
Step 2: Create Routes
In next step, we will add new two routes in web.php file. One route for generate form and another for post method So let's simply create both route as bellow listed:
Step 3: Create Controller
here this step now we should create new controller as ImageUploadController,So run bellow command for generate new controller
php artisan make:controller ImageUploadController
At last step we need to create imageUpload.blade.php file and in this file we will create form with file input button. So copy bellow and put on that file.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ImageUploadController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
return view('imageUpload');
}
/**
* Write code on Method
*
* @return response()
*/
public function store(Request $request)
{
$request->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:1024',
]);
$imageName = time().'.'.$request->image->extension();
$request->image->move(public_path('images'), $imageName);
/ store image in database from here /
return redirect()->back()->with('success','Image uploaded successfully.')->with('image',$imageName);
}
}
Step 4: Create Blade File
At last step we need to create imageUpload.blade.php file and in this file we will create form with file input button. So copy bellow and put on that file.
resources/views/imageUpload.blade.php
Today,I will describe you how to you can execute artisan commands from controller in laravel. we will explain laravel run artisan command from controller.we are call artisan command from controller in laravel. it will run artisan command from controller laravel.
You can easy execute artisan command from controller in laravel. we can do it using Artisan facade. Laravel Artisan facade that way we can easily run all artisan command with argument.we can do it using Artisan facade. Laravel Artisan facade that way we can easily run all artisan command with argument. you can also use this example in laravel.
Artisan facade have two method call() and queue(), queue() through we can simply make process in queue like email sending etc. So let's see simple example with route. In this example we run "php artisan migrate" command using artisan facade. Here i bellow example you can learn how you can run artisan commands from route.
Exmaple:1
Here In this example, Here bellow example you can call artisan migrate command from controller
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
/* php artisan migrate */
\Artisan::call('migrate');
dd('all migration run successfully');
}
Exmaple:2
In this example, you can call artisan cache clear command from controller
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
/* php artisan cache:clear */
\Artisan::call('cache:clear');
dd('cache clear successfully');
}
Exmaple:3
bellow example, You can call artisan run seeder command from controller
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
/* php artisan migrate */
\Artisan::call('db:seed', array('--class' => "AdminSeeder"));
dd('Seeder run successfully');
}
Exmaple:4
In this example, you can call artisan run configration clear command from controller
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
/* php artisan config:clear */
\Artisan::call(config:clear);
dd('Configuration cache cleared!');
}
Today, I will learn you how to use validation custom error messages in laravel. we will show explain of laravel validation custom error messages. I will explain step by step laravel validation custom error messages.we will implement a laravel custom validation message in controller. This article goes in detailed on laravel form validation custom error messages.
I will show you three way to set custom error messages with laravel validation. we will add custom error messages in laravel app. sometime I need to change default laravel validation error message to his own.
Example 1: Directly in Controller Code
In this example, we can directly change from controller method, i think if you want to make it quick then this option will be perfect.
StudentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Student;
class StudentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$students = Student::all();
return view('students.index', compact('students'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:students,email',
'password' => 'required|same:confirm_password'
],
[ 'name.required' => 'The :attribute field can not be blank value.']);
$input = $request->all();
$input['password'] = bcrypt($input['password']);
Student::create($input);
return redirect(route('students.index'));
}
}
Example 2: Using Custom Request
In this example,we have to create custom request and then you can use it in your controller, this method is better way of laravel, so can run following command to create student form request.
php artisan make:request StudentFormRequest
app/Http/Requests/StudentFormRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StudentFormRequest extends FormRequest
{
/**
* Determine if the student is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return false;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required',
'email' => 'required|email|unique:students,email',
'password' => 'required|same:confirm_password'
];
}
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'name.required' => 'The :attribute field can not be blank value',
];
}
}
In this example, we will set custom messages by directly on laravel set default files. but it will change in your whole project. So here bellow i added controller validation code for my student module like as bellow.
StudentController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Student;
class StudentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$students = Student::all();
return view('students.index', compact('students'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required|email|unique:students,email',
'password' => 'required|same:confirm_password'
]);
$input = $request->all();
$input['password'] = bcrypt($input['password']);
Student::create($input);
return redirect(route('students.index'));
}
}
Now i want to change my validation error message for "name" field, so open "validation.php" file and change like as bellow:
resources/lang/en/validation.php
....
'custom' => [
'name' => [
'required' => 'The :attribute field can not be blank value.',
],
],
....