Неопределенная переменная: теги, отображение данных из базы данных на модале начальной загрузки - PullRequest
0 голосов
/ 27 сентября 2019

У меня есть простое приложение CRUD, в котором я хочу отображать теги во всплывающем моде начальной загрузки.

Вот что я пробовал до сих пор.

Контроллер тегов

class TagController extends Controller
{


    public function index()
    {
        $tags =Tag::all();
        return view('pages.index')->withTags($tags);
    }


    public function store(Request $request)
    {
        $this->validate($reguest, array('name'=>'required|max:255'));
        $tag = new Tag;
        $tag->name = $request->name;
        $tag->save();
        $request->session()->flash('succes', 'Tag was successful created!');
        return redirect()->route('pages.index');
    }


}

Теперь я хочу отобразить эти теги для представления, связанного с другим контроллером PageListController

Вот представление pages.index

@extends('layouts.app', ['activePage' => 'table', 'titlePage' => __('Table List')])

@section('content')
<div class="card-body">
    <div class="table-responsive">
      <table class="table">
        <thead class=" text-primary">
          <th>
            ID
          </th>
          <th>
            Page Name
          </th>
          <th>
          Articles
          </th>
          <th>
            Tags
          </th>

        </thead>
        <tbody>
          @foreach ($pages as $page)

          <tr>
              <td>
                {{$page->id}}
              </td>
              <td>
                {{$page->pagetitle}}
              </td>
              <td>
                {{$page->articlelist}}
              </td>
              <td>
                    <button type="button" class="btn btn-sm btn-success" data-toggle="modal" data-target="#tagsModel">
                            {{ __('view tags') }}
                          </button>

                              <!-- Modal -->
                              <div class="modal fade" id="#tagsModel" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                                <div class="modal-dialog" role="document">
                                  <div class="modal-content">
                                    <div class="modal-header">
                                      <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                                      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">&times;</span>
                                      </button>
                                    </div>
                                    <div class="modal-body">
                                        <table class="table">
                                            <thead class=" text-primary">
                                              <th>
                                                ID
                                              </th>
                                              <th>
                                               name
                                              </th>
                                            </thead>
                                            <tbody>
                                                @foreach ($tags as $tag)   
                                                <tr>
                                                    <td>
                                                        {{$tag->id}}
                                                    </td>
                                                    <td>
                                                       {{$tag->name}}
                                                    </td>
                                                </tr>
                                                @endforeach

                                            </tbody>
                                          </table>
                                    </div>
                                    <div class="modal-footer">
                                      <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                      <button type="button" class="btn btn-primary">Save changes</button>
                                    </div>
                                  </div>
                                </div>
                              </div>
                </div>
              </td>

          </tr>
          @endforeach
        </tbody>
      </table>
    </div>
  </div>
@endsection

Вот мой файл маршрутов web.php

<?php

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

Route::get('/home', 'HomeController@index')->name('home')->middleware('auth');
Route::resource('pages', 'PageListController');
Route::resource('pages', 'TagController');


Route::group(['middleware' => 'auth'], function () {
    Route::get('table-list', function () {
        return view('pages.index');
    })->name('table');
});

Route::group(['middleware' => 'auth'], function () {
    Route::resource('user', 'UserController', ['except' => ['show']]);
    Route::get('profile', ['as' => 'profile.edit', 'uses' => 'ProfileController@edit']);
    Route::put('profile', ['as' => 'profile.update', 'uses' => 'ProfileController@update']);
    Route::put('profile/password', ['as' => 'profile.password', 'uses' => 'ProfileController@password']);
});

Я просто хочу отображать теги, используя всплывающее окно начальной загрузки.

Теперь, когда я запускаю свое приложение, я получаю следующую ошибку

Undefined variable: pages (View: C:\custom-xammp\htdocs\royalad-dashboard\resources\views\pages\index.blade.php)

Что яделать неправильно в моем коде?любая помощь или предложения будут оценены

1 Ответ

1 голос
/ 27 сентября 2019

Вы используете $ страниц в вашем блейде, который не был отправлен.Было отправлено только $ тэгов.

Если у вас есть модель PageList, попробуйте это:

public function index()
{
    $tags = Tag::all();
    $pages = PageList:all();
    return view('pages.index', compact('tags', 'pages'));
}
...