简体   繁体   English

未定义变量:Blade Laravel 7.x 中的横幅

[英]Undefined variable: banners in Blade Laravel 7.x

I'm new in laravel 7.x.我是 laravel 7.x 的新手。 I'm getting an error "Undefined variable: banners (View: C:\xampp\htdocs\mytravel\resources\views\admin\index.blade.php) " Please help to find Where is the problem and how can i solve it?我收到错误“未定义变量:横幅(查看:C:\xampp\htdocs\mytravel\resources\views\admin\index.blade.php)”请帮助查找问题出在哪里以及如何解决?

Here is BannerController.php这是BannerController.php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Banner;

class BannerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // $banners = Banner::all()->toArrary();
        // return view('admin.index', ['banners' =>$banners]));

        $banners = Banner::all();
        return view('admin.index', compact('banners'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.layout.bannercustomize');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'h4_title' => 'required',
            'h2_title' => 'required',
            'banner_paragraph' => 'required'
        ]);

        $banner = new Banner([
            'h4_title' => $request->get('h4_title'),
            'h2_title' => $request->get('h2_title'),
            'banner_paragraph' => $request->get('banner_paragraph'),
        ]);
        $banner->save();

        return redirect()->back()->with('success', 'Data Added');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

Here is web.php这是web.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('index');
});

Route::get('/contact-us', function () {
    return view('contactus');
});

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

// Admin panel Pages Routes

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

Route::get('/admin/bannercustomize', function () {
    return view('admin/layout/bannercustomize');
});
// Controller routes
Route::resource('banner', 'BannerController');

Here is model Banner.php这是 model Banner.php


namespace App;

use Illuminate\Database\Eloquent\Model;

class Banner extends Model
{
    protected $fillable = ['h4_title', 'h2_title', 'banner_paragraph', 'banner_photo'];
}

Here is index.blade.php这是index.blade.php


@section('content')
<div class="container-scroller">
      <!-- partial:partials/_navbar.html -->
      @include('admin.layout.nav')

      <!-- partial -->
    <div class="container-fluid page-body-wrapper">
        <!-- partial:partials/_sidebar.html -->
        @include('admin.layout.sidebar')
        <!-- partial -->
        <div class="main-panel">
          <div class="content-wrapper">
            <!-- Page Title Header Starts-->
            <div class="row page-title-header">
              <div class="col-12">
                <div class="page-header">
                  <h4 class="page-title">Dashboard</h4>

                </div>
              </div>

            </div>
            <!-- Page Title Header Ends-->


            <div class="row">
              <div class="col-md-12">
                <div class="row">
                  <div class="col-md-12 grid-margin">
                    <div class="card">
                      <div class="card-body">
                        <div class="d-flex justify-content-between">
                          <h4 class="card-title mb-0">Banner</h4>
                          <a href="#"><small>Show All</small></a>
                        </div>
                        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Est quod cupiditate esse fuga</p>
                        <div class="table-responsive">
                          <table class="table table-striped table-hover">
                            <thead>
                              <tr>
                                <th>ID</th>
                                <th>H4 Title</th>
                                <th>H2 Title</th>
                                <th>Paragraph</th>
                                <th>Image</th>
                                <th>Edit</th>
                                <th>Delete</th>
                              </tr>
                            </thead>
                            <tbody>
                              @foreach($banners as $banner)
                              <tr>
                                <td>{{$banner->id}}</td>
                                <td>{{$banner->h4_title}}</td>
                                <td>{{$banner->h2_title}}</td>
                                <td>{{$banner->banner_paragraph}}</td>
                                <td>no image</td>
                                <td></td>
                                <td></td>
                              </tr>
                              @endforeach
                            </tbody>
                          </table>
                        </div>
                      </div>
                    </div>
                  </div>


                </div>
              </div>

            </div>

          </div>
          <!-- content-wrapper ends -->
          <!-- partial:partials/_footer.html -->
          @include('admin.layout.footer')
          <!-- partial -->
        </div>
        <!-- main-panel ends -->
    </div>
      <!-- page-body-wrapper ends -->
</div>
<!-- container-scroller -->

@endsection

NB: I'm trying to show all my table data in admin index.blade.php file注意:我正在尝试在管理 index.blade.php 文件中显示我的所有表格数据

You have a callback in your route.. In this function its redirecting to the blade without the data,, So you are not getting the data in blade.. Its never going to the controller您的路线中有回调.. 在此 function 中,它重定向到没有数据的刀片,因此您没有在刀片中获取数据.. 它永远不会到达 controller

You have to use like this你必须像这样使用

Route::get('/',   'BannerController@index');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM