<?php

namespace App;
use Session;
use Illuminate\Database\Eloquent\Model;

class Toko extends Model
{
    protected $fillable = ['name'];

    public  function  menus()
    {
      return  $this->hasMany('App\Menu');
    }

    public static function boot()
    {
      parent::boot();
      self::deleting(function($toko){

        if($toko->menus->count()>0){
          //$jlh  = $toko->menus->count('amount');
          $html = 'Toko tidak bisa dihapus karena masih memiliki Makanan :';
          $html .= '<ul>';
          foreach($toko->menus as $menu){
            $html .="<li> $menu->title</li>";
          }
          $html .= '</ul>';

          Session::flash("flash_notification",[
            "level"=>"danger",
            "message"=>$html
          ]);

          return false;
        }
      });
    }
}
