This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Hi everyone,
hope someone can help me out with this question:
OctoberCms offers mutators (https://octobercms.com/docs/database/mutators) to manipulate DB field content view very conveniently. Like doing so:
<?php namespace Acme\Blog\Models;
use Model;
class User extends Model
{
public function getFirstNameAttribute($value)
{
return ucfirst($value);
}
}
But is there a way to return different values depending on weather this is called a) by a backend list view, b) a backend form view, c) a frontend form (using the php code section)?
something like this:
...
public function getFirstNameAttribute($value)
{
// if backend list view
return ucfirst($value);
// if backend form view
return strtolower($value);
// if frontend view
return strtoupper($value);
}
...
Last updated
The best approach here would be to use different mutator names for each instance and then have the list, form or frontend call the different name
Hello daftspunk, Thanky you very much for your reply and your hint....
The best approach here would be to use different mutator names for each instance and then have the list, form or frontend call the different name
.... well, now I have to find out how to let the list, form etc. call the mutators :) At the moment I placed the accessors/mutators in the respective model php of my plugin.
Well, meanwhile I solved it this way:
...
public function getFirstNameAttribute($value)
{
// if backend form view
if (Request::is('backend/*') && Request::is('*/update/*')) {
return strtolower($value);
}
// if backend list view
if (Request::is('backend/*')) {
return ucfirst($value);
}
// if it's neither form nor column, then it is frontend view
return strtoupper($value);
}
...
All better solutions are welcome :)
Thanks for the solution, I was in the same situation.
Your solution should perfectly work but if it's for a plugin, you should not hardcode the url. Your customers may have an url like "admin/" or "panel/". After some searches I found App::runningInBackend(); which do the trick.
I don't need to know for now in which controller I run, but if I had: Assuming OctoberCMS is probably using a CRUD structure for the resources in backend, you should be able to work around with Request::isMethod('post'). Even if thist part of the url should not change, I don't like to hardcorde anything in my backend.
In a future October release in ten years, this part of the url may have change, and your plugin won't work anymore.
Last updated
1-5 of 5