0

I want to build query like this in laravel 5.3

SELECT p.post_text, p.bbcode_uid, u.username, t.forum_id, t.topic_title, t.topic_time, t.topic_id, t.topic_poster
FROM phpbb_topics t, phpbb_posts p, phpbb_users u
WHERE t.forum_id = 9
AND p.post_id = t.topic_first_post_id
AND u.user_id = t.topic_poster
ORDER BY t.topic_time
DESC LIMIT 10

1 Answer 1

2

Consider for example there are two tables one is users and the other is posts

while creating migration add user_id foreign key to your posts table as

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

create User and Post model, and in your User model define relationship to Post as

public function post(){
    return $this->hasMany(Post::class);
}

and in your Post model define a relationship to User model as

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

now you can get the user from Post model

$post = Post::findOrFail(1);
return $post->user;
Sign up to request clarification or add additional context in comments.

2 Comments

I don't wanna use foreign key. without that how can i implement it?
If don't want to use foreign key then it's not possible you either execute the raw query or use eloquent join methods. But using foreign key has many advantages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.