A
A
Arman2019-12-17 16:21:09
Laravel
Arman, 2019-12-17 16:21:09

What is the easiest way to make one dependency dependent on another?

Finger example:

public function page(Website $website, Page $page): View
{
    // ...
}

public function category(Website $website, Category $category): View
{
    // ...
}

It turns out that the Website dependency is being introduced (we get it by domain) and it is necessary that the second Page/Category dependency be from Website:
$page = $website->page()->firstOrFail($pageId);
$category = $website->category()->firstOrFail($categoryId);

How can you make it easier?
Thinking aside:
public function page(WebsitePage $websitePage): View
{
    // ...
}

public function category(WebsiteCategory $websiteCategory): View
{
    // ...
}

but something volumes confuse and how to get $website back without a request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
NubasLol, 2019-12-17
@NubasLol

It turns out that the Website dependency is being introduced (we get it by domain) and it is necessary that the second Page/Category dependency be from Website:

What for?
public function page(Website $website): View
{
    $page = $website->page()->firstOrFail($pageId);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question