get days between two dates in php

<?php
$datetime1 = date_create($subscription[$index]['end_date']);
$datetime2 = date_create(date('Y-m-d'));
$interval = date_diff($datetime1, $datetime2);
$NoOfDays= $interval->format('%a');
echo $NoOfDays;
?>

get country code from phone number php from string format

<?php
 $user_phone = "+919555699081";
 $len = strlen($user_phone);
 $phone = substr($user_phone, -10);
 $country_code = substr($user_phone, 0,$len -  strlen($phone));
 echo $country_code."-".$phone."-"; echo $user_phone; 
?>

how can i get raw data in laravel api

public function MyFunction(Request $request)
{
  $data = array();
  $data['input'] = $request->all();
$data['raw'] = Request::createFromGlobals()->getContent();
  return $this->sendResponse($data, 'Successfully');   
}

connection string in laravel for eloquent model

    $data = array();
      $lang = $request->lang;
      $id = $request->id;
      $data['success'] = false;
      $user_id = 0;
      if (Auth::check()) {
      $user = Auth::user();
      $user_id = $user->id;
       }
      //$data['chat_delete'] = DB::connection($lang)->table('astro_chat')->where(['id'=> $id])->delete();
      $astroChatModel  = new AstroChat();
      $astroChatModel->setConnection($lang);
      $astroChat = $astroChatModel->find($id);
      if(  $astroChat && $user_id == $astroChat->user_id ){ $data['success'] = $astroChat->delete();}
      return $this->sendResponse($data, 'Successfully'); 

add soft delete in laravel

In the migration file add:

$table->softDeletes();

In the model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;


class CalNotes extends Model
{
    use HasFactory;
    protected $table = 'cal_notes';
    protected $fillable = ['user_id','title','description','price','ip'];
    protected $primaryKey = 'id';
    use SoftDeletes;

}

In the controller:

 $id = $request->id;
 $calNotesModel  = new CalNotes();
 $calNotes = $calNotesModel->find($id);
 $calNotes->delete();

 if you need to restore the deleted user

$calNotes = CalNotes::withTrashed()->find($id);
        $calNotes->restore();

get double value in MySQL with 2 decimal places

 public function getRatings(Request $request)
    {
      $data = array();
      $lang = $request->lang;
      $data['ratings']= DB::connection($lang)->table('review')->select(DB::raw('format(avg(rating),2) as `rate`'),  'review_id')->groupby('review_id')->get(); 
      return $this->sendResponse($data, 'Successfully'); 
    }
format(number, qtyDecimals)
sample: format(1000, 2)
result 1000.00

group by in MySQL example only date from datetime

MySQL Example

SELECT date(created_at),count(*) FROM `users` WHERE 1 group by date(created_at) ORDER BY `date(created_at)` DESC

Laravel Example with groupByRaw

 $data = DB::connection('default')->table('users')->select(DB::raw('DATE(created_at) date'),DB::raw('count(created_at) as page_count'))->groupByRaw('DATE(created_at)')->get();

Laravel Example with groupBy

 $data = DB::connection('default')->table('users')->select(DB::raw('DATE(created_at) created_date'),DB::raw('count(created_at) as page_count'))->groupBy('created_date')->get();

Laravel distinct count group by

    public function showUniqueUsersBhagwatByDate(Request $request)
    {
        \LogActivity::addToLog('admin');
        $data= array();
        $data = DB::table('cal_user_activity')->select(DB::raw('DATE(updated_at) date'),DB::raw('count(DISTINCT  user_id) as page_count'))->where('module','BhagwatAudio')->groupby('date')->get();
        return view('admin/shownewbhagwatgeetalistener',['data'=>$data,'title'=>'Show Uniques Users Listeners','isOthers'=>1]);
    }

join table in laravel eloquent

$articles = DB::table('articles')
            ->select('articles.id as articles_id', ..... )
            ->join('categories', 'articles.categories_id', '=', 'categories.id')
            ->join('users', 'articles.user_id', '=', 'users.id')

            ->get();

trim left characters in php

The ltrim() function removes whitespace or other predefined characters from the left side of a string. Related functions: rtrim() – Removes whitespace or other predefined characters from the right side of a string. trim() – Removes whitespace or other predefined characters from both sides of a string.

Return Value: Returns the modified string

Changelog: The charlist parameter was added …

PHP Version: 4+