shiva said:
I'm trying create a page that shows up content depending on the menu you select.
So if I'm on the root or home menu then I get the home content and if I go to the about menu the I go to the about page which shows the about content.
At the moment I got this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'content_id' in 'where clause' (SQL: select * from `content` where `content_id` = menu_id)
I do understand the error, but I thought that the line I had would reference the menu function in the content model.
HomeController
public function index() { $menus = Menu::all(); $menus_child = Menu::where('menu_id', 0)->with('menusP')->get(); $content = Content::where('content_id', '=', 'menu_id')->with('menu')->get(); return View::make('index', compact('menus', 'menus_child', 'content')); }
Content model
public function menu(){ return $this->belongsToMany('Menu', 'content_menu', 'content_id', 'menu_id'); }
index.blade.php
@extends('templates::public') @section('content') Hi @foreach($content as $page) {{ $page->title }} @endforeach @stop
Alright, I find your code slightly confusing, but I will present the setup I would use:
First of all, why should we have a separate database table for our menu? It can be useful if you have loads of different pages that you wish to load, but it is rarely that useful and I feel like you are only complicating things. If you want to, you can of course keep that structure and I'll reply with an appropriate answer for that case.
What I would suggest is that you have one database table, called pages
, which will contain, at minimum, the following columns: id [unsigned int, incrementing]
, title [varchar *or* text]
, content [longtext]
, parent [unsigned int]
, is_in_menu [bool]
.
Your title
would then be the title of the page that is used in the menu and at the top of the page. The content
is obviously the content of the page (if it contains some text for example), parent
is an int referencing another page in the same table, by id
. Last but not least is_in_menu
determines whether the page becomes a menu item or not.
Here, things become quite a bit easier than previously... We can structure the menu with submenu items using our parent
column.
public function index()
{
$pages = Page::all()->where('is_in_menu', 1);
$pages_arr = [];
foreach ($pages as $page) {
$page['children'] = Page::all()->where('parent', $page['id']); // We could of course save all direct children
$pages_arr[] = $page; // of a page in a column
}
// Please note that this makes quite a few db requests. I'm guessing MySQL/Eloquent has a much prettier way to
// do this
return view('index', compact('pages_arr'));
}
I'm not gonna swear this code works, but hopefully you get the idea. You should also note two things:
@extends('templates::public')
@section('content')
Hi
@foreach($pages_arr as $page)
{{ $page[title] }}
With children
@foreach($page['children'] as $child)
{{ $child[title] }}
@endforeach
@endforeach
@stop
Again, this is just a rough outline of how I would handle pages and menus, and the way I use foreach loops is absolutely horrid, so this obviously needs improving.
Finally, I just want to point out that in your code you use where('content_id', '=', 'menu_id') which, if I'm not mistaken, should not work, since it is literally saying where the content id equals the string 'menu_id'
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community