-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathContactsImport.php
48 lines (38 loc) · 1.1 KB
/
ContactsImport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
namespace App\Imports;
use App\Models\Contacts;
use App\Models\Tags;
use App\Models\Groups;
use Str;
use Maatwebsite\Excel\Concerns\ToModel;
class ContactsImport implements ToModel
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
$tag_id = Tags::where('name', 'like', '%'.$row[2].'%' )
->where('user_id', '=', auth()->user()->id)
->first();
if( !isset($tag_id->id) ){
$tag_id = Tags::create([
'name' => $row[2],
'description' => $row[2],
'color' => '#ff0000',
'user_id' => auth()->user()->id,
]);
}
if($row[1] != '' and $row[1] != 'phone' and $row[1] != 'name' and $row[0] != ''){
return new Contacts([
"imported" => true,
"name" => $row[0],
"phone" => $row[1],
"tag_id" => isset($tag_id->id) ? $tag_id->id : null,
'user_id' => auth()->user()->id,
]);
}
}
}