1
|
|
|
<?php |
2
|
|
|
namespace Tacone\Bees\Demo\Models; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Article |
6
|
|
|
*/ |
7
|
|
|
class Article extends \Eloquent |
8
|
|
|
{ |
9
|
|
|
protected $table = 'demo_articles'; |
10
|
|
|
|
11
|
|
|
public function author() |
12
|
|
|
{ |
13
|
|
|
return $this->belongsTo('\Tacone\Bees\Demo\Models\Author', 'author_id'); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
public function comments() |
17
|
|
|
{ |
18
|
|
|
return $this->hasMany('\Tacone\Bees\Demo\Models\Comment', 'article_id'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function categories() |
22
|
|
|
{ |
23
|
|
|
return $this->belongsToMany('\Tacone\Bees\Demo\Models\Category', 'demo_article_category', 'article_id', 'category_id'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function detail() |
27
|
|
|
{ |
28
|
|
|
return $this->hasOne('\Tacone\Bees\Demo\Models\ArticleDetail', 'article_id'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function scopeFreesearch($query, $value) |
32
|
|
|
{ |
33
|
|
|
return $query->where('title', 'like', '%'.$value.'%') |
34
|
|
|
->orWhere('body', 'like', '%'.$value.'%') |
35
|
|
|
->orWhereHas('author', function ($q) use ($value) { |
36
|
|
|
$q->whereRaw(" CONCAT(firstname, ' ', lastname) like ?", array("%".$value."%")); |
37
|
|
|
})->orWhereHas('categories', function ($q) use ($value) { |
38
|
|
|
$q->where('name', 'like', '%'.$value.'%'); |
39
|
|
|
}); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected static function boot() |
43
|
|
|
{ |
44
|
|
|
parent::boot(); |
45
|
|
|
|
46
|
|
|
static::deleting(function ($article) { |
47
|
|
|
$article->detail()->delete(); |
48
|
|
|
if ($article->photo) { |
49
|
|
|
@unlink(public_path().'/uploads/demo/'.$article->photo); |
50
|
|
|
} |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|