将id从公共函数传递给其他公共函数

时间:2018-02-07 19:47:15

标签: php laravel eloquent

我有一个公共函数调用标题:

public function header() {
    $link = Link::with('page', 'tag')->orderBy('clicks', 'desc')->first();
    $link_id = $link->id;

    return view('site.templates.linksheader', compact('link'));
}

我得到了一个公共函数调用index2:

public function index2() {

    $links = Link::where('status', '=', 1)
            ->orderBy('clicks', 'desc')
            ->with('page', 'tag')
            //->whereNotIn('id', $id);
            ->take(12)
            ->get();
}

->whereNotIn的{​​{1}}中,我希望获得index2函数$id ...

1 个答案:

答案 0 :(得分:2)

你可以做到

public function header() {
    $link = $this->getFirstLink();

    return view('site.templates.linksheader', compact('link'));
}

public function index2() {

    $links = Link::where('status', '=', 1)
        ->orderBy('clicks', 'desc')
        ->with('page', 'tag')
        ->whereNotIn('id', $this->getFirstLink()->id);
        ->take(12)
        ->get();
}

private function getFirstLink() {
    return Link::with('page', 'tag')->orderBy('clicks', 'desc')->first();
}
相关问题