I am trying to use if/else on a submit button given that if an employee time in is null a Sign In button will be shown otherwise Sign Out button is displayed. Below is my set_attendance form. The if/else does not work as expected. Could you please help how to make it work.
<table >
<thead>
<tr>
<th> {{ __(' SL') }}</th>
<th> {{ __(' Employee Name') }}</th>
<th> {{ __('Action') }}</th>
</tr>
</thead>
<tbody>
@foreach($attendances as $key => $attd)
<tr>
<td>{{$key 1}}</td>
<td>{{ $attd['name'] }}</td>
<td>
<form action="{{ route('attendance.timeclock') }}" method="post" name="department_add_form">
{{ csrf_field() }}
<input type="hidden" name="id" id="id" value="{{ $attd['id'] }}">
@if($timein[1]['timein'] == Null)
<button type="submit" ><i ></i> {{ __('Sign IN') }}</button>
@else
<button type="submit" ><i ></i> {{ __('Sign Out') }}</button>
@endif
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
Below is my timeclockcreate method in a controller
public function timeclockcreate(Request $request){
$id = $request->input('id');
$data['attendances'] = User::all()->toArray();
$data['timein'] = User::query()
->leftjoin('attendances', 'attendances.user_id', '=', 'users.id')
->select('attendances.*', 'users.*')
->orderBy('users.name', 'ASC')
->get()
->toArray();
return view('fms.attendances.set_attendance',$data)->with('id', $id)->with('message', 'Inserted successfully.');
}
My attendance table
public function up()
{
Schema::create('attendances', function (Blueprint $table) {
$table->increments('id');
$table->integer('created_by');
$table->integer('user_id')->unsigned();
$table->time('timein')->nullable();
$table->time('timeout')->nullable();
$table->date('logdate');
$table->string('status');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
CodePudding user response:
It looks like you are accessing the same 'timein' for each employee with
$timein[1]['timein']
I'm guessing this is why it's not working.
You can search through the timein array to find the right timein row for the user you are currently looking at.
But instead you should set up an eloquent relationship with models that way you dont need to use joins. It simplifies queries a lot. https://laravel.com/docs/9.x/eloquent-relationships. Hope that makes sense.
CodePudding user response:
@if( $attd['timein'] === NULL )