Skip to content
Advertisement

how to return a button using controller response to html

in my laravel application i have defined some rules which is returned by controller here it is code

$is_order_exist =  Order::where([
            'customer_id' => $customer_id,
            'is_confirmed_admin' => null,
        ])->first();
    if($is_order_exist){
        return 'You've already 1 order exist, first it will approve then you can proceed anymore, Thank you! ';
    }

i wnat in return it should also return a button <a href="{{ route('edit.order' , $order->id) }}" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i>Edit Order</a> how i can return that button using controoller?

Advertisement

Answer

The simple solution would be to save the button HTML and message to a variable, and return it:

$message = 'You've already 1 order exist, first it will approve then you can proceed anymore, Thank you! '
$button = '<a href="{{ route('edit.order' , $order->id) }}" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i>Edit Order</a>';

return [
    'button' => $button,
    'message' => $message,
]

Then you can use your return values wherever you need. But, this is not ideal solution, as it breaks the purpose of MVC. Better solution would be to have button prepared in your layout, and display it on certain condition. For examle:

$data['message'] = 'You've already 1 order exist, first it will approve then you can proceed anymore, Thank you!'
$data['showButton'] = $is_order_exist //Will return true/false

return $data;

And then, in your blade, wrap your button in if condition:

@if($showButton)
   <a href="{{ route('edit.order' , $order->id) }}" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i>Edit Order</a>
@endif

Note: Haven’t tested any of these solutions, so if something breaks, let me know.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement