1

I'm just getting going with Laravel, and have used Eloquent to define my Campaign table. I have a Campaign model which is currently empty.

I'm not sure how to add attributes to this model to represent the fields in the db - or even if I should. The Laravel documentation seems thin on models and searches keep leading me to accessors and mutators.

If I have a database field called platform_type in my campaigns table, how do I link the PlatformType model attribute to this field?

To clarify:

This is not a question about relationships - there is only one entity in my solution thus far.

platform_type is a field in my campaigns table because it is an attribute of a campaign - I'm asking how to represent this in my model.

0

2 Answers 2

1

The model has an internal array which stores the attributes of a given row (it's called $attributes and replicated by $original if you look for them in the source code). The reason it's replicated is so when you call save() it will only do a save if you actually changed them from the originals.

You can access said attributes via $modelInstance->getAttribute("platform_type") or $modelInstance->platform_type which will call the magic __get method that in turn calls the getAttribute

So in your case you can have:

$campaign = Campaign::find($id);
echo $campaign->platform_type;

The ORM will automatically create the relevant SQL query and fill the model instance with the attributes of the row it finds.

Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, thanks. I've also been looking at Symfony and Doctrine, where the model definition is used to define the db table so all fields are controlled in the model. Feels very weird not having this, but I can see the utility.
Laravel has the schema builder and uses migrations which is a bit of a different approach. I guess if your tables are more fluid and change more frequently perhaps doctrine might be better.
0

You need to define relationships. In the PlatformType model:

public function campaigns()
{
    return $this->hasMany(Campaign::class, 'platform_type');
}

And in the Campaign model:

public function platformType()
{
    return $this->belongsTo(PlatformType::class, 'platform_type');
}

You also need to rename the campaign table to campaigns. Or you should add this to the model to be able to use a custom name:

protected $table = 'campaign';

At this point, these tables will be connected and relationships will work. However, it is recommended to add foreign key constraints.

2 Comments

Thanks @Alexey, but there is currently only one table & model and no requirement for relationships - platform_type is a field of campaigns (which does have an 's', it was my typo). My question is about representing the fields on the table in the model for that table entity.
@AdamHopkinson you don't need to do anything except adding $fillable array if you're planning to use the mass assignment feature

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.