将相同的数据传递到不同的视图而无需一遍又一遍地初始化

时间:2018-08-13 17:39:33

标签: php controller routing laravel-5.2

这是我的代码,我想每次通过数据时都传递$topLineLinks而不运行查询。 $topLineLink包含在每个页面布局中。这是我的web.php调用的Laravel控制器,该文件是用于定义路由的文件。

class HomeController extends Controller
{    
    public $topLineLinks;

    function __construct(){
        $this->topLineLinks=Link::where('type','social')->get();
    }

    public function index(){             
        $categories =Category::join('links','categories.iconImage','=','links.id')->select('categories.*','links.link')->get();
        $banners= Link::where('type','banner')->get();

        return view('layouts.home',compact('categories','$this->topLineLinks','banners'));
    }//Loading links for home page elements

    public function category($category){
        $title = Category::where('name',$category)->get();                   
        $id;
        foreach($title as $item){
            $id=$item['id'];            
        }                                                
        $banners = CategoryBanners::join('links','category_banners.image_id','links.id')->select('links.*')->where('category_id',$id)->get();
        $categories =  Subcategory::join('links','subcategories.iconImage','=','links.id')->select('subcategories.*','links.link')->where('parent_id',$id)->get();
        return view('layouts.category',compact('category','categories','banners'));
    }//Loading links for category page to show subcategories

    public function subCategory($category,$subCategory){        
        $products = Product::join('subCategories','products.subCategory_id','=','subcategories.id')->join('links','products.image_id','=','links.id')->select('products.*','links.link')->where('subCategories.name',$subCategory)->get();        
        return view('layouts.subCategory',compact('category','subCategory','products'));
    }//Loading page to show the product in each subcategory    
}

0 个答案:

没有答案