Models Used

These are the entities used in this demo:


<?php namespace Zofe\Rapyd\Demo;

/**
 * Article
 */
class Article extends \Eloquent
{
    protected 
$table 'demo_articles';

    public function 
author()
    {
        return 
$this->belongsTo('Zofe\Rapyd\Demo\Author''author_id');
    }

    public function 
comments()
    {
        return 
$this->hasMany('Zofe\Rapyd\Demo\Comment''article_id');
    }

    public function 
categories()
    {
        return 
$this->belongsToMany('Zofe\Rapyd\Demo\Category''demo_article_category''article_id','category_id');
    }

    public function 
detail()
    {
        return 
$this->hasOne('Zofe\Rapyd\Demo\ArticleDetail''article_id');
    }

    public function 
scopeFreesearch($query$value)
    {
        return 
$query->where('title','like','%'.$value.'%')
            ->
orWhere('body','like','%'.$value.'%')
            ->
orWhereHas('author', function ($q) use ($value) {
                
$q->whereRaw(" CONCAT(firstname, ' ', lastname) like ?", array("%".$value."%"));
            })->
orWhereHas('categories', function ($q) use ($value) {
                
$q->where('name','like','%'.$value.'%');
            });

    }

    protected static function 
boot()
    {
        
parent::boot();

        static::
deleting(function ($article) {
            
$article->detail()->delete();
            if (
$article->photo)  @unlink(public_path().'/uploads/demo/'.$article->photo);
        });
    }
}

<?php namespace Zofe\Rapyd\Demo;

/**
 * ArticleDetail
 */
class ArticleDetail extends \Eloquent
{

    protected 
$table 'demo_article_detail';
    public 
$timestamps false;

    public function 
article()
    {
        return 
$this->belongsTo('Zofe\Rapyd\Models\Article''article_id');
    }

}

<?php namespace Zofe\Rapyd\Demo;

/**
 * Author
 */
class Author extends \Eloquent
{

    protected 
$table 'demo_users';

    protected 
$appends = array('fullname');

    public function 
articles()
    {
        return 
$this->hasMany('Zofe\Rapyd\Models\Article');
    }

    public function 
comments()
    {
        return 
$this->hasMany('Zofe\Rapyd\Models\Comment');
    }

    public function 
getFullnameAttribute($value)
    {
        return 
$this->firstname ." "$this->lastname;
    }

}

<?php namespace Zofe\Rapyd\Demo;

/**
 * Category
 */
class Category extends \Eloquent
{

    protected 
$table 'demo_categories';

    public function 
articles()
    {
        return 
$this->belongsToMany('Zofe\Rapyd\Models\Article''demo_article_category''category_id','article_id');
    }

    public function 
parent()
    {
        return 
$this->belongsTo('Category''parent_id');
    }

    public function 
childrens()
    {
        return 
$this->hasMany('Category''parent_id');
    }
}

<?php namespace Zofe\Rapyd\Demo;

/**
 * Comment
 */
class Comment extends \Eloquent
{

    protected 
$table 'demo_comments';

    public function 
article()
    {
        return 
$this->belongsTo('Zofe\Rapyd\Models\Article''article_id');
    }

    public function 
author()
    {
        return 
$this->belongsTo('Zofe\Rapyd\Models\Author''author_id');
    }
}