0

Even though there are many solutions provided for this error, I am encoutering, I have not find the solution yet, below is the error I am getting when trying to display data from the database on the bootstrap card ") ErrorException Undefined variable: published_articles (View: /home/nosi/Oresol-Lesotho-Skills-Exercise/blog/resources/views/article_data.blade.php) (View: /home/nosi/Oresol-Lesotho-Skills-Exercise/blog/resources/views/article_data.blade.php"

below is my route inside the web.php:

  Route::get('article_data', 'PublishArticlesController@retrieveArticles')->name('article_data');

below is my controller:

public function retrieveArticles(){

$articles = PublishArticle::latest()->paginate(6);
return view('article_data')->with(['published_articles'=>$articles]);

}

below is my blade file which is inside views, articles_data.blade.php:

    @extends('layouts.public_navbar')

@section('content')
<div class="container text-center">
    <h1>Explore Published Articles</h1>
</div>
<div class="container">
    <div class="card-deck row">
    @foreach ($published_articles as $article)
            <div class="col-xs-12 col-sm-6 col-md-4">
                <div class="card">
                    <div class="view overlay">
                        <a href="#!">
                            <div class="mask rgba-white-slight"></div>
                        </a>
                    </div>
                    <div class="card-body">
                        <h4 class="card-title">{{ $article->article_title }}</h4>
                        <p>Author: {{ $article->author_name }}</p>
                        <p class="card-text">{{ $article->article_body }}</p>
                        <a href="" class="btn btn-primary btn-md">Read more</a>
                    </div>
                </div>
            </div>
        @endforeach
    </div>
</div>

<!-- Add Bootstrap JS and Popper.js scripts (required for Bootstrap) -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
@endsection

your help will be highly apreciated

1
  • There are multiple ways to send your articles to your article_data view. 'with' would not have been my first choice. Try view('article_data', $articles), keep it simple. When things work, then apply your personal insights Commented Oct 30, 2023 at 4:12

1 Answer 1

0

When you use with() to pass data, the syntax is:

return view('article_data')->with('published_articles', $articles);

you can use an array if you pass the data in the view() function:

return view('article_data', ['published_articles'=>$articles]);
Sign up to request clarification or add additional context in comments.

Comments

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.