javascriptphplaravelif-statementlaravel-blade

Using Javascript inside blade @if


I was wondering. It's not possible to put Javascript inside blade @if right? Is blade @if some form of php but using php code will be discouraged in view right? What should I do? Here's the logic of what I'm trying to achieve. Basically, what alternative ways are there to achieve what I'm trying to do? I'm trying to show button when the three conditions are met.

@if(isToday($one_parking_info->booking_data->m_bkl_end_date) && isBeforeElevenPM() && notEndInT($one_parking_info->booking_data->m_plots_address_city))
    <button type="button" class="btn btn-primary btn-sm" onClick="cancelBooking('{{ $one_parking_info->booking_data->m_bkl_gov_id }}')">取消</button>
@endif

Solution

  • Blade is a form of php, so all work is done on the server. So you cannot call javascript functions inside @if expression. Instead you can run the functions in the controller and return a value to say if you should include the button or not or you can have a javascript to show or hide the button if you don't care if the users can find and show the button if they want.

    Something like in your controller return view('your view')->with(['showButton' => $shouldShow) and in your view @if($shouldShow) and the rest of your code