0

I would like to know how can i pass the data in my database and display it in Welcome.blade.php i already have a data but i can't make it display in this view

this is my code in HomeController.php.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        
        $this->middleware('auth');
        /**
          * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */

    }
    public function index()
    {
        return view('home');
    }
}

and this is my code in web.php, see that i am using HomeController@index

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        
        $this->middleware('auth');
        /**
          * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */

    }
    public function index()
    {
        return view('home');
    }
}

now in my Welcome.blade.php i want to display the data in my database and my code right now is this.

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                @foreach($posts ?? '' as $post)
                <div class="card-header">
                    {{$post->title}}
                </div>
                @endforeach
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

and this is my web.php code

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('about', function () {
    return view('about');
})->middleware('checking');

Auth::routes(['register' => false]);

Route::group(['middleware' => 'auth'], function(){

    Route::get('/home', [
        'uses' => 'HomeController@index',
        'as' => 'home'
    ]);

    Route::get('/post/create', [

        'uses' => 'PostsController@create',
        'as' => 'post.create'
    ]);
    
    Route::post('/blogs/store', [

        'uses' => 'PostsController@store',
        'as' => 'blogs.store'
    ]);

    Route::get('/blogs/delete/{id}', [

        'uses' => 'PostsController@destroy',
        'as' => 'blogs.delete'
    ]);

    Route::get('/blogs/edit/{id}', [

        'uses' => 'PostsController@edit',
        'as' => 'blogs.edit'
    ]);

    Route::post('/blogs/update/{id}', [

        'uses' => 'PostsController@update',
        'as' => 'blogs.update'
    ]);


    Route::get('/posts', [
    
        'uses' => 'PostsController@index',
        'as' => 'posts'
    ]);
});

Route::get('register', function () {

    return redirect('/home');
});

also this is my Post.php model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [

    'title','content','featured'
    ];

    public function getFeaturedAttribute($featured) {
        
        return asset($featured);
    }
}

but why am i getting this kind of error?

Invalid argument supplied for foreach()(View: C:\xampp\htdocs\porge\dev\resources\views\welcome.blade.php)

jaze cara
  • 9
  • 2

2 Answers2

1

Alright. this is what I see. you want to display the data in the welcome view which is loaded directly in your routes without passing any data.

Route::get('/', function () {
    return view('welcome');
});

=> My suggestions: The Common way to pass data is to use a controller. So you can use in your case, the HomeController or create a WelcomeController (what I will use next) .

Your public index function in the WelcomeController

public function index()
{
    $posts = Post::all();
    return view('welcome')->with('posts', $posts);
}

Your route for /

Route::get('/', [
    'uses' => 'WelcomeController@index',
    'as' => 'welcome_page'
]);

And then you loop the post like that in the welcome view

@foreach($posts as $post)
    <div class="card-header">
        {{$post->title}}
    </div>
@endforeach

hopefully that helps.

Ravier
  • 41
  • 3
0

I see several problem here. And yes, you should post your web.php part that matters for this request. Assuming your table for 'posts' has a model called Post

public function index()
{
   $posts = Post::all();
   return view('home', compact('posts'));
}

In your blade:

<div class="card">
  <div class="card-body">
    <ul class="list-group">
    @foreach($posts as $post)
        <li class="list-group-item">
            {{$post->title}}
        </li>
    @endforeach
     </ul>
   </div>
</div>

That'll look a lot cleaner, don't you think?

Dimitri Mostrey
  • 2,302
  • 1
  • 12
  • 11