Current File : //home/tradevaly/www/app/Http/Controllers/User/WatchHistoryController.php
<?php
namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\WatchHistory;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use Brian2694\Toastr\Facades\Toastr;
use DataTables;

class WatchHistoryController extends Controller
{
    public function viewHistory(Request $request){
        $watch_histories= WatchHistory::where('user_id',auth()->user()->id)->get();
        
        if ($request->ajax()) {
            return Datatables::of($watch_histories)
                ->addIndexColumn()
                ->addColumn('image', function($row){
                    $image = '';
                    if(isset($row->product->image))
                    {
                        $image = '<img src="'.asset('storage/images/admin/product/'.$row->product->image).'"  width="138" height="44" class="lazy" style="width:50px; border-radius:10%;">';
                    }
                    return $image;
                })
                ->addColumn('title', function($row){
                    $title = '';
                    if(isset($row->product->title))
                    {
                        $title = $row->product->title;
                    }else{
                        $title = "product Deleted";
                    }
                    return $title;
                })
                ->addColumn('email', function($row){
                    $email = '';
                    if(isset($row->visitor->email))
                    {
                        $email = $row->visitor->email;
                    }
                    return $email;
                })
                ->addColumn('total_view', function($row){
                    $total_view = '';
                    if(isset($row->visitor))
                    {
                        $total_view = visitor_count($row->product_id);
                    }
                    return $total_view;
                })
                ->addColumn('visitor', function($row){
                    $visitor = '';
                    if(isset($row->visitor->name))
                    {
                        $visitor = $row->visitor->name;
                    }
                    return $visitor;
                })
                ->addColumn('time', function($row){
                    $time = '';
                    if(isset($row->visitor))
                    {
                        $time = ($row->created_at)->diffForHumans();
                    }
                    return $time;
                })
                ->addColumn('action', function($row){
                    $action = '';
                    if(isset($row->product->title)){
                    $action = '<a class="btn btn-success" href="'.route('user.product.analytics',$row->product_id).'">Analytics </a>';
                    }else{
                    $action = '<a class="btn btn-danger" href="'.route('user.product.analytics',$row->product_id).'">Analytics </a>';
                    }
                    return $action;
                })
                
                ->rawColumns(['image', 'title','email','total_view','visitor','time','action'])
                ->make(true);
        }   
        return view("user.pages.product.view_history",compact('watch_histories'));
    }
    public function analytics(Request $request,$id){
        $product= Product::where('id',$id)->first();
        if(isset($product))
        {
        $watch_histories= WatchHistory::where('product_id',$id)->where('user_id',auth()->user()->id)->latest()->paginate(20);
        
        if ($request->ajax()) {
            return Datatables::of($watch_histories)
                ->addIndexColumn()
                
                ->addColumn('visitor', function($row){
                    $visitor = '';
                    if(isset($row->visitor->name))
                    {
                        $visitor = $row->visitor->name;
                    }else{
                        $visitor = "visitor deleted";
                    }
                    return $visitor;
                })
                ->addColumn('email', function($row){
                    $email = '';
                    if(isset($row->visitor->email))
                    {
                        $email = $row->visitor->email;
                    }else{
                        $email = "visitor deleted";
                    }
                    return $email;
                })
                ->addColumn('phone', function($row){
                    $phone = '';
                    if(isset($row->visitor->phone))
                    {
                        $phone = $row->visitor->phone;
                    }else{
                        $phone = "visitor deleted";
                    }
                    return $phone;
                })
                ->addColumn('company', function($row){
                    $company = '';
                    if(isset($row->visitor->company->name))
                    {
                        $company = $row->visitor->company->name;
                    }else{
                        $company = "visitor deleted";
                    }
                    return $company;
                })
                ->addColumn('time', function($row){
                    $time = '';
                    if(isset($row->created_at))
                    {
                        $time = $row->created_at->diffForHumans();
                    }else{
                        $time = "visitor deleted";
                    }
                    return $time;
                })
                ->addColumn('action', function($row){
                    $action = '';
                    if(isset($row->product->slug) && isset($row->visitor->id))
                    {
                        $action = '<a class="btn btn-success" href="'.route("admin.product.singleInquery",['id'=>$row->visitor->id,'product'=>$row->product->id]).'">Inquery</a>';
                    }else{
                        $action = "visitor deleted";
                    }
                    return $action;
                })
                
                ->rawColumns(['visitor','email','phone','company','time','action'])
                ->make(true);
        }
        return view("user.pages.product.analytics",compact('product','watch_histories'));
        }else{
            Toastr::error('Danger!', 'Product already Deleted!');
            return back();
        }
    }
}