<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DB;
class CarExchange extends Model{
    use HasFactory;
    protected $table = 'car_exchange';
    
    protected $fillable = [
        'category_id',
        'product_id',
        'engine_number',
        'chassis_number',
        'price',
        'status',
        'created_by',
        'created_at',
        'updated_by',
        'updated_at'
    ];

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

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