列出表中的所有值并查询整数列的​​实际值并在视图中显示

时间:2019-02-21 17:58:43

标签: laravel laravel-5.7

我的表School中有三列:

id->int, name->string, status->int

我一直在使用dropdown中的值来填充status中的值,该数组是静态的,仅在view上使用:

{!! Form::select('status', ['1' => 'Active', '2' => 'In Active'], '1', ['class' => 'form-control','placeholder' => 'Please Select Dean']); !!}

现在我想在表中列出School中的值。

我一直在使用:

public function index()
{
    $title = 'School Info';
    $schoolList = School::all();

    return view('school.index',compact('schoolList','title'));
}

索引视图

 <td></td>
 <td>{!!$school->name!!}</td>
 <td>{!!$school->status!!}</td>

这将在status=1 or 2上返回view。但是,在表中我想通过查询静态变量数组来使用status = Active或In Active。

目前学校和地位之间没有关系。

如何查询状态并在表上显示实际状态值,而不是整数值?

2 个答案:

答案 0 :(得分:1)

由于刀片中有(int),因此您可以在刀片中进行类似的操作

// In Blade.....

<td>{!! $school->status == "1" ? "Active" : "In Active" !!)</td> 

答案 1 :(得分:0)

如果将状态值另存为1或0(如整数),则可以使用访问器:

enter image description here

在您的情况下,以School模型编写代码,

public function getStatusAttribute($value)
{
     if($value == 1){return 'Active';}
     if($value == 0){return 'InActive';}
     return $value; //if your status value is not 1 or 0 it will return the exact value of status
}

现在,如果您使用的话,

<td>{!!$school->status!!}</td> 

在刀片文件中,它将显示为活动或不活动