如何将值从视图传递到控制器

时间:2019-01-13 08:13:40

标签: laravel model-view-controller

在视图中,当我们将按钮路由到控制器内部的函数时,如何在该视图中传递当前值中的两个或更多个值。

我正在练习创建学生成绩管理系统。在从ResultController的索引路由的视图中,我们具有用于查看类..或单个学生的评分表的链接选项。当我们单击选择班级时,它会重定向到一个视图,其中有两个下拉菜单可以选择班级和一批学生。当我们选择受尊重的班级和批次时,值class_id和batch_id被路由到ResultControler中的函数结果,我们从该班级和批次中选择学生。在该视图中,我们显示了学生的分数表(如果有的话),下面我添加了一个按钮来添加分数/创建分数表。

但是,我很困惑如何才能通过按钮传递那些class_id和batch_id以在ResultController中创建函数。

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



public function choose()
{
    $classes= Sclass::all();
    $batches= Batch::all();

    return view('chooseclassbatchresult',compact('classes','batches'));
}

public function result(Request $request)
{
    $classid = $request->class;
    $batchid = $request->batch; 
    //dd($batchid);

    $students =Student::where('sclass_id',$classid)
                        ->where('batch_id', $batchid)
                        ->whereHas('subject')
                        ->get();

    $class= Sclass::findOrFail($classid);                    

    return view('showstudentresult',compact('students','class','classid','batchid'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
     // I need class_id and batch_id here


   // dd($classidd);

    $students = Student::where('sclass_id',$classid)
                        ->where('batch_id',$batchid)
                        ->whereDoesntHave('subject')
                        ->get();

         //dd($students);               

路线:

Route::get('/rms','MainPageController@index')->name('rms');

Route::get('results/choose','ResultController@choose')->name('chooseresult');

Route::post('/showstudentresult','ResultController@result')->name('showstudentresult');

Route::resource('results','ResultController');

chooseclassbatchresult.blade.php

@extends('layout')


@section('content')
    <h1>Please Choose the Class and Respected Batch Of Student For Result</h1>

    </br>
    </br>

    <form action="{{route('showstudentresult')}}" method="post">
        @csrf

        <p>
            <label>Class Name</label>

            <select name='class'>
                @foreach($classes as $class)
                    <option value="{{$class->id}}">{{$class->name}}</option>
                @endforeach
            </select>
            </br>
        </p>

        <p>
            <label>Batch</label>
            <select name='batch'>

                @foreach($batches as $batch)
                    <option value="{{$batch->id}}">{{$batch->batch}}</option>
                @endforeach
            </select>

        </p>

        </br>

        <input type="submit" value="View">
    </form>

    </br>
    </br>
    </br>

    <h1>OR</h1>

    <h3>
        <button><a href={{route('students.create')}}>Add New Student</a></button>
    </h3>
@endsection

Showstudentresult.blade.php

@extends('layout')


@section('content')

    <table border=1>
        <thead>

        <tr>
            <th>S.N</th>
            <th>Name</th>
            <th>Roll NO</th>
            @foreach($class->subjects as $subject)
                <th>{{$subject->name}}</th>
            @endforeach
            <th>Total Mark</th>
            <th>Percentage</th>
            <th>Division</th>
            <th>Action</th>
        </tr>
        </thead>
        <tbody>
        <?php $id = 1; ?>

        @foreach($students as $student)
            <tr>
                <td><?php echo $id;?></td>
                <td>{{$student->name}}</td>
                <td>{{$student->roll}}</td>
                @foreach($student->subjects as $subject)
                    <th>{{$subject->pivot->mark}}</th>
                @endforeach
                <td>{{$student->result->total_mark}}</td>
                <td>{{$student->result->percentage}}</td>
                <td>{{$student->result->division}}</td>
                <td>
                    <table>
                        <tr>
                            <td>
                                <button><a href="{{route('students.edit',$student->id)}}">Edit</a></button>
                            </td>
                            <td>
                                <form action="{{route('students.destroy',$student->id)}}" method="post">
                                    @csrf
                                    @method('DELETE')
                                    <input type="submit" value="Delete"
                                           onclick="return confirm('Are you sure you want to delete the student?')">
                                </form>
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <?php $id++ ?>
        @endforeach
        </tbody>
    </table>

    </br>
    </br>

    <button><a href={{results.create}}>Create New</a></button>
@endsection

2 个答案:

答案 0 :(得分:0)

如罗斯威尔逊在评论中建议的那样

  

我建议创建一个类似的单独页面   selectclassbatchresult结果具有提交要创建的数据的表单

在路线文件中添加一条路线,例如:

Route::post('/createPage', 'ResultController@createPage')->name('createPage');

ResultController 中添加以下功能:

public function createPage(Request $request)
{
    // Get your required ids here
    $classid = $request->class;
    $batchid = $request->batch; 
    //dd($classid);
    //dd($batchid );
}

在您的chooseclassbatchresult视图中,添加下面的另一种表单

<form action="{{ route('createPage') }}" method="post">
        @csrf
        <p>
            <label>Class Name</label>

            <select name='class'>
                @foreach($classes as $class)
                    <option value="{{$class->id}}">{{$class->name}}</option>
                @endforeach
            </select>
            </br>
        </p>

        <p>
            <label>Batch</label>
            <select name='batch'>

                @foreach($batches as $batch)
                    <option value="{{$batch->id}}">{{$batch->batch}}</option>
                @endforeach
            </select>

        </p>

        </br>

        <input type="submit" value="View">
</form>

答案 1 :(得分:0)

感谢您的回复。我有办法解决这个问题。我了解到可以使用输入type =“ hidden”将这些值带回控制器。

创建路线:

Route::post('/create_res', 'ResultController@create_res')->name('results.create_res');

在视图中,chooseclassbatchresult.blade.php

<form action="{{route('results.create_res')}}" method="POST">
    @csrf

<input type="hidden" name="classid" value="{{$classid}}">
<input type="hidden" name="batchid" value="{{$batchid}}"> 


<input type="submit" value="Create Mark Sheet">

</form>

在结果控制器中;

public function create_res(Request $request){


        $classid = $request->classid;

        $batchid = $request->batchid;



        $students = Student::where('sclass_id',$classid)
                            ->where('batch_id',$batchid)
                            ->whereDoesntHave('subject')
                            ->get();

         $classes= Sclass::findOrFail($classid);   
         //dd($students);            


        return view('addmarksheet',compact('students','classes'));
    }