Set Attribute With Same Name As Databse Field in Laravel returns Error

Set Attribute With Same Name As Databse Field in Laravel returns Error

Related searches

I have a problem in my Project. I have a field Name contact_number where I stored json data in this format {"code":"","number":""}. I want to set Two Attribute name getCountryCodeAttribute and getContactNumberAttribute so that I can access code and number individually. But it Throws error

Undefined property: App\Models\Profile::$contact_number

My Model Code Is

?php

namespace App\Models;

class Profile extends Model
{
    use SoftDeletes;
    protected $guarded = [];

    protected $casts = [
        'contact_number' => 'array',
    ];

    public function getCountryCodeAttribute()
    {
        $contact_number = $this->contact_number;
        return $contact_number['code'];
    }

    public function getContactNumberAttribute()
    {
        $contact_number = $this->contact_number;
        return $contact_number['number'];
    }

}

I don't know how to solve this. Thanks in advance for your help


Within the accessor you can not access the property. So it's Undefined

    public function getContactNumberAttribute()
    {
        $contact_number = $this->contact_number;//$this->contact_number is not valid
        return $contact_number['number'];
    }

change ContactNumber part to something else like PhoneNumber

    public function getCountryCodeAttribute()
    {
        $contact_number = $this->contact_number;

        return $contact_number['code'];
    }

    public function getPhoneNumberAttribute()
    {
        $contact_number = $this->contact_number;

        return $contact_number['number'];
    }

and then access it like this.

    dd(User::find(1)->phone_number);
    dd(User::find(1)->country_code);

An Eloquent model attribute or relation with a name that clashes with , Laravel Version: 5.7.15 PHP Version: 7.2 Short Description: If you a name that clashes with a protected property should throw an error or warning #26730 changes() { return $this->hasMany(Change::class, 'submission_id'); } } Any model with a database column called "status" would have a problem. Is there a way to map field names to a database to a different attribute name in the model? For example, if the database has a field name of customer_id but I wanted to use eloquent in this way Customer::get(['id']) I've tried using the getAttribute method but that is called after eloquent has attempted to get the value.


  1. You already have a field named contact_number, but you want to defined a attribute contract_number, this is a mistake, try to use another name.
  2. Your contract_number is an json format. After you take it out from database, it need to be decoded, or it will be string, so you cannot use $contact_number['number'] too.
public function getProfileContractCodeAttribute()
{
    $contact_number = json_decode($this->contact_number, true);
    return $contact_number['code'];
}

public function getProfileContractNumberAttribute()
{
    $contact_number = json_decode($this->contact_number, true);
    return $contact_number['number'];
}

so you can get attribute like this:

Profile::find(1)->profile_contract_number;

Using attribute name in function with same name � Issue #484 , Hey Jens, I got a model that has the attribute "features" in it's mongo document. If I now use $this->features inside this function I receive the following error I figured that it might be related to the magic function calling from Laravel. but on \Illuminate\Database\Eloquent\Model.php getAttribute() it will� If you would like the :attribute portion of your validation message to be replaced with a custom attribute name, you may specify the custom names by overriding the attributes method. This method should return an array of attribute / name pairs:


try it with json_decode

public function getCountryCodeAttribute()
{
    $contact_number = json_decode($this->contact_number);
    return $contact_number['code'];
}

public function getContactNumberAttribute()
{
    $contact_number = json_decode($this->contact_number);
    return $contact_number['number'];
}

Eloquent: Mutators - Laravel, <?php namespace App; use Illuminate\Database\Eloquent\Model; class User You may also use accessors to return new, computed values from existing attributes: The mutator will receive the value that is being set on the attribute, allowing function to the name and set its resulting value in the internal $ attributes array. Laravel Validation mechanism has a lot of rules provided - a field can be required, integer, IP address, timezone etc. But sometimes there is a need for a special rule which is not in that list. One example of this is when you have two fields and you need only one of them to be filled. One, or another, but NOT BOTH. Laravel doesn't have a rule for that, so let's create one!


Validation - Laravel, Creating Form Requests; Authorizing Form Requests; Customizing The Error a new blog post, while the POST route will store the new blog post in the database. Creating The Controller. Next, let's take a look at a simple controller that handles these routes. This method should return an array of attribute / name pairs: Set up the env file. Now that we have a specific database we want to use for our project, we can set the variables we need in the .env file. By default, the DB_DATABASE key is set to homestead, but we will change it to 54 for this example. We leave the DB_USERNAME and DB_PASSWORD fields at their defaults.


Be Careful to Use Laravel Eloquent's Accessors, Mutators And , Laravel Elqouent provides some convenient ways to manage the models. Field | Type | Someday, full name of each user should be returned and we decide not to modify for the column, fullName which not really exists in the database. It shows that every time we retrieve the model attribute, fullName� If a matching model instance is not found in the database, a 404 HTTP response will automatically be generated. Customizing The Key Name. If you would like model binding to use a database column other than id when retrieving a given model class, you may override the getRouteKeyName method on the Eloquent model: /** * Get the route key for the


In this post, i will let you know how to get table name from model in Laravel. According to Laravel standard structure, We do not need to tell Eloquent which table will be used for model because by convention, model is used to interact with the table and plural name of model class will be used as table name.