Laravel - 如何在任何标题字段为空时禁用提交

迈克福卢

在我的 Laravel-5.8 中,我有一个名为 Goal 的模型:

protected $fillable = [
              'id',
              'weighted_score',
              'title',
              'start_date',
              'end_date',
          ];

对于模型,我有一个控制器:

索引控制器

public function index()
{
    $userEmployee = Auth::user()->employee_id;
    $goals = Goal::where('employee_id', $userEmployee)->get();

    return view('goals.index')->with(['goals', $goals);
}

此控制器呈现此视图

看法

<div class="card-body">
    <div class="table-responsive">
        <table class=" table table-bordered table-striped table-hover datatable">
            <thead>
                <tr>
                    <th width="8%">
                        Type
                    </th>
                    <th width="11%">
                        Start Date - End Date
                    </th>
                    <th>
                        Weight(%)
                    </th>  
                    <th>
                        &nbsp;
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach($goals as $key => $goal)
                        <td>
                            {{$goal->title ?? '' }}
                        </td>                             
                        <td>                               
                            {{Carbon\Carbon::parse($goal->start_date)->format('M d, Y') ?? '' }} - {{Carbon\Carbon::parse($goal->end_date)->format('M d, Y') ?? '' }}
                        </td>  
                        <td>
                            {{$goal->weighted_score ?? '' }}
                        </td>                            
                </tr>
                @endforeach 
            </tbody>
        </table>
    </div>
    <div class="row no-print">
        <div class="col-12">
                <a href ="{{ route('post.publish.all')}}" class="btn btn-primary float-left"><i class="fas fa-arrow-right"></i> Submit</a>               
        </div>       
    </div>             

    </div>

路线:

Route::resource('goals', 'GoalsController');  

Route::get('post/publish_all_posts', 'GoalsController@publish_all_posts')->name('post.publish.all');

publish_all_posts() 的控制器函数

public function publish_all_posts(){

    $unapproved_post = Goal::where('employee_id', $userEmployee)
            ->update([
                'is_approved' => 1                   
                ]);

}

为此,显示该特定员工的所有目标:

$goals = Goal::where('employee_id', $userEmployee)->get();

我希望应用程序仅启用此功能:

        <div class="col-12">
                <a href ="{{ route('post.publish.all')}}" class="btn btn-primary float-left"><i class="fas fa-arrow-right"></i> Submit</a>               
        </div>       

仅当名为 title 的字段中没有 NULL 值时

$goals = Goal::where('employee_id', $userEmployee)->get();

我如何实现这一目标?

谢谢

惠斯诺萨

你可以尝试这样的事情。

@if(! $goals->pluck('title')->contains(null))
<div class="col-12">
  <a href ="{{ route('post.publish.all')}}" class="btn btn-primary float-left"><i class="fas fa-arrow-right"></i> Submit</a>               
</div>
@endif 

当目标标题不包含 null 时,使用 laravel 收集方法显示链接。文件

希望对你有帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章