Php 获取表中某一行的数据-Laravel 5.2

Php 获取表中某一行的数据-Laravel 5.2,php,mysql,laravel,Php,Mysql,Laravel,我有一个带有输入字段的表,我必须从中获取信息并保存它,但我也必须从表中的每一行获取信息。我有一个提交按钮为整个表 我需要获取$student的id和$subject的id。我怎样才能得到它们 我在把桌子贴上去 <table class="table table-responsive table-striped" id="admin-table"> <thead> <tr> <th>Клас:</th>

我有一个带有输入字段的表,我必须从中获取信息并保存它,但我也必须从表中的每一行获取信息。我有一个提交按钮为整个表

我需要获取
$student
的id和
$subject
的id。我怎样才能得到它们

我在把桌子贴上去

 <table class="table table-responsive table-striped" id="admin-table">
    <thead>
    <tr>
        <th>Клас:</th>
        <th>N:</th>
        <th>Име:</th>
        <th>Предмет:</th>
        <th>Оценка:</th>
        <th>Тип на оценката:</th>

    </tr>
    </thead>
    <tbody>
    @foreach($students as $student)
        @foreach($class as $classes)
          @foreach($sub as $subject)
        <tr>
            <td>
                {{$classes->name}}
            </td>
            <td>
                {{$student->number_in_class}}

            </td>
            <td>
                {{$student->full_name}}
            </td>
            <td>
                {{$subject}}
            </td>
            {!! Form::open(['action' => 'Educator\AccountController@markStudent', 'class' => 'form-horizontal']) !!}
            <td>
                {!! Form::text('mark',null, ['class'=>'form-control col-md-2']) !!}
            </td>
            <td>
                {!! Form::select('markType', $markType, null, ['class'=>'form-control']) !!}
            </td>
        </tr>
            @endforeach
          @endforeach
        @endforeach
    </tbody>
</table>
    <div align="center">
        <a href="{{url('educator/class-subject')}}"><button type="button" class="btn btn-default">Назад</button></a>
        {!! Form::submit('Запиши', ['class' => 'btn btn-default']) !!}

    </div>
    {!! Form::close() !!}
以及我保存所需数据的方法:

public function markStudent(Request $request)
{
    $validator = Validator::make($request->all(), [
        'mark' => 'numeric|required|min:2|max:6',
    ]);

    if ($validator->fails()) {
        return redirect()
            ->back()
            ->withErrors($validator)
            ->withInput();
    }

    StudentMark::create([
        'student_id' => the student id which i have to get from the table in the view,
        'subject_id' => $request->the subject id which i have to get from the table in the view,
        'mark_type_id' => $request->input('markType'),
        'mark' => $request->input('mark')]);

    $user = Auth::user();
    $subject = ClassSubject::where('teacher_id', $user->id)
        ->get()
        ->pluck('subject_id', 'id');


    return view('educator.account.marks', [
        'user' => Auth::user(),
        'marks' => StudentMark::whereIn('subject_id',$subject)
            ->orderBy('created_at', 'desc')
            ->get(),
    ]);

}

Form::open
向上移动,将其放在
@foreach
子句之后,但数据之前:

然后将布局更改为:

<td>
    {!! Form::label('classes_name', $classes->name) !!}
</td>
<td>
    {!! Form::label('student_number_in_class', $student->number_in_class) !!}
</td>
<td>
    {!! Form::label('student_full_name', $student->full_name) !!}
</td>
<td>
    {!! Form::label('subject', $subject) !!}
</td>

我还没有测试过,但应该可以。如果
标签不适合您,请尝试使用其他表单类型。

只需在保存结果的控制器方法上添加此查询即可

$studentid = Student::where('colname','=',colvalue)->select('id')->first();
$subjectid = Subject::where('colname','=',colvalue)->select('id')->first();

视图中传递给控制器的所有数据。使用该信息获取学生和科目的id。我无法为您提供确切的解决方案,因为我不知道表之间的关系或数据库中每个表的列名。

我已经用控制器编辑了我的帖子,在控制器中我实际获得了我需要的数据。也许现在我正确地理解了您。请阅读更新后的答案。这是一个好主意,但不幸的是,不使用标签和其他任何类型的表单看起来都不好。文本应该可以。如果它很难看,您可以添加
input type=“hidden”
。它不会显示,但信息肯定会出现在请求对象中。
colname
到底是什么anycol'是科目和学生的id。
StudentMark::create(['student\u id'=>学生id,'subject\u id'=>科目id,'mark\u type\u id'=>$request->input('markType'),'mark'=>$request->input('mark'))
实际上anycol是列名或您希望与模型进行比较以获取其id的列。这里是一个学生模型,包含一些列名和列值,作为输入传递,最后我们得到该学生的id。同样的主题。另外,我的坏colname是列值。是的,我知道,但是主题id和学生id不在输入字段中。我只是在foreach中循环它们,例如在我的视图中的表的一行中:
student\u id,subject\u id,mark的输入字段,marktype的输入字段
我不仅需要得到输入字段,还需要得到这一行中的subject\u id和student\u id。哦,我明白了。您的意思是说您有一些原始值不是输入,但仍然需要传递给控制器?
$request->input('student_number_in_class'),
$studentid = Student::where('colname','=',colvalue)->select('id')->first();
$subjectid = Subject::where('colname','=',colvalue)->select('id')->first();