How do I add a custom field to a model in Laravel?

ReactingToAngularVues

I'm very new to Laravel and php frameworks in general, so sorry if I miss anything basic. I've created a new model called Journey in my application which extends the standard Eloquent model. What I've noticed is that I'm using my Journey model in two different Controllers and I'm duplicating a bit of code because of it.

Essentially, what I'm doing is I'm taking the title of a Journey and I'm formatting it with a custom class to clean the title (convert to lowercase, add hyphens, remove whitespace, etc) so I can append it to my page URLs.

In one controller, I'm calling:

$journey = Journey::find($id);
$journey->cleanURL = Url::clean($journey['name']); // This creates a new element/property with a clean string

And in the other, I'm calling:

$journeys = Journey::all();
foreach ($journeys as $journey) {
    $journey->cleanURL = URL::clean($journey['name']);
}

It would be inappropriate to add a fixed field to my database with the cleaned URL because I may change the title (which the cleaned URL is based on) at any time and I'd like the URL to update automatically in this event. However, saying this, I'm repeating myself by calling Url::clean twice.

What I'd like to do is write a method or alter an existing method, so that when I call Journey::all() or Journey::find() or any query-based method, the URL field is already present and filled. I've tried looking through some of the Vendor/Eloquent files, but they just make me confused.

How would I go about doing this?

Marcin Nabiałek

You can use accessor for this.

Add to your Journey model the following function:

public function getCleanUrlAttribute($value)
{
    return Url::clean($this->name);
}

Now you will be able to use:

$journey = Journey::find($id);
echo $journey->clean_url;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I add custom field to Python log format string?

From Dev

How do I add a custom field to logstash/kibana?

From Dev

How do I add user defined custom fields to a model?

From Dev

How do I add a custom method to a backbone model?

From Dev

How do I add user defined custom fields to a model?

From Dev

Laravel: How do I create a custom pivot model?

From Dev

How do I get the current model from custom method in laravel

From Dev

Laravel how to add a custom function in an Eloquent model?

From Dev

How can I add a custom method to auth_user model class? I need to return a calculated field along with results

From Dev

How do I add a link to a Custom Boolean Field and pass parameters using Django_Tables2

From Dev

How do I add custom actions to a change model form in Django Admin?

From Dev

How do I add a field to a group?

From Dev

How do I migrate data (i.e. specific model field values) from a Mezzanine Displayable model to a new custom Mezzanine Page model?

From Dev

How do I call a model in Laravel 5?

From Dev

How do I add custom fonts?

From Dev

How do I add a custom color to this script?

From Dev

How do I add a custom plugin to Nagios?

From Dev

How do I add custom fonts?

From Dev

How do I add a class to a custom namespace?

From Dev

How do I add a custom launcher?

From Dev

How can I add a field in a model in odoo 9 new API?

From Dev

How do you add custom field to woocommerce product attribute

From Dev

How to add a custom function/method in sqlalchemy model to do CRUD operations?

From Dev

How do you add a custom uniqueness validation to an ActiveRecord model in Rails?

From Dev

How do I add a Laravel command in a subfolder?

From Dev

How do I add images in laravel view?

From Dev

How do I easily create a calculated field in a Django model?

From Dev

How do I filter a model field on a form that is a foreignkey but not the main foreignkey

From Dev

How do I make a custom model Field call to_python when the field is accessed immediately after initialization (not loaded from DB) in Django >=1.10?

Related Related

  1. 1

    How do I add custom field to Python log format string?

  2. 2

    How do I add a custom field to logstash/kibana?

  3. 3

    How do I add user defined custom fields to a model?

  4. 4

    How do I add a custom method to a backbone model?

  5. 5

    How do I add user defined custom fields to a model?

  6. 6

    Laravel: How do I create a custom pivot model?

  7. 7

    How do I get the current model from custom method in laravel

  8. 8

    Laravel how to add a custom function in an Eloquent model?

  9. 9

    How can I add a custom method to auth_user model class? I need to return a calculated field along with results

  10. 10

    How do I add a link to a Custom Boolean Field and pass parameters using Django_Tables2

  11. 11

    How do I add custom actions to a change model form in Django Admin?

  12. 12

    How do I add a field to a group?

  13. 13

    How do I migrate data (i.e. specific model field values) from a Mezzanine Displayable model to a new custom Mezzanine Page model?

  14. 14

    How do I call a model in Laravel 5?

  15. 15

    How do I add custom fonts?

  16. 16

    How do I add a custom color to this script?

  17. 17

    How do I add a custom plugin to Nagios?

  18. 18

    How do I add custom fonts?

  19. 19

    How do I add a class to a custom namespace?

  20. 20

    How do I add a custom launcher?

  21. 21

    How can I add a field in a model in odoo 9 new API?

  22. 22

    How do you add custom field to woocommerce product attribute

  23. 23

    How to add a custom function/method in sqlalchemy model to do CRUD operations?

  24. 24

    How do you add a custom uniqueness validation to an ActiveRecord model in Rails?

  25. 25

    How do I add a Laravel command in a subfolder?

  26. 26

    How do I add images in laravel view?

  27. 27

    How do I easily create a calculated field in a Django model?

  28. 28

    How do I filter a model field on a form that is a foreignkey but not the main foreignkey

  29. 29

    How do I make a custom model Field call to_python when the field is accessed immediately after initialization (not loaded from DB) in Django >=1.10?

HotTag

Archive