在 laravel 如何使用 2 个输入字段 "id" 和 "date of join" 在视图中搜索和显示学生数据

in laravel how to search and display student data in view by using 2 input filed "id" and and "date of join"

目前我创建了单个输入字段 "id" 如果输入有效 ID,它会显示学生(姓名、电子邮件、ID)。这是完美的工作。

现在我要添加一个输入字段 "date of join"

在这个函数中我要检查输入"id"和"date of join",是mach然后在视图中显示学生数据,否则它会显示 ("data not found") 消息

我的观点

<form action="/search" method="POST" role="search">
    {{ csrf_field() }}
    <div class="input-group">
        <input type="text" class="form-control" name="id"
            placeholder="Search users">

             <span class="input-group-btn">
            <button type="submit" class="btn btn-default">
                <span class="glyphicon glyphicon-search"></span>
            </button>
        </span>
    </div>

</form>

<div class="container">
    @if(isset($details))
        <p> The Search results for your query <b>  </b> are :</p>
    <h2>Sample User details</h2>
    <table class="table table-striped">
        <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>id</th>
                    <th>Photo</th>
                    <th>certificate_approval</th>
            </tr>
        </thead>
        <tbody>
            @foreach($details as $user)
            <tr>
                <td>{{$user->student_name}}</td>
                <td>{{$user->email}}</td>
       <div class="card-body">
    <img src="STUDENT_DATA/STUDENT_PHOTO/{{ $user->student_photo }}" class="img-circle" width="90" />


                                </div>


                                    <td>{{$user->student_registration_id}}</td>
                                     <td>{{$user->student_certificate_approval}}</td>
            </tr>
            @endforeach
        </tbody>
    </table>
    @elseif(isset($message))
    <p>{{$message}}</p>
    @endif
</div>

我的路线

 use App\Student;
 use Illuminate\Support\Facades\Input;
Route::any( '/search', function(){
    $id = Input::get( 'id' );
    if($id != ""){

$user = Student::where( 'student_registration_id', 'LIKE', '%' . $id . '%' )->get();

     if (count ( $user ) > 0)
     return view( 'my-search' )->withDetails( $user )->withQuery($id);
}

     return view ( 'my-search' )->withMessage ( "No Details found!" );
} );

我的模型

<?php

namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    protected $table = 'student_lists';


    protected $fillable = ['student_name', 'student_registration_id', 'date_of_join', 'student_phone_no', 'student_photo', ];





}

这是你想要的吗?

  $id = Input::get( 'id' );
                               $date_of_join = Input::get('date_of_join');
                                if($id != ""){

                                    $user = Student::where( 'student_registration_id', 'LIKE', '%' . $id . '%' );
                                    if (!empty($date_of_join))
                                    {
                                        $user->where('date_of_join','=',$date_of_join);
                                    }
                                    $user = $user->get();

                                    if (count ( $user ) > 0)
                                        return view( 'my-search' )->withDetails( $user )->withQuery($id)->withDateOfJoin($date_of_join);
                                    }

                                    return view ( 'my-search' )->withMessage ( "No Details found!" );
                                } );

您必须在查询中添加 where 条件才能搜索日期

Student::where( 'student_registration_id', 'LIKE', '%' . $id . '%' )->get();

在此查询中,您可以将其修改为:

$student = Student::where( 'student_registration_id', 'LIKE', '%' . $id . '%' );
if($dateOfJoin){
   $student->where('date_of_join', $dateOfJoin)
}
   $user = $student->get();

现在您可以将多个 where 或任何其他条件链接到查询。

此外,您可以使用@forelse 来显示空数据,例如

@forelse ($details as $detail)
<li>{{ $detail->name }}</li>
@empty
<p>No Details</p>
@endforelse

如你所说"input id and date of joining"。两者匹配,然后显示学生详细信息。

$user = Student::where( 'student_registration_id', 'LIKE', '%' . $id . '%' )
                ->where('date_of_join', 'LIKE', '%' . $date_of_join . '%')
                ->where('approved', 'APPROVE')
                ->get();

if(!empty($user)){
     return view( 'my-search' )->withDetails( $user )->withQuery($id);
} else {
     return view ( 'my-search' )->withMessage ( "No Details found!" );
}