Answer the question
In order to leave comments, you need to log in
Get forum.name via thread.id in post column (table joins)?
The project is old, in the new version of Laravel you can set up a relation without any problems through Eloquent ORM. And here are the entries:
$posts = Post::orderBy('id', 'desc')
->join('threads', 'threads.id', '=', 'posts.thread_id')
->join('forums', 'forums.id', '=', 'threads.forum_id')
->join('users', 'users.id', '=', 'posts.user_id')
->select('posts.text', 'posts.id', 'posts.sticky', 'users.first_name', 'users.last_name', DB::raw('threads.name as thread_name'), DB::raw('threads.id as thread_id'), DB::raw('forums.name as forum_name'));
$posty = Post::join('threads', 'threads.id', '=', 'posts.thread_id')
->join('forums', 'forums.id', '=', 'threads.forum_id')
->join('users', 'users.id', '=', 'posts.user_id')
->select('posts.text', 'posts.id', 'posts.sticky', 'users.first_name', 'users.last_name', DB::raw('threads.name as thread_name'), DB::raw('threads.id as thread_id'), DB::raw('forums.name as forum_name'));
dd($posty->forum_name);
Whoops, looks like something went wrong.
1/1
ErrorException in PostsController.php line 115:
Undefined property: Illuminate\Database\Eloquent\Builder::$forum_name
in PostsController.php line 115
at HandleExceptions->handleError(8, 'Undefined property: Illuminate\\Database\\Eloquent\\Builder::$forum_name', 'C:\\Users\\BonBonS\\Desktop\\OpenServer\\domains\\forum2\\app\\Http\\Controllers\\Admin\\PostsController.php', 115, array('post' => object(Post), 'posty' => object(Builder))) in PostsController.php line 115
at PostsController->edit(object(Post))
....
Answer the question
In order to leave comments, you need to log in
Answer:
$posts = Post::orderBy('id', 'desc')
->join('threads', 'threads.id', '=', 'posts.thread_id')
->join('forums', 'forums.id', '=', 'threads.forum_id')
->join('users', 'users.id', '=', 'posts.user_id')
->select('posts.text', 'posts.id', 'posts.sticky', 'users.first_name', 'users.last_name', DB::raw('threads.name as thread_name'), DB::raw('threads.id as thread_id'), DB::raw('forums.name as forum_name'));
$post = $posts->find($post->id);
dd($post->forum_name);
You need to learn the join hardware first, and then try to use them in laravel.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question