Support the ongoing development of Laravel.io →
Database Eloquent Views
Last updated 1 year ago.
0

why not? Just don't forget to pass model member in view.

Last updated 1 year ago.
0

But what would i put in my get_url() method? How can I get what model instance it is?

Last updated 1 year ago.
0

In your Eloquent model add the following variable to the top. This will add a new field to your model.

protected $appends = ['url'];

Continue to add a new accessor method to the same model

public function getUrlAttribute()
{
	return URL::to('profile/' . $this->id);
}

This is called an accessor. Read more about what accessors are here: http://laravel.com/docs/eloquent#accessors-and-mutators

You can use it by passing the model data to the view and calling the new field.

<a href="{{ $user->url }}">{{ $user->name }}</a>

I hope this helped you.

Last updated 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.