Model Product
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function category()
{
return $this->belongsTo(ProductCategory::class, 'category_id', 'id');
}
public function specification()
{
return $this->belongsTo(ProductSpecification::class, 'id', 'product_id');
}
} |
|
Migration Product
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| <?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('category_id', false);
$table->string('title');
$table->integer('sort_order')->unsigned();
$table->text('image')->nullable();
$table->boolean('status');
$table->timestamps();
$table->foreign('category_id')->references('id')->on('product_categories')->onDelete('cascade');
$table->index('status');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('products');
}
} |
|
Model Product Category
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| <?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class ProductCategory extends Model
{
public function products()
{
return $this->hasMany(Product::class, 'category_id', 'id');
}
} |
|
Migration ProductCategories
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
| <?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('product_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('sort_order')->unsigned();
$table->text('image')->nullable();
$table->boolean('status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('product_categories');
}
} |
|
Controller
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
| public function products(ProductRepository $productRepository)
{
$data['products'] = $productRepository->getFromFront();
//dd($data['products']->first()->category); //вот так вот через репозитороий не возвращает
dd(Product::findOrFaIL(1)->category); //А на прямую с модели возвращяет
return $this->index('front.products', $data);
} |
|
ProductRepository
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| <?php
namespace App\Repositories\Product;
use App\Repositories\CoreRepository;
use App\Models\Product as Model;
/**
* Class WorkCaseRepository.
*/
class ProductRepository extends CoreRepository
{
/**
* @return string
* Return the model
*/
public function getModelClass()
{
return Model::class;
}
public function getFromFront()
{
$columns = array('id', 'title', 'image');
return $this->startConditions()
->where(array('status' => true))
->with('category:id')
->orderBy('sort_order', 'asc')
->paginate(3, $columns);
} |
|
CoreRepository
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| <?php
namespace App\Repositories;
use Illuminate\Database\Eloquent\Model;
/**
* Class CoreRepository
*
* @package App\Repositories
* Репозиторий работы с сущностью.
* Может выдавать наборы данных.
* Не может создавать/изменять сущности.
*/
abstract class CoreRepository
{
/**
* @var Model
*/
protected $model;
/**
* CoreRepository constructor.
*/
public function __construct()
{
$this->model = app($this->getModelClass());
}
/**
* @return mixed
*/
abstract protected function getModelClass();
/**
* @return \Illuminate\Contracts\Foundation\Application|Model|mixed
*/
protected function startConditions(){
return clone $this->model;
}
} |
|
Не возвращяет категорию продукта
Хотя при вызове на прямую через модель выводит
Добавлено через 1 минуту
Возвращает null вместо категории