我有2個分類和其子條款的回購。我設置接口等等,並一直在通過一些代碼來清理它,並從我的控制器中刪除雄辯,因此也使它們變得很瘦。將存儲庫方法應用於關係模型
我很努力地讓我的頭在如何應用方法的孩子關係沒有得到錯誤。請輕鬆地學習這一點,但感覺即將失去一些顯而易見的東西,並在圈子裏走來走去,一直到現在爲止都失去了一切。
MyController.php
TaxonomyInterface.php
interface TaxonomyInterface{
public function createTermFromSlug($term_name);
}
DBTaxonomyRepository
class DBTaxonomyRepository
extends AbstractDBRepository
implements TaxonomyInterface
{
protected $table = 'taxonomy';
public $timestamps = false;
public function term()
{
return $this->hasMany('App\Repositories\DBTaxonomyTermRepository', 'taxonomy_id', 'id');
}
/**
* Create term based on unique slug.
*
* @param $term_name
* @internal param $name
*/
public function createTermFromSlug($term_name)
{
$term = $this->firstOrCreate(array(
'slug' => str_slug($term_name)
));
$term->name = $term_name;
$term->save();
}
}
我已經嘗試了很多東西,現在我明顯錯過了一些基本的東西,我不能通過試驗和錯誤學習它。這是我離開它的當前狀態。
簡而言之,我想將存儲庫方法應用於其術語實體的分類實體,從而與控制器脫鉤。
我希望在此之後將其抽象出來,以便我可以在處理不同分類法的各種不同控制器中重複使用它。
'AbstractDBRepository'擴展了雄辯嗎? – user3158900
是的。 '使用Illuminate \ Database \ Eloquent \ Model; 抽象類AbstractDBRepository extends Model' – Jimbo