laravel get data from multiple tables

$user = User::select("id", "name")
        ->with(['positions' => function ($query) {
            $query->select('name');
        }, 'profile' => function ($query) {
            $query->select("user_id", "company_name"); 
        }])->get();


In User model write many to many relation with user positions (designation)

public function positions()
{
    return $this->belongsToMany(\App\Position::class, 'user_position', 'user_id', 'position_id')
    ->withPivot(['position_id', 'user_id']); //if you don't need pivot you can remove it
}

In user Model relation with profile table
public function profile()
{
    return $this->hasOne(Profile::class, 'user_id', 'id');
}

4.13
8
Susan B 55 points

                                    <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

4.13 (8 Votes)
0
0
10
Unohu 115 points

                                    use App\Models\Post;

$comments = Post::find(1)->comments;

foreach ($comments as $comment) {
    //
}

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
laravel db select multiple tables get multiple tables data in one array laravel how to get multi table data in one query laravel using joints with where clause how to get multi table data in one query laravel using joints get data from multiple tables laravel using orm how to get data from more than two tables in laravel eloquent laravel withtrashed multiple tables store multiple data in one table view laravel get all data from mutiple table laravel how to get data from two table using eloquent laravel query multiple tables laravel fetch from multiple tables laravel how to display data from two tables in laravel multiple table laravel query data from two tables laravel how to get data from multiple table using single model in laravel detail data multiple table laravel laravel select from multiple tables how to fetch data from two tables in laravel get data from multiple tables in laravel how to get 2 values from laravel table laravel 8 multiple tables how to select something from two tables in laravel eloquent how to fetch multiple table in one view in laravel blade get data from multiple tables show multiple data from various table in laravel how to get data from two tables using one query in laravel laravel eloquent with multiple tables search data from multiple tables in laravel laravel get from 2 table in one variable get 2 table data laravel how can we get data from multiple tables on single blade file in laraval select multiple rows laravel datatbale get data from two tables in laravel laravel collect data from 2 tables into one how to store multiple tables data in laravel controller show data of two table laravel laravel db select from multiple tables how to get data from two table in laravel with condition how to get data from two table in laravel laravel query to get data from two tables based on condition fetch data from multiple tables in laravel eloquesnt fetch data from multiple tables in laravel using eloquent to get data from multiple tables laravel multiple tables through a single column How to search data from multiple table in laravel find value from multiple tables in laravel get all data of two tables using with laravel laravel get data from multiple tables laravel return values from multiple table how to select data from two tables in sql laravel select multiple tables in laravel how to get two table data in one query laravel eloquent get data from multiple tables get data from two tables laravel eloquent retrieve multiple tables how to get data from two table in one html table in laravel laravel get data from two tables DB laravel get data from two tables laravel 8 how to display data from multiple tables select data from two table laravel laravel select multiple tables how to get data from 2 tables in mysql laravel how to select data from two table in one query using laravel how to fetch data from multiple table in laravel how to get two table data in laravel laravel get data from multiple tables with
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source