engrnvd / laravel-crud-generator

MIT License
137 stars 51 forks source link

Edit #1

Closed jimgwhit closed 8 years ago

jimgwhit commented 8 years ago

The generator worked, but no edit page or view page will show, just got errors. Using laravel 5.2. Search and pagination works. Im my table, petid is primary key, I get

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `pets` where `id` = 4 limit 1)

I tryed changing all occurances of id to petid, still nothing. In model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Pet extends Model {

    public $guarded = ["id","created_at","updated_at"];

    public static function findRequested()
    {
        $query = Pet::query();

        // search results based on user input
        \Request::input('petid') and $query->where('petid',\Request::input('petid'));
        \Request::input('petname') and $query->where('petname','like','%'.\Request::input('petname').'%');
        \Request::input('ownerid') and $query->where('ownerid',\Request::input('ownerid'));
        \Request::input('petowner') and $query->where('petowner','like','%'.\Request::input('petowner').'%');
        \Request::input('species') and $query->where('species','like','%'.\Request::input('species').'%');
        \Request::input('sex') and $query->where('sex',\Request::input('sex'));
        \Request::input('ostreet') and $query->where('ostreet','like','%'.\Request::input('ostreet').'%');
        \Request::input('odate') and $query->where('odate',\Request::input('odate'));
        \Request::input('ocheck') and $query->where('ocheck',\Request::input('ocheck'));
        \Request::input('dogpic') and $query->where('dogpic','like','%'.\Request::input('dogpic').'%');

        // sort results
        \Request::input("sort") and $query->orderBy(\Request::input("sort"),\Request::input("sortType","asc"));

        // paginate results
        return $query->paginate(15);
    }

    public static function validationRules( $attributes = null )
    {
        $rules = [
            'petid' => 'required|integer',
            'petname' => 'required|string|max:30',
            'ownerid' => 'integer',
            'petowner' => 'string|max:30',
            'species' => 'string|max:20',
            'sex' => '',
            'ostreet' => 'string|max:15',
            'odate' => 'date',
            'ocheck' => '',
            'dogpic' => 'string|max:100',
        ];

        // no list is provided
        if(!$attributes)
            return $rules;

        // a single attribute is provided
        if(!is_array($attributes))
            return $rules[$attributes];

        // a list of attributes is provided
        $newRules = [];
        foreach ( $attributes as $attr )
            $newRules[$attr] = $rules[$attr];
        return $newRules;
    }

}

I tryed changing

\Request::input('petid') and $query->where('petid',\Request::input('petid'));

to

\Request::input('petid') and $query->where('petid',\Request::input('id'));

also tryed changing

public $guarded = ["id","created_at","updated_at"];

to

public $guarded = ["petid","created_at","updated_at"];

I have tryed various combinations of id and petid.

engrnvd commented 8 years ago

Hi @jimgwhit

At the moment NVD CRUD generator accepts only 'id' column as the primary key. Change 'petid' to 'id' in your database table and the problem will be gone.

P.S. Having a primary key other than 'id' will be supported in upcoming versions. But right now, you have to follow the convention.

jimgwhit commented 8 years ago

Thanks for the reply, any idea how soon the version will be out that supports other than id?

engrnvd commented 8 years ago

It's the first thing on my Todo list.

silnose commented 7 years ago

Do you have any news?