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

you could have a partial view with the common input for create and edit

http://laravel.com/docs/5.1/blade#control-structures

0

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... 
0

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?

Last updated 8 years ago.
0

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
Last updated 8 years ago.
0

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?

Last updated 8 years ago.
0

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.

0

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.

Last updated 8 years ago.
0

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:

  • CrudMethods - a traits file with default CRUD methods for controllers
  • CrudHelper - core functionality for various CRUD methods with a getViewData() method to wrap it all up
  • CrudMeta - a metadata class, one per model, which describes the properties views should render
  • views for each of the CRUD methods plus index, which build themselves according to the passed model's meta

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 :)

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mseymour mseymour Joined 25 Feb 2014

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.