It might be this line, I guess it should be 0, since you where using that in your first where clause.
$content = Content::where('content_id', '=', 0)->with('menu')->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
It might be this line, I guess it should be 0, since you where using that in your first where clause.
$content = Content::where('content_id', '=', 0)->with('menu')->get();
If I change my
$content = Content::where('content_id', '=', 'menu_id')->with('menu')->get();
to
$content = Content::where('content_id', '=', 0)->with('menu')->get();
I still have the same error. Which means that laravel is looking for 'content_id' in the content table. If I have to change 'content_id' to 'id' then I don't get errors but the content that I added for that menu item doesn't show up.
@Shiva hard to understand from your post, but I assume based on your relationship you referencing a pivot table called content_menu i.e. you have menu, content and pivot table called content_menu?
@nolros - yep I have a pivot table called content_menu.
any ideas
Any Ideas?
Obviously this is wrong
// Note the 'menu_id' should be the id of the menu
$content = Content::where('content_id', '=', 'menu_id')->with('menu')->get();
// Maybe something like this, note the php variable there
$content = Content::where('content_id', '=', $menu->id)->with('menu')->get();
If I change my
$content = Content::where('content_id', '=', 'menu_id')->with('menu')->get();
to
$content = Content::where('content_id', '=', $menu->id)->with('menu')->get();
I get this error
ErrorException (E_UNKNOWN)
Undefined variable: menu
so then I changed
$content = Content::where('content_id', '=', $menu->id)->with('menu')->get();
to
$content = Content::where('content_id', '=', $menus->id)->with('menu')->get();
and I got this error
ErrorException (E_UNKNOWN)
Undefined property: Illuminate\Database\Eloquent\Collection::$id
This should help you.
$content = Content::->with(['menu' => function($query){
$query->where('content_id', '=', 'menu_id')
}])->get();
I'm getting this error
syntax error, unexpected '}'
the code is
$content = Content::with(['menu' => function($query){
$query->where('content_id', '=', 'menu_id')
}])->get();
I don't see where the } is coming from. All the brackets close each other.
@Shiva sry i forgot semicolon :P
$content = Content::with(['menu' => function($query){
$query->where('content_id', '=', 'menu_id');
}])->get();
@SachinAgarwal - I get all of the titles, instead of the one that is connected to the menu id
Can you show you table structure and relationships you defines in eolquent models?
@SachinAgarwal - Sure no problem My menu table
--
-- Table structure for table `menus`
--
CREATE TABLE IF NOT EXISTS `menus` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`image` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`menu_id` int(10) unsigned NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=30 ;
My Content table
--
-- Table structure for table `content`
--
CREATE TABLE IF NOT EXISTS `content` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`content` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`image` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=51 ;
My content_menu table
--
-- Table structure for table `content_menu`
--
CREATE TABLE IF NOT EXISTS `content_menu` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`content_id` int(10) NOT NULL,
`menu_id` int(10) NOT NULL,
`created_at` timestamp NOT NULL,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ;
My relationships in my Menu model
public function content(){
return $this->belongsToMany('Content', 'content_menu', 'menu_id', 'content_id');
}
My relationships in my Content model
public function menu(){
return $this->belongsToMany('Menu', 'content_menu', 'content_id', 'menu_id');
}
Any Ideas?
@shiva And you want menu.menu_id = content_menu.content_id ?
or Content_menu.menu_id = menu.menu_id ?
or content_menu.menu_id - content_menu.content_id?
@nolrosHow about add the db name to table name?
@SachinAgarwal - I would like content_menu.menu_id = content_menu.content_id @chardlkin - What do you mean add the db name to table name?
@shiva May be because the column names are same, you might be not getting what you want. Not sure though. Just try with table name like this (M just writing the where clause):
->where("`content_menu`.menu_id" = "`content_menu`.content_id");
or
->where("content_menu.menu_id" = "content_menu.content_id");
My guesses are it should work. But not sure.
@SachinAgarwal - if I do
->where("`content_menu`.menu_id", "=", "`content_menu`.content_id");
Then I get this error
Column not found: 1054 Unknown column '`content_menu`.menu_id' in 'where clause' (SQL: select `menus`.*, `content_menu`.`content_id` as `pivot_content_id`, `content_menu`.`menu_id` as `pivot_menu_id` from `menus` inner join `content_menu` on `menus`.`id` = `content_menu`.`menu_id` where `content_menu`.`content_id` in (38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50) and ```content_menu```.`menu_id` = `content_menu`.content_id)
if I do
->where("content_menu.menu_id", "=", "content_menu.content_id");
Then I get a list of all the titles instead of the one that is suppose to go with the menu_id
@Shiva I found the exact prob with this thing and it is little weird. I made a new thread for it. Check it. You will understand what the prob is. https://laracasts.com/discuss/channels/general-discussion/l5-weirdness-in-querying-many-to-many-relation
@SachinAgarwal - I tried that one code that you used that you said worked in phpmyadmin but it doesn't seem to work. I kept getting an error. When I add
dd(DB::getQueryLog());
this is what I got
'select `menus`.*, `content_menu`.`content_id` as `pivot_content_id`, `content_menu`.`menu_id` as `pivot_menu_id` from `menus` inner join `content_menu` on `menus`.`id` = `content_menu`.`menu_id` where `content_menu`.`content_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and `content_id` = ?'
This is what I have in my homecontroller index function
$content = Content::with(['menu' => function($query){
$query->where('content_id', '`menu_id`');
}])->get();
@shiva This will give all users in eloquent. But if you run the generated query in phpmyadmin, it will work. I think either we are misunderstanding the concept of eager loading or Laravel Eloquent do not support this thing.
@SachinAgarwal - I tried the solution that was given to you but it isn't working. Maybe I did it wrong.
Here is the code
$content = Content::join('content_menu', function($join)
{
$join->on('content.id', '=', 'content_menu.content_id')->on('content.id', '=', 'content_menu.menu_id');
})->get();
when I use
dd(DB::getQueryLog());
I get
"select * from `content` inner join `content_menu` on `content`.`id` = `content_menu`.`content_id` and `content`.`id` = `content_menu`.`menu_id`"
@Shiva This will work I have tested it, the query generated is also perfect. Check for the typos. And if not working what is the error your are getting?
I don't get any errors. On my page I just get a blank page and in phpmyadmin when I run the query I don't get any errors there either
Any ideas
@SachinAgarwal I tried this
$content = Content::whereHas('menu', function($q)
{
$q->whereRaw('content.id = menu.id');
})->get();
but I get this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'menu.id' in 'where clause' (SQL: select * from `content` where (select count(*) from `menus` inner join `content_menu` on `menus`.`id` = `content_menu`.`menu_id` where `content_menu`.`content_id` = `content`.`id` and content.id = menu.id) >= 1)
any ideas
@SachinAgarwal - So I fixed the error, but nothing is showing up, I just get a blank page and if I run the query in phpmyadmin then it doesn't return any rows
Please or to participate in this conversation.