0

In my Laravel-5.8, I have a model called Goal:

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

For the model, I have a controller:

Index Controller

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

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

This controller renders this view

view

<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>

routes:

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

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

Controller function for publish_all_posts()

public function publish_all_posts(){

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

}

For this that displays all the goals for this particular employee:

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

I want the application to only enable this:

        <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>       

only when there is no NULL value in the field called title

in

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

How do I achieve this?

Thanks

mikefolu
  • 1,203
  • 6
  • 24
  • 57

1 Answers1

1

You could try something like this.

@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 

when goals titles not contains null show the link using laravel collection methods. documentation

Hope it helps you.

wheesnoza
  • 201
  • 1
  • 5
  • I want to disable it, but it hides it. How do I make it to disable it – mikefolu May 08 '20 at 09:05
  • @mikefolu [how to disable a tag](https://stackoverflow.com/a/4416239/13468924) and use the code `class="{{ $goals->pluck('title')->contains(null) ? 'not-active':'' }}"` – wheesnoza May 08 '20 at 09:52