PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 21, 2022

[FIXED] How to get the primary key of the Users table to comments table foreign key using eloquent in Laravel 7 or 8

 October 21, 2022     eloquent, has-many, has-many-through, laravel     No comments   

Issue

i want to get or join the users table into my comments table. but i dont have a idea how to do that i used hasMany to get all the comments table to posts . and i want to join the users table to

here is my Model Posts

namespace App\Models;
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    class Posts extends Model
    {
        use HasFactory;
        public function comments(){
            return $this->hasMany(Comments::class,'posts_id');
        }
    }

here is my PostsController i used hasMany with posts and comments tables

namespace App\Http\Controllers;
use App\Models\Posts;
class UserController extends Controller
{
    public function index(){
        return Posts::with('comments')->get();
        return view('welcome',['authors'=>$authors]);
    }
}

the output of my PostsController using hasMany

   [
    {
    "id": 1,
    "post": "im in relationship now",
    "comments": [
            {
            "posts_id": 1,
            "comments_id": 1,
            "users_id": 1,
            "comments": "sweet"
            },
            {
            "posts_id": 1,
            "comments_id": 2,
            "users_id": 2,
            "comments": "grats"
            },
            {
            "posts_id": 1,
            "comments_id": 3,
            "users_id": 3,
            "comments": "wow"
            },
            {
            "posts_id": 1,
            "comments_id": 4,
            "users_id": 4,
            "comments": "grats dudes"
            }
        ]
    },
    {
    "id": 2,
    "post": "im graduate now",
    "comments": [
            {
            "posts_id": 2,
            "comments_id": 5,
            "users_id": 5,
            "comments": "stay strong"
            },
            {
            "posts_id": 2,
            "comments_id": 6,
            "users_id": 6,
            "comments": "sweets"
            },
            {
            "posts_id": 2,
            "comments_id": 7,
            "users_id": 7,
            "comments": "ayiee"
            }
        ]
    }
]

on the output you see the comments has a users_id i want to join the users table to grab the information of that table like name and username


Solution

in Comment.php model add this relation

public function user() {
        return $this->belongsTo( User::class );
    }

in for loop of comments in view

{{$comments->user->name}}


Answered By - Rajkumar Sharma
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing