Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Davieboy's avatar

Controller, Models, Views & Functions

I'm looking for a bit of help or guidance here on how this should be done rather than spending hours throwing mud at the wall until something sticks.

I'm trying to learn Laravel while porting my existing PHP project over and there's a few hurdles i'm struggling with, mainly passing the data retrieved back to another function.

I currently have postal addresses in a database table with 7 fields for each line of the address.

The controller returns the correct data to the view, but some addresses have empty lines due to not having any info in that section, I'm guessing i would use the modal to do the actual PHP donkey work but i'm struggling to understand how i do this efficiently, my plan was to create a simple function with the code below in it

$fadd1 = (empty($add1) ? '' : $add1."<br />");
$fadd2 = (empty($add2) ? '' : $add2."<br />");
$fadd3 = (empty($add3) ? '' : $add3."<br />");
$fadd4 = (empty($add4) ? '' : $add4."<br />");
$ftown = (empty($town) ? '' : $town."<br />");
$fcounty = (empty($county) ? '' : $county."<br />");
$fpostcode = (empty($postcode) ? '' : $postcode);

Then i could reuse on other sections of the site but its the whole getting it from the view then sending it to the function then back to the view again in the desired format.

Any help would be appreciated.

0 likes
3 replies
fylzero's avatar

@davieboy Just a suggestion... you can use an Elvis operator to clean this up a bit. I would just use the tags when you use the variable instead of concat'ing them. Also, you no longer need to self-close br tags in html5.

// Elvis operator ?: checks if there is a value...
// If so, sets to itself...
// Otherwise...  empty string.
$fadd1 = ($add1) ?: '';
{{ $fadd1 }}<br>
24 likes
ahmeddabak's avatar

the easy way would be to create a global function called format_address() that take the data and return a formatted address, you can create a helpers file and use composer autoload to load it, if this is to hard for you, just put it on the top of your route file, though this is really a bad practice.

after creating the function you can call it in your front end.

{{format_address($var1, $var2);

Another solution if you are using Elequent and you have a model you can create an Accessor and then use in in your view as an attribute

class YourClassName extends Model {

public function getFormattedAddressAttribute() {
    // do the logic here
    //$this->add1;
}

and in your view

{{$model->formated_address}}

both ways you will have a code that is reusable and callable from the view

Davieboy's avatar

Thanks for your replies and advice on this, I think the global would be worth the effort , although it wont be used very often it would be worth having for when it is needed.

TBH im just a bit frustrated with it all ATM and cant get any of it working as expected, I'm going to finish the laravel 6 series then come back to it with a fresh set of eyes.

I did find a temp solution in the meantime though, its a bit repetitive and not the cleanest but i can live with it for now until i figure out how to do it properly

@empty($business->add2)
    {{ $business->add2 }}
@else
    {{ $business->add2 }}<br>
@endempty

Thanks for the help.

Please or to participate in this conversation.