【问题标题】:Laravel - How to disable submit when any of the title field is nullLaravel - 当任何标题字段为空时如何禁用提交
【发布时间】:2020-05-08 01:37:02
【问题描述】:

在我的 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();

我如何做到这一点?

谢谢

【问题讨论】:

    标签: laravel


    【解决方案1】:

    你可以试试这样的。

    @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 收集方法显示链接。 documentation

    希望对你有所帮助。

    【讨论】:

    • 我想禁用它,但它隐藏了它。如何让它禁用它
    • @mikefolu how to disable a tag 并使用代码class="{{ $goals-&gt;pluck('title')-&gt;contains(null) ? 'not-active':'' }}"
    猜你喜欢
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多