you could have a partial view with the common input for create and edit
you could also do it dynamically.. have an array for each form/model then loop through it depending on what model is being loaded.. this would entail some thing (one option) like: ursite.com/users now you know user model is being called so you get the array of users and loop through it to display form fields...
$forms = [
'users' = [
'first_name' => [
type => text,
label => 'First Name'....
so on... you could also do it instead by querying users table.. iterating over each object and instead of displaying value get the key... ie you want the $user->first_name part rather than the actual value...
I suppose that could work.
I have about 22 models that only shares one field in common ("name"), with about 16 sharing two ("code" and "name), and 10 sharing three ("code", "name", "description).
Obviously, creating views for all of them using data table indexes, create/edit etc. would be quite a bit of work.
Would it be better to have some kind of "group" repository or something so then all of those models that only needs to be listed/edited/created within a single area?
Going off what moinescumihai said, in your form, you can just include partials:
@include('partials.name')
@include('partials.code')
@include('partials.description')
Inside the code and description partials, you can check if the model has that field before displaying that field.
@if($model->getAttribute('code') !== null)
// Display input
@endif
Going off what shez1983 said, you can loop through the fillable attributes and display an input field for that:
@foreach ($model->getFillable() as $field)
<input type="text" name="{{ $field }}">
@endforeach
thomastkim said:
Going off what moinescumihai said, in your form, you can just include partials:
...
Inside the code and description partials, you can check if the model has that field before displaying that field.
...
Going off what shez1983 said, you can loop through the fillable attributes and display an input field for that:
...
Seems good and all. Is there a way thought generate CRUD buttons without hard-coding link_to_route('admin.members.create', ...)
? Would using Route::currentRouteName()
and using that to swap the last element (.index
, .show
, etc.) with the appropriate action work?
Edit: Or getting the controller itself and using action()
to generate the link for the action needed?
Hmm...that's a good question. First, are you using Laravel 4? I don't think link_to_route
exists anymore in 5.
From what you listed, it looks like the controller method is simpler. Maybe someone else can chime in on a better method if one exists though.
thomastkim said:
Hmm...that's a good question. First, are you using Laravel 4? I don't think
link_to_route
exists anymore in 5.
Laravel 5.1 with Laravel Collective's HtmlServiceProvider.
I built a small system to do just this, at some point I'll publish it.
It hinges on a couple of classes, and some default views:
The CrudMeta class looks like this:
<?php namespace App\data\meta;
use davestewart\laravel\helpers\meta\CrudMeta;
class UserMeta extends CrudMeta
{
public $singular = 'user';
public $plural = 'users';
public $rules =
[
'name' => 'required|string|min:5|max:20',
'password' => 'sometimes|required|min:6|max:30',
'email' => 'required|email|sometimes'
];
public $controls =
[
'name' => 'text',
'email' => 'text',
'password' => 'text',
];
public $fields =
[
'index' => 'name email',
'create' => 'name email password',
'show' => 'name email',
'edit' => 'name email',
];
}
My controllers can then either use the default trait, or override then make calls to the repo, pass the results to the helper, and render using the default view (per method) or pass to a new view.
Took a while to set up, but means my app is super lean as I only add functionality when I need it, and I don't butcher my controllers or models (or views!) by adding loads of properties that don't really belong there.
Saying that, the app isn't that mature yet... it may be down the road that I find the base abstraction to be too much of a trade-off.
We'll see :)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community