Yêu cầu thg 11 13, 2018 7:37 CH 1269 0 1
  • 1269 0 1
0

Unresolvable dependency resolving [Parameter #0 [ <required> $model ]] in class App\Repositories\Eloquents\DbBaseRepository

Chia sẻ
  • 1269 0 1

Mình tìm hiểu về Repository, gặp ngay em bug này. Vẫn chưa tìm được câu trả lời. Mong mọi người giúp đỡ.

Sau đây là một số đoạn code của mình.

  1. App\Http\Controllers\HomeController.php:

  2. App\Providers\AppServiceProvider.php:

  3. App\Repositories\Eloquents\DbBaseRepository.php:

<?php
namespace App\Repositories\Eloquents;

class DbBaseRepository
{
    /**
     * Eloquent model
     */
    protected $model;

    /**
     * @param $model
     */
    function __construct($model)
    {
        $this->model = $model;
    }

    public function basePaginateList($per)
    {
        return $this->model->orderBy('id', 'desc')->paginate($per);
    }

    public function baseCreate($data)
    {
        return $this->model->create($data);
    }

    public function baseUpdate($data, $key, $value)
    {
        $obj = $this->model->where($key, $value)->first();

        return $obj->update($data);
    }

    public function baseDestroy($key, $value)
    {
        return $this->model->where($key, $value)->delete();
    }

    public function baseFindBy($key, $value)
    {
        return $this->model->where($key, $value)->first();
    }

    public function baseFindAllBy($key, $value)
    {
        return $this->model->where($key, $value)->orderBy('id', 'desc')->get();
    }

    public function getSpecifiedColumn($key, $value, $array)
    {
        return $this->model::where($key, $value)->first($array);
    }
}

  1. App\Repositories\Eloquents\DbDishRepository.php:
<?php

namespace App\Repositories\Eloquents;

use App\Repositories\Interfaces\DishRepository;

class DbDishRepository extends DbBaseRepository implements DishRepository
{
    public function getModel()
    {
        return App\Dish::class;
    }
    public function topLists()
    {
        $result = $this->_model::whereNotNull('id')
            ->orderBy('like_number', 'desc')
            ->take(10)
            ->get();
    }

    public function newLists()
    {
        $result = $this->_model::whereNotNull('id')
            ->orderBy('created_at', 'desc')
            ->take(10)
            ->get();
    }
}
  1. App\Repositories\Interfaces\BaseRepository.php:
<?php

namespace App\Repositories\Interfaces;

interface BaseRepository
{
    public function getAll($per);

    public function create($param);

    public function get($key, $value);

    public function update($data, $key, $value);

    public function delete($key, $value);
}
  1. App\Repositories\Interfaces\DishRepository.php:
<?php

namespace App\Repositories\Interfaces;

interface DishRepository
{
    public function topLists();

    public function newLists();
}

1 CÂU TRẢ LỜI


Đã trả lời thg 11 14, 2018 12:29 SA
Đã được chấp nhận
+6

Bạn đang gặp phải lỗi đó là do Laravel không thể resolve ra một instance mà class DbBaseRepository yêu cầu.

Cụ thể là ở đoạn code Constructor của class DbBaseRepository

/**
     * @param $model
     */
    function __construct($model)
    {
        $this->model = $model;
    }

Đoạn này để tạo ra một instance của class DbBaseRepository thì ta cần một instance $model nữa, tuy nhiên framework lại không hề biết làm thế nào để tạo ra cái $model đó.

Bởi thế, trước tiên, bạn cần phải chỉ rõ $model đó đến từ đâu, là instance của class nào, làm thế nào để có thể resolve được ra nó trước đã 🤔

Chẳng hạn như là function __construct(DbBaseModel $model), hoặc là bạn có thể bỏ hẳn nó ở phần parameters trong constructor đi, và khởi tạo nó ở bên trong ý 😄

function __construct()
{
    $this->model = new $this->getModel();
}
Chia sẻ
Avatar Quiet @simple1805
thg 11 14, 2018 2:31 SA
Avatar Minh Phuong @phuongyeol
thg 11 14, 2018 4:07 SA

Cảm ơn anh. Câu trả lời rất hữu ích!!

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí