<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Category extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $guarded = [];
protected $fillable = ['name'];
public function subcategories()
{
return $this->hasMany(Subcategory::class);
}
public function products()
{
return $this->hasManyThrough(Product::class, Subcategory::class);
}
public function addSubcategory($name)
{
$this->subcategories()->create([
'name' => $name,
'category_id' => $this->id,
]);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Category extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
protected $guarded = [];
protected $fillable = ['name'];
public function subcategories()
{
return $this->hasMany(Subcategory::class);
}
public function products()
{
return $this->hasManyThrough(Product::class, Subcategory::class);
}
public function addSubcategory($name)
{
$this->subcategories()->create([
'name' => $name,
'category_id' => $this->id,
]);
}
public static function boot() {
static::deleting(function(Category $category) {
//$subcategories = $category->subcategories;
//foreach ($subcategories as $subcategory) $subcategory->delete();
});
}
}
public function destroy(Category $category)
{
$category->delete();
return back();
}
public function index()
{
return Category::all();
}
[{"id":6,"name":"asd","created_at":"2018-05-01 11:27:38","updated_at":"2018-05-01 11:27:38","deleted_at":null},{"id":7,"name":"qwe","created_at":"2018-05-01 11:27:41","updated_at":"2018-05-01 11:27:51","deleted_at":"2018-05-01 11:27:51"}]
if(($category_id != -1) && ($subcategory_id == -1))
{
$products = Product::whereHas('subcategory', function ($query) {
$query->where('category_id', '=', request()->category_id);
})->get();
}
if(($category_id != -1) && ($subcategory_id != -1))
{
$products = Product::where('subcategory_id','=', $subcategory_id)->whereHas('subcategory', function ($query) {
$query->where('category_id', '=', request()->category_id);
})->get();
}
"Согласно спецификации CSS ширина блока складывается из ширины контента (width), значений отступов (margin), полей (padding) и границ (border). Аналогично обстоит и с высотой блока. Свойство box-sizing позволяет изменить этот алгоритм, чтобы свойства width и height задавали размеры не контента, а размеры блока."(htmlbook.ru/css/box-sizing)