<?php

namespace App\Models;

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

class CarExchangeProduct extends Model{
    use HasFactory;
    protected $table = 'car_exchange_product';
    
    protected $fillable = [ 
        'name',
        'category_id',
        'status',
        'created_by',
        'created_at',
        'updated_by',
        'updated_at'
    ];

    public function export($slug){
        $collection = DB::table('car_exchange_product')
                        ->select('car_exchange_product.name', 'car_exchange_category.name as car_exchange_category_name', 'car_exchange_category.status')
                        ->leftjoin('car_exchange_category' ,'car_exchange_category.id' ,'car_exchange_product.category_id');

        if($slug != 'all')
            $collection->where(['car_exchange_product.status' => $slug]);
        
        $data = $collection->get();
        
        if($data->isNotEmpty()){
            return $data;
        }else{
            return null;
        }
    }
}
