v2 first commit
This commit is contained in:
		
							
								
								
									
										50
									
								
								app/Imports/Incomes/Customers.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								app/Imports/Incomes/Customers.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,50 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Imports\Incomes;
 | 
			
		||||
 | 
			
		||||
use App\Models\Common\Contact as Model;
 | 
			
		||||
use App\Http\Requests\Common\Contact as Request;
 | 
			
		||||
use Maatwebsite\Excel\Concerns\ToModel;
 | 
			
		||||
use Maatwebsite\Excel\Concerns\WithHeadingRow;
 | 
			
		||||
use Maatwebsite\Excel\Concerns\WithMapping;
 | 
			
		||||
use Maatwebsite\Excel\Concerns\WithValidation;
 | 
			
		||||
use Maatwebsite\Excel\Validators\Failure;
 | 
			
		||||
 | 
			
		||||
class Customers implements ToModel, WithHeadingRow, WithMapping, WithValidation
 | 
			
		||||
{
 | 
			
		||||
    public function model(array $row)
 | 
			
		||||
    {
 | 
			
		||||
        return new Model($row);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function map($row): array
 | 
			
		||||
    {
 | 
			
		||||
        $row['company_id'] = session('company_id');
 | 
			
		||||
        $row['type'] = 'customer';
 | 
			
		||||
 | 
			
		||||
        // Make enabled field integer
 | 
			
		||||
        if (isset($row['enabled'])) {
 | 
			
		||||
            $row['enabled'] = (int) $row['enabled'];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $row;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function rules(): array
 | 
			
		||||
    {
 | 
			
		||||
        return (new Request())->rules();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function onFailure(Failure ...$failures)
 | 
			
		||||
    {
 | 
			
		||||
        foreach ($failures as $failure) {
 | 
			
		||||
            $message = trans('messages.error.import_column', [
 | 
			
		||||
                'message' => $failure->errors()->first(),
 | 
			
		||||
                'sheet' => 'customers',
 | 
			
		||||
                'line' => $failure->attribute(),
 | 
			
		||||
            ]);
 | 
			
		||||
    
 | 
			
		||||
            flash($message)->error()->important();
 | 
			
		||||
       }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user