duandao8607 2015-09-14 07:59
浏览 43
已采纳

在视图laravel 5中包含控制器

I'm new to laravel and I got stuck.

My problem is I want 2 sections (navigation, content) that has dynamic data

Here's some code Main Blade

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Portfolio</title>
</head>
    <body>
        <div class="navigation">
            @yield('menu')
        </div>
        <div class="content">
            @yield('content')   
        </div>
    </body>
</html>

portfolio blade

    @extends('main')

@section('content')
    @foreach($data as $portfolio)
        <a href="portfolio/{!!$portfolio->portfolio_url!!}"><img src='{{ URL::asset("images/$portfolio->picture.jpg") }}'/></a>
    @endforeach
@stop

and my navigation blade

@extends('main')
@section('menu')
    @foreach($menuknoppen as $menuknop)
            <a href='{{ URL::to("$menuknop->menu_url") }}'>{{$menuknop->menutitle}}</a>
    @endforeach
@stop

the portfolio blade has a controller, but also the menu blade has a controller

Edit1:

the problem is the navigation isn't showing even if I add static text

Edit2:

My controllers my portfolio controller

    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function index(){
        //here comes a whole list with what i've done
        $results = DB::table('projects')->get();
        //return $results;
        $data = array();
        foreach ($results as $key => $result) {
            $data[] = $result;
        }
        return view('portfolio.portfolio')->with('data', $data);
    }
    public function getProject($portfolio_url){
        //this gets the project thats clicked
        $results = DB::select('select * from projects where portfolio_url = ?', array($portfolio_url));
        return view('portfolio.single')->with('data', $results['0']);
    }

}

my navigation controller

class menuController extends Controller {

    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    // public function __construct($table){
    //  $results = DB::table($table)->get();

    //     return view('menu')->with('menuknoppen', $results);
    // }
    public function index(){
        $results = DB::table('navigation')->get();

        return view('menu')->with('menuknoppen', $results);
    }

}
  • 写回答

2条回答 默认 最新

  • douyun3799 2015-09-14 08:39
    关注

    Your main blade should be:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
     <meta charset="UTF-8">
     <title>Portfolio</title>
    </head>
    
    <body>
        <div class="navigation">
            @include('menu');
        </div>
        <div class="content">
            @yield('content')   
        </div>
    </body>
    </html>
    

    Your portfolio should be:

    @extends('main')
    
    @section('content')
      @foreach($data as $portfolio)
       <a href="portfolio/{!!$portfolio->portfolio_url!!}"><img src='{{ URL::asset("images/$portfolio->picture.jpg") }}'/></a>
      @endforeach
    
    @stop
    

    Navigation field should be:

    //Don't use extends here
     @foreach($menuknoppen as $menuknop)
            <a href='{{ URL::to("$menuknop->menu_url") }}'>{{$menuknop->menutitle}}</a>
    @endforeach
    

    Pass multiple data

    public function index()
    {
      $data = //data code;
      $results = // results code
      return view(portfolio.portfolio, compact('data', 'results'));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题
  • ¥20 materialstudio计算氢键脚本问题
  • ¥15 有没有代做有偿主要做数据可视化部分即可(2023全国高考更省一本线理科类)