I would like to concatenate two columns in my list view.
I want to have "firstname name".
So i did an accesor in my model:
public function getFullNomAttribute() {
return $this->firstname . ' ' . $this->name;
}
And in my controller:
$this->crud->addColumn([
'label' => "concatened_name",
'type' => "model_function",
'function_name' => 'getFullNomAttribute',
]);
It's working as long as i have the two columns defined with addColumn in my controller:
$this->crud->addColumn([
'name' => 'name',
'label' => "name",
'type' => 'text'
]);
$this->crud->addColumn([
'name' => 'firstname',
'label' => "firstname",
'type' => 'text'
]);
But i end up with three columns: firstname, name and concatened_name
And if i remove the two addColumn my accesor is not working anymore...
I want just concatened_name in my list.
Any idea ? Thanks!
Make sure you're NOT using $this->crud->setFromDb();
If that does not fix it, maybe try something like
Your Model
public function getFullNomAttribute()
{
return $this->concatenateNom();
}
private function concatenateNom()
{
return trim($this->['attributes]['firstname'] . ' ' . $this->attributes['name']);
}
Your Controller
$this->crud->addColumn([
'label' => "Full Nom",
'type' => "model_function",
'function_name' => 'concatenateNom',
]);
Then make sure there is no "name" or "fullname" columns defined?
I'm not using $this->crud->setFromDb();
In your code above i guess i have to call getFullNomAttribute and not concatenateNom in my controller right ?
Are you sure about the syntax for $this->attributes['name']?
I have an "Undefined index: name" error.
In my model the fields are in protected $fillable
I'll keep trying.
I've tried everything.
With $this->attributes['name'] instead of $this->name and with a second function like you told me.
Each time I have an ErrorException in my model : Undefined index
But if i add the columns with addColumn in my controller it's working...
Looks like if the columns are not 'declared' with addColumn CRUD does not know it.
I think it's best to post your whole Model, whole Controller and the database schema for your user.
It is much easier to understand by seeing it than trying to explain
I think it's the same bug as here:
https://github.com/Laravel-Backpack/CRUD/issues/306
model_function Column does not work with enableAjaxTable...
My workaround idea is to make my own column types/blade.
But the $entry variable only contains fields declared with addColumn in the controller. So i in my case i need to have name and firstname. And i end-up displaying columns i don't want in the list.
Is there a way to get database fields value but not displaying it in the list? Maybe a "hidden" Column Types?
So they are available in $entry and we can work with this in blade files.
OK here is my "ugly" workaround. It may help others.
I've created a custom empty columns type : hidden.blade.php, displaying nothing.
In my controller i can declare:
$this->crud->addColumn([
'name' => 'name',
'label' => "Name",
'type' => 'hidden'
]);
$this->crud->addColumn([
'name' => 'firstname',
'label' => "FirstName",
'type' => 'hidden'
]);
Then I have another custom columns type displaying name and firstname:
echo $entry->name . " " . $entry->firstname;
^
Sorry the solution above is not working as i still have the columns names displayed even with my "hidden" custom fields types.
Sorry for jumping in. If you already have the accessor, this should work for you:
$this->crud->addColumn([
'name' => 'full_nom',
'label' => "name",
'type' => 'text'
]);
Eloquent makes this process invisible because of the mutator. It's just like you would have that column in the db.
Hope it helps. Cheers!
Thanks for the help but it's not working.
In my model:
public function getFullNameAttribute() {
return $this->firstname . ' ' . $this->name;
}
In my controller:
$this->crud->addColumn([
'name' => 'full_name',
'label' => "name",
'type' => 'text'
]);
--> SQLSTATE[42S22]: Column not found: 1054 Unknown column 'full_name' in 'field list'
I also try adding in the model:
protected $appends = ['full_name'];
Still not working
@tabacitu, it works when i disable enableAjaxTable()
With $this->crud->enableAjaxTable(); i have the error ...
_SQLSTATE[42S22]: Column not found: 1054 Unknown column 'full_name' in 'field list'_
But i really need to enable ajax as my table is huge.
Use the below thread
Accessor or model_function still not working with enableAjaxTable()...
This is really blocking for me. Any news on that?
Here's what you can do to make your attribute show when enableAjaxTable() is present on your CRUD:
public function getFullnameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
public function showFullname()
{
return $this->fresh()->fullname; // make sure you call fresh instance or you'll get an error that fullname is not found or something like that...
}
$this->crud->addColumn([
'label' => 'Full name',
'type' => 'model_function',
'function_name' => 'showFullname'
]);
That's it
It works!!
Thank you @viktorivanov
This is amazing. Thank you @viktorivanov !!
I find myself having to give the column a real name attribute. Otherwise my query adds select "" as "". I use a random other column which I don't need for the table in order to make this work. For example, I don't use the referrer column in my table, so I can use it for my "phone" column (which is a function-derived column):
[
'type' => 'model_function',
'label' => 'Phone',
'function_name' => 'showPhoneColumn',
'name' => 'referrer'
],
@viktorivanov did you get it working without including the name attribute?
@jsiebach I've not encountered such a behavior :confused:
Please check You are using accessor for the phone field
Most helpful comment
Here's what you can do to make your attribute show when enableAjaxTable() is present on your CRUD:
That's it