Skip to content
Advertisement

Concatenate php in laravel

I am not very good at php. I have a function to check for an empty field and the correctness of email

if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format";
}

There is also a code in php.blade in which you need to insert this check

@php

$split = explode('@', $email);
$first = $split[0];
$second = $split[1];

@endphp
<a href="" data-first="{{ $first }}" data-second="{{ $second }}" class="js-combaine-email"></a>

How to do it correctly?

I just pasted into @php and it doesn’t work

@php

if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $emailErr = "Invalid email format";
}
 else {
$split = explode('@', $email);
$first = $split[0];
$second = $split[1];
}

@endphp
<a href="" data-first="{{ $first }}" data-second="{{ $second }}" class="js-combaine-email"></a>

Advertisement

Answer

Here is a solution where you dont show the link if there is no valid email

@if(!empty($email) && filter_var($email, FILTER_VALIDATE_EMAIL)) 
    @php
        $split = explode('@', $email);
        $first = $split[0];
        $second = $split[1];
    @endphp
<a href="" data-first="{{ $first }}" data-second="{{ $second }}" class="js-combaine-email"></a>
@endif

You can also add another @else before the @endif and show a message "no-email"

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