Как добавить пользовательские действия против любой кнопки в Laravel? - PullRequest
0 голосов
/ 15 апреля 2019

Я делаю веб-приложение, в котором есть две таблицы: классы и разделы. Мои миграции показаны ниже.

Классы

Schema::create('classes', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->timestamps();
});

Разделы

Schema::create('sections', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->integer('class_id')->unsigned();
    $table->foreign('class_id')->references('id')->on('classes');
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
    $table->timestamps();
});

В моем файле Blade я хочу показать все кнопки классов. Когда я нажимаю на эту кнопку, она должна показывать все разделы, где class_id в таблице разделов выбранной кнопки.

Я застрял из-за синтаксиса. Я новичок в Laravel и не знаю, как добавить ответ против кнопки.

Мой контроллер для секции

public function index()
{
    $sections = Section::all();
    $class = Class::all();
    $users = User::all();

    return view('sections.index')->with('sections', $sections)->with('class', $class)->with('users', $users);
}

лезвие

@foreach($classs as $cat)

    <button class="btn btn-info">{{$cat->title}}</button>

    //this shows all buttons with class name

@endforeach

<div class="box-body">
    <table class="table table-responsive">
        <thead>
        <tr>
            <th>Section Name</th>
            <th>Class</th>
            <th>Teacher</th>
            <th>Modify</th>
        </tr>
        </thead>

Как я могу получить все разделы только выбранного класса под этими таблицами?

Это предложенный код, в котором я внес изменения, я не получаю данные при нажатии на кнопку. Пожалуйста, скажи мне, что мне делать? Нужно ли менять контроллер

@foreach($classs as $cat)
    <button class="btn btn-info class_btn" id="{{$cat->class_id}}">{{$cat->title}}</button>
    <div class="box-body" style="display:none" id="table_{{$cat->class_id}}">
        <table class="table table-responsive">
            <thead>
            <tr>
                <th>Section Name</th>
                <th>Class</th>
                <th>Teacher</th>
                <th>Modify</th>
            </tr>
            </thead>
            <tbody>
            @foreach($sections as $sec)
                <tr>
                    <td>{{$sec->title}}</td>
                    <td>
                        {{ \Illuminate\Support\Facades\DB::table('classses')->where('id',$sec->class_id)->value('title')}}
                    </td>
                    <td>
                        {{ \Illuminate\Support\Facades\DB::table('users')->where('id',$sec->user_id)->value('name')}}
                    </td>
                    <td>
                        <button class="btn btn-info" data-mytitle="{{$sec->title}}" data-myclassid="{{$sec->class_id}}"  data-myuserid="{{$sec->user_id}}" data-secid={{$sec->id}} data-toggle="modal" data-target="#edit">Edit</button>
                        /
                        <button class="btn btn-danger" data-secid={{$sec->id}} data-toggle="modal" data-target="#delete">Delete</button>
                    </td>
                </tr>

            @endforeach
            </tbody>
        </table>
    </div>
@endforeach
<script>
    //onclick show particular table
    $('.class_btn').on('click',function(){
        $('.box-body').hide();
        $('#table_'+$(this).attr('class_id')).show();
    });
    </script>

1 Ответ

0 голосов
/ 15 апреля 2019

Вы используете Jquery и функция hide (), show () показывает конкретную таблицу определенному классу Like As

@foreach($class as $cat)
    <button class="btn btn-info class_btn" id="{{Your_class_id}}">{{$cat->title}}</button>
    <div class="box-body" style="display:none" id="table_{{Your_class_id}}">
        <table class="table table-responsive">
            <thead>
            <tr>
                <th>Section Name</th>
                <th>Class</th>
                <th>Teacher</th>
                <th>Modify</th>
            </tr>
            </thead>
            <tbody>
                //print your data
            </tbody>
        </table>
    </div>
@endforeach
<script>
//onclick show particular table
$('.class_btn').on('click',function(){
    $('.box-body').hide();
    $('#table_'+$(this).attr('id')).show();
});
<script>

Другой способ использования Ajax ....

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...