I have a task - insert a custom column in one of our admin pages, where I can to call method from Model for each entry, generated by backpack CRUD generator.
I have found in official documentations statement that looks like what I need here:
https://laravel-backpack.readme.io/v3.0/docs/crud-columns-types#section-roll-your-own
But there is nothing about how to implement this in the controller right way.
So I have tried to do just like this
Controller:
$status = [
'label' => 'Status',
'name' => 'status',
'type' => 'text'
];
$this->crud->addColumn($status);
and as mentinoned in documentation, I have create my own blade file in
resources\views\vendor\backpack\crud\columns
Here it is:
{{-- status --}}
<td>{{ $entry->isBlocked }}</td>
Where isBlocked is method in my Model. I have an error about database and nothing is working.
Please say is it even possible to do what I wnat and if it is - please say how to do it right way both in view and controller
Thankyou!
Let's check your code
$status = [
'label' => 'Status',
'name' => 'isBlocked', // your column name
'type' => 'status' // your blade name, e.g status.blade.php
];
$this->crud->addColumn($status);
and inside status.blade.php
{{-- status --}}
<td>{{ $entry->{$column['name'] }}</td>
Any question, please comment
Related
When I return from a function with a view and a compact table:
$table = (new \Okipa\LaravelTable\Table)->model(Expense::class)->routes([
'index' => ['name' => 'admin.expenses.index'],
'edit' => ['name' => 'admin.expenses.edit'],
'destroy' => ['name' => 'admin.expenses.destroy'],
])->tbodyTemplate('bootstrap.expenses.tbody')
->query(function($query) use($user){
$query->select("expenses.*");
// $query->join('projects',"projects.id","=","expenses.project_id");
// $query->addSelect('projects.name as project');
$query->where('user_id', $user->id)->whereNull('travel_id');
});
return view('backend.expenses.index', compact('table'));
Laravel displays this error:
Return value of Okipa\LaravelTable\Table::render() must be of the type string, object returned (View: /home/armand/Desktop/GSurvey/expenses/resources/views/vendor/laravel-table/bootstrap/table.blade.php)
The index blade is where it supposed to be and in the view I have just a simple line to display the table: {{ $table }}
Can anyone extend a helping hand, maybe I am missing something?
Thank you in advance,
Kind regards,
Armand Camner
Lets say i'm building the following form with symfony form builder:
$builder->add('version', ChoiceType::class, [
'choices' => [
'Wheezy' => 7,
'Jessie' => 8,
'Stretch' => 9
]
])
Later i need to access it from a twig template for instance to display a table:
...
<td>{{ entity.version }}</td>
<td>{{ entity.foo }}</td>
<td>{{ entity.bar }}</td>
...
If i do this, i will end up with a version equal to 7, 8, or 9, and i don't want to do the following which will obviously defeat it's purpose:
$builder->add('version', ChoiceType::class, [
'choices' => [
'Wheezy' => "Wheezy",
'Jessie' => "Jessie",
'Stretch' => "Stretch"
]
])
How can i do this without having to map it inside my template ? i also really want to avoid doing a whole entity, would be an overkill for so few entries. I'm pretty sure or at least i hope that there is something to handle this case scenario already bundled with symfony, thanks.
You either need to go with this, but it will use more space in your database:
$builder->add('version', ChoiceType::class, [
'choices' => [
'Wheezy' => "Wheezy",
'Jessie' => "Jessie",
'Stretch' => "Stretch"
]
]);
Or handle it in your entity, for example:
class Entity {
const VERSIONS = [
'Wheezy' => 7,
'Jessie' => 8,
'Stretch' => 9
];
// code
public function getVersion($string = false) {
if ($string && \in_array($this->version, self::VERSIONS))
return \array_search($this->version, self::VERSIONS);
return $this->version;
}
}
In your form builder you just need to set the choices to the Entity list of versions.
$builder->add('version', ChoiceType::class, [
'choices' => Entity::VERSIONS
]);
And finally set your getter $string value to true in your template
...
<td>{{ entity.version(true) }}</td>
<td>{{ entity.foo }}</td>
<td>{{ entity.bar }}</td>
...
By default, the behavior of the getter getVersion will act as normal, if you set the $string boolean parameter to true, it will render the value as a string
Edit:
You didn't add any information about PHP version, so I assumed that you use at least version 7.0, thus the return type declaration. Also note that you need at least PHP 5.6 to use array as a constant's value.
652/5000
I am using in my project of Laravel 5 the package https://github.com/spatie/laravel-activitylog in its version 2.3. I have a page where I go through a list of activities and try to get the changes but it returns an empty array.
This is my Controller
public function history($id) {
$incidence = Incidence::find($id);
$activities = Activit::where('subject_id', $incidence->id)->get ();
return view('incidence.history', compact('activities'));
}
This is my html page
#foreach ($activities as $activity)
<p> {{ $activity->created_at }} </p>
<p> {{ $activity->changes() }} </p>
#endforeach
And this is the output in the browser
Clarify that I have done 4 update to the same record which I see reflected in the activity_log table of the database that uses the package. But I do not understand why the arrangement of the changes is not shown as indicated by the site's documentation:
Calling $ activity-> changes will return this array:
[
'attributes' => [
'name' => 'updated name',
'text' => 'Lorum',
],
'old' => [
'name' => 'original name',
'text' => 'Lorum',
],
];
Since it uses a database table I'm certain this has to do with the way Laravel allows methods to be chained. When you do $activity->changes() it expects you to continue the query. IE: $activity->changes()->where('etc', 1)->get() or something similar. So by calling $activity->changes() you are just sending a partial query to eloquent.
Is it possible to use a variable as the call to a Laravel Blade directive?
For my menu system, I've defined a component and I would like to be able to set the visibility of a link in the component. I have created numerous Blade directives (staff, admin, client, etc.) which check a user's role.
My component definition looks like this:
#component('components.primaryMenu', [
'items' => [
[
'route' => route('some.uri'),
'visibility' => ['staff', 'client'],
'label' => 'Item 1',
],
[
'route' => route('another.uri'),
'visibility' => ['everyone'],
'label' => 'Item 2',
],
]
])#endcomponent
What I would like to do is:
<ul class="nav">
#foreach($items as $item)
#foreach($item['visibility'] as $visibility)
#{{ $visibility }} // Should interpolate to #staff / #client
// Link stuff in here
#end{{ $visibility }} // Should interpolate to #endstaff / #endclient
#endforeach
#endforeach
</ul>
When I run this code I get "Invalid argument supplied for foreach()". I'm guessing because the #{{ $visibility }} declarations are throwing off the parser.
My Blade directives are defined in a service provider and look like this:
Blade::if('staff', function () use ($user) {
return $user->isType('staff');
});
Blade::if('client', function () use ($user) {
return $user->isType('client');
});
I want to pass some user data to a view so it can be displayed in the profile page. There is quite a lot of it but I don't want to just pass everything, because there are some things the view shouldn't have access to. So my code looks like this:
return view('profile', [
'username' => Auth::user()->username,
'email' => Auth::user()->email,
'firstname' => Auth::user()->firstname,
'country' => Auth::user()->country,
'city' => Auth::user()->city->name,
'sex' => Auth::user()->sex,
'orientation' => Auth::user()->orientation,
'age' => Auth::user()->age,
'children' => Auth::user()->children,
'drinking' => Auth::user()->drinking,
'smoking' => Auth::user()->smoking,
'living' => Auth::user()->living,
'about' => Auth::user()->about,
]);
My question is: Can this be written shorter/simpler?
Thanks!
EDIT:
I don't want this: {{ Auth::user()->firstname }} because there is a logic in a view, which is bad - I think, there should be just plain variables to be displayed, in view, not anything else.
So I'm looking for something like:
return view('profile', Auth::user()->only(['firstname', 'email', ...]));
You could create by yourself a method named like getPublicData and then return all those properties you need.
...
public function getPublicData() {
return [
'property_name' => $this->property_name
];
}
...
... and then use it in your controller/views. Maybe it's not an optimal solution, but you can isolate this thing in the model and avoid too much code in the controller.
Another advanced approach could be the override of the __get method. However, I am not the first in this case.
Hope it helps!
You don't need to pass these variable to the view, you can directly access them in the view using blade
<h1>{{ Auth::user()->username }}</h1>