这是我的代码,我想每次通过数据时都传递$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
}