3

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);
    }

}
13
  • You want to include the navigation to you portfolio? Commented Sep 14, 2015 at 8:10
  • So what is your actual question? Commented Sep 14, 2015 at 8:15
  • Sorry I didn't ask the problem Updated Commented Sep 14, 2015 at 8:19
  • Where is navigation in your file tree? For example: views/layouts/navigation. Commented Sep 14, 2015 at 8:22
  • Am i right that you want to include the navigation inside your portfolio? Commented Sep 14, 2015 at 8:29

2 Answers 2

3

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'));
}
Sign up to request clarification or add additional context in comments.

12 Comments

Now the question is how do I add the navigation view inside the main blade?
I've just do it. I included it. Just try to change your code.
That true sorry didn't see that! Now i have the same error again Undefined variable: menuknoppen. When you include a view does it execute the controller? because my navigation is comming from a database
Can i see your controllers? And also how you call this $menuknoppen
I added the controllers
|
1

You have mixed the concept of @yield , @include and @extend

@yield provides a place for you to replace, so when you call @extend in other view you can reuse the template in view which you extend and replace the part with @yield

@include means this part of code is always replaced by the view it defined

So when you are designing a webpage, you need to make sure what is "always called" (use @include) and what could be replaced (use @yield)

As an assisting explanation to aldrin27 working code, I hope this make your mind clearer on the blade template, it rocks! :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.