0

データベースにデータを渡し、それを Welcome.blade.php に表示する方法を知りたいです。データは既にありますが、このビューに表示することはできません。

これは私codeの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');
    }
}

これはweb.phpの私のコードです。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');
    }
}

今私の Welcome.blade.php で、データベースにデータを表示したいのですが、今のコードはこれです。

@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

これは私のweb.phpコードです

<?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');
});

また、これは私の Post.php モデルです

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $fillable = [

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

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

しかし、なぜこの種のエラーが発生するのですか?

foreach() に無効な引数が指定されました (View: C:\xampp\htdocs\porge\dev\resources\views\welcome.blade.php)

4

2 に答える 2