0

What I'm trying to achieve is post all news by @foreach and between the @foreach do another @foreach to post all comments with the ID from the news post.

I'm unsure on how to pass this ID, to the getNewsComments function.

My controller:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\News;
use App\newsComments;


class newsController extends Controller
{
    public function getAllNews(){
        $results = News::all();
        return view('index')->with('news', $results);
    }
}

Route:

Route::get('/', 'newsController@getAllNews');

News model:

class News extends Model
{
    // set table
    protected $table = 'lg_news';


    public function newsComments(){
        return $this->hasMany('App\newsCommments');
    }
}

Comment model:

class newsComments extends Model
{
    // set table name
    protected $table = 'lg_newscomments';

}

view

@foreach ($news as $article)

  @foreach($news->$newsComments as $comment)
  @endforeach
@endforeach

Error:

Undefined variable: newsComments (View: C:\xampp\htdocs\resources\views\index.blade.php)

2 Answers 2

2

Change this line:

return view('index')->with('news', $results);

to

return view('index', ['news' => $results]);

and it probably will work.

PS: with() function will set a session! It will not pass a variable to view.

Sign up to request clarification or add additional context in comments.

12 Comments

Getting the same error: Undefined variable: newsComments (View: C:\xampp\htdocs\resources\views\index.blade.php)
change that method name to comments (because we are referring to that with $post->comments in the view page) and it will work
You mean this bit? @foreach($news->$newsComments as $comment) - if I change it to @foreach($news->$comments as $comment) it gives me the same error, but with comments.
remove the $ sign before the the comments! It needs to be @foreach($news->comments as $comment) Reminder: And be sure to rename the method name at News model to comments instead of newsComments That will fix your issues
Property [comments] does not exist on this collection instance. I've removed the $ and changed the method to public function comments()
|
1

You don't need multiple routes you just need to have two tables related to each other with 1:N relationship

Article (1) -> Comments (N)

Then you will create a model for each table and create the relation like explained in the Laravel documentation

One to Many relationship in Laravel

Then you will fetch all posts and pass them to the view:

public function getAllArticles()
{
 $posts = Post::all();
 return view('view-name', $posts);
}

And at the end, create a view and show posts and comments:

    @foreach($posts as $post)
     {{ $post->title }}
     {{ $post->body }}
     @foreach($post->comments as $comment)
      {{ $comment->title }}
      {{ $comment->body }}
     @endforeach
    @endforeach

Reminder: $post->comments , comments is the method name defined in the model where you create the relationship

Define route at web.php:

Route::get('/', 'ControllerName@getAllArticles');

Go to localhost:8000/ (or to your site domain if the site is hosted) to see the result

7 Comments

I've been messing around with it, but I don't get it to work. A question: Route::get('/post/{id}', 'ControllerName@methodName')->name('route-name'); Does this route only get posts if you visit the url /post/'id'? Because I need it on my index page, and not on a seperate page.
ControllerName is your controller where you have defined the method and methodName is the method name itself. If for example your controller is called ArticleController and the method inside that controller is called getArticleById you have to define the route like this Route::get('/article/{id}', 'ArticleController@getArticleById'); And URL address is localhost:8000/article/1 (will show the post with id equal to 1)
Yes, I got that. But doing /article/{id} doesn't this mean you'll actually have to 'visit' www.website.com/article/23 (for example)? What I would like to achieve, is to have the News on the index page + comments aswel.
Yes exactly! See the answer I just edited :) and mark it as correct if it helped you solve the problem. Best of luck
What I would like to achieve, is to have the News on the index page + comments aswel, without having to navigate to www.website.com/article/1. So it's just www.website.com where News is shown + Comments of the news, sorry if I wasn't clear enough.
|

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.