Friday, 12 August 2022

Laravel get company logo from email

if (($email = filter_var($row->company_email, FILTER_VALIDATE_EMAIL)) !== false) {
$email = $row->company_email;
$domain_name = substr(strrchr($email, "@"), 1);
return '<img style="width:32px;" src=' .
"https://logo.clearbit.com/{$domain_name}?size=64" . ' alt="not available">';
}

Laravel delete datatable record with sweet alert

<a data-id="'.$row->id.'" data-bs-toggle="modal"
data-bs-target="#deleteModal" class="text-info bs-tooltip delete-sales-closure" title="Delete ">
<i class="fas fa-trash"></i></a> &nbsp;

<script>
$("body").on('click','.delete-sales-closure',function(){
let sales_closure_id = $(this).attr('data-id');
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
type:'post',
url:"{{ route('salesClosure.delete') }}",
data:{_token:"{{csrf_token()}}",sales_closure_id:sales_closure_id},
success:function(response){
$('#sales_closure').DataTable().ajax.reload();
Swal.fire(
'Deleted!',
'Record has been deleted.',
'success'
)
}
});
}
})
});
</script>

Laravel update and delete image

if ($request->hasfile('project_nda')) {
if ($SalesClosure->project_nda !="" && File::exists(public_path($SalesClosure->project_nda))) {
unlink(public_path($SalesClosure->project_nda));
}
$file = $request->file('project_nda');
$project_nda = "/uploads/sales_closure/{$file->hashName()}";
$file->move(public_path('/uploads/sales_closure'), $project_nda);
$SalesClosure->project_nda = $project_nda;
}

Laravel generate Sequence number

public function generateCode(Request $request)
{
$year = date('y');
$SalesClosure = SalesClosure::orderBy('created_at', 'DESC')->withTrashed()->first();
if ($SalesClosure) {
$lastNumber = substr($SalesClosure->project_code, 6, 12);

$code = "GRG{$year}-" . str_pad($lastNumber + 1, 6, "0", STR_PAD_LEFT);
$isExist = SalesClosure::where('project_code', $code)->withTrashed()->first();
if ($isExist > 0) {
$lastNumber = substr($code, 6, 12);
$code = "GRG{$year}-" . str_pad($lastNumber + 1, 6, "0", STR_PAD_LEFT);
}
} else {
$code = "GRG{$year}-" . str_pad('100001', 6, "0", STR_PAD_LEFT);
}
return $code;
}

Tuesday, 9 August 2022

Laravel get data by dependant columns ( multiple where conditions orwhere conditions)

My task is to create a form with access;
while creating form i added a radio buttons that is 
1-all team
2-individual dept
3-all dept
4-selected user
if admin selects 1 then the table data will show to all team with admin
if admin selects 2 then another dropdown popup from there we need select one dept
-then the table data will show to only selected dept users including admin


$data= CustomFormTable::query()
->where(function($query){
$query->where('access_to','=',1);
// ->Where('access_to_dept','=',$userDeptId);
})
->orWhere(function($query) use ($userDeptId){
$query->where('access_to','=',2)
->Where('access_to_dept','=',$userDeptId);
})
->orWhere(function($query) use ($userDeptId){
$query->where('access_to','=',3);
// ->Where('access_to_dept','=',$userDeptId);
})
->orWhere(function($query) use ($authUserId){
$query->where('access_to','=',4)
->Where('access_to_user','=',$authUserId);
})
->latest()->with('owner')->get();

Laravel CSS and js not working on live server

 1) Create a new file index.php in root  directory of project, and copy paste below code

<?php

require __DIR__.'/public/index.php';

?>

2) Create a new file  .htaccess in root  directory of project, and copy paste below code

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>

RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Monday, 8 August 2022

Php Laravel add google v2 recaptcha

<div class="form-group{{ $errors->has('g-recaptcha-response') ? ' has-error' : '' }} row">
<div class="col-md-6 offset-md-4">
<div class="g-recaptcha" data-sitekey="6LedFpggAAAAADmTwu5aKnN5vqrOKiZrWtx9RSlV"></div>
@if ($errors->has('g-recaptcha-response'))
{{-- <span class="help-block"> --}}
<span class="text-danger">{{ $errors->first('g-recaptcha-response') }}</span>
{{-- </span> --}}
@endif
</div>
</div>


<script src="https://www.google.com/recaptcha/api.js" async defer></script>

Sunday, 7 August 2022

generate csv for unknown database table columns in laravel

 /*-----------export DATA to csv ----------------- */

public function exportDataCsv(Request $request,$id)
{
$fileName = 'data.csv';
$data=DB::table($id)->get();
$headers = array(
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=$fileName",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
);
$columns= DB::getSchemaBuilder()->getColumnListing($id);
$callback = function() use($data, $columns) {
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
if(count($data)>0 )
{
foreach($data as $key=> $ob1) {
for($i=0;$i<count($columns);$i++)
{
$col=$columns[$i];
$row[$key][] = $ob1->$col;
}
fputcsv($file,$row[$key]);
}
}else{

fputcsv($file,array('No data'));
}
};
return response()->stream($callback, 200, $headers);
}

Laravel generate csv for database tables column names only

/*-----------Get Sample CSV ----------------- */
public function getSampleCsv(Request $request,$id)
{
$columns= DB::getSchemaBuilder()->getColumnListing($id);
if (($key = array_search('created_at', $columns)) != false) {
unset($columns[$key]);
}
if (($key = array_search('updated_at', $columns)) != false) {
unset($columns[$key]);
}
$fileName = 'sample.csv';
$headers = array(
"Content-type" => "text/csv",
"Content-Disposition" => "attachment; filename=$fileName",
"Pragma" => "no-cache",
"Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
"Expires" => "0"
);
$callback = function() use($columns) {
$file = fopen('php://output', 'w');
fputcsv($file, $columns);
};

return response()->stream($callback, 200, $headers);
}

Laravel Export data to csv

 use Illuminate\Http\Response; // Define a function to export data to CSV function exportToCSV($exportData, $columns) {     $filename = ...