Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9115873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:32:11+00:00 2026-06-17T04:32:11+00:00

i have 2 table and a pivot table. structure of the table is mentioned

  • 0

i have 2 table and a pivot table. structure of the table is mentioned below.

users table:
id username password
1   admin    xyzasd

roles table:

id  name
1   role1
2   role2

role_user table:

id user_id role_id
1    1       1
2    1       2

My user model:

class User extends Basemodel{
    public static $table = 'users';
    public static $timestamps = true;

    public function roles()
    {
        return $this->has_many_and_belongs_to('Role');
    }

    public static function menu(){
        $roles = User::find(1)->roles()->get();
        return $roles;
    }
}

my controller:

public function get_index()
    {
        return View::make('home.index')
            ->with('title','testing many to many')
            ->with('menu',User::menu());
    }

and in my blade view file i have this statement {{$menu}} all i get is a message array, can some one guide me on how to fetch the records ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T04:32:12+00:00Added an answer on June 17, 2026 at 4:32 am

    First, User should extend Eloquent. Also, you should create a Role model.

    //application/models/User.php

    class User extends Eloquent
    {
        public static $table = 'users';
        public static $timestamps = true;
    
        public function roles()
        {
            return $this->has_many_and_belongs_to('Role');
        }
    }
    

    //application/models/Role.php

    class Role extends Eloquent
    {
        public static $table = 'roles';
        public static $timestamps = true;
    
        public function users()
        {
            return $this->has_many_and_belongs_to('User');
        }
    }
    

    Then for your controller you basically can just pass the user.

    public function get_index()
    {
        $user = User::find(1);
    
        return View::make('home.index')
            ->with('title','testing many to many')
            ->with('user', $user);
    }
    

    Then in your view you can do cool stuff like:

    <h1>What's up {{ $user->username }}?</h1>
    <h2>You have the following roles:</h2>
    @foreach($user->roles as $role)
        <p>{{ $role->name }}</p>
    @endforeach
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

need help in error in database pivot. i have table tamed table_score like below:
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),
Possible Duplicate: SQL Server dynamic PIVOT query? I have temporary table with following structure:
Question 1: I have a table with the below structure and data: app_id transaction_id
Hi I have a table test its structure is given below: **Testing** PK C1
I have a pivot table, well of course every row will be included in
I have table to test score data that I need to pivot and I
I have created a GUI that will setup the pivot table. For example, I
how to carry out the pivot function i have a table with datas Day
Possible Duplicate: How can i made a pivot for this I have a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.