Skip to content

Instantly share code, notes, and snippets.

@ahmadwaliesipick
Created September 22, 2019 16:06
Show Gist options
  • Save ahmadwaliesipick/7521c2e57d38d73e801c5f4372358375 to your computer and use it in GitHub Desktop.
Save ahmadwaliesipick/7521c2e57d38d73e801c5f4372358375 to your computer and use it in GitHub Desktop.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;
class Product extends Model
{
use LogsActivity, SoftDeletes;
protected $fillable = ['title','user_id','price','vendor_id','category_id','slug','description','company_id'];
protected static $logAttributes = ['title','user_id','price','vendor_id','category_id','slug','description','company_id'];
protected $dates = ['deleted_at'];
/**
* Get the index name for the model.
*
* @return string
*/
public function searchableAs()
{
return 'products_index';
}
//each category might have multiple children
public function inventory() {
return $this->hasMany(Inventory::class, 'product_id')->orderBy('updated_at','desc');
}
public function inventory_up() {
return $this->hasMany(Inventory::class, 'product_id')->where('action', 'up');
}
public function inventory_down() {
return $this->hasMany(Inventory::class, 'product_id')->where('action', 'down');
}
//each category might have multiple children
public function category() {
return $this->belongsTo(Category::class, 'category_id');
}
public function company() {
return $this->belongsTo(Company::class,'company_id');
}
public function delete()
{
$this->inventory()->delete();
return parent::delete();
}
public function vendor() {
return $this->belongsTo(Vendor::class, 'vendor_id');
}
public function inventories() {
return $this->hasMany(Inventory::class, 'product_id');
}
public function attachments() {
return $this->hasMany(Attachment::class, 'ref_id')->orderBy('id','desc')->where('modal','product');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment