Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

laravel backpack and iis and sql server #1047

Closed
maryxis opened this issue Oct 5, 2017 · 6 comments
Closed

laravel backpack and iis and sql server #1047

maryxis opened this issue Oct 5, 2017 · 6 comments

Comments

@maryxis
Copy link

maryxis commented Oct 5, 2017

Hi,

I'm new in Laravel and i have several issues according my connection with my iis and sql server.

First of all i have 405 error on deleting from CrudTable. I suppose that this is an iis error but i'm asking here in case that someone knows how to figure this out.

The other problem that i have is that i cannot display in 1 to many relationship the value and not the id of a foreign table.

I'm getting an empty column and in create blade, i select a specific value and the system inserts a zero on that column.

Below you can see my code.

table book_code
{
code varchar(15) PK,
title varchar(50)
}

bookcodes Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class bookcodes extends Model
{
    use CrudTrait;

     /*
    |--------------------------------------------------------------------------
    | GLOBAL VARIABLES
    |--------------------------------------------------------------------------
    */

    protected $table = 'book_codes';
    protected $primaryKey = 'code';
    // public $timestamps = false;
    // protected $guarded = ['id'];
    protected $fillable = ['title'];
    // protected $hidden = [];
    // protected $dates = [];

    /*
    |--------------------------------------------------------------------------
    | FUNCTIONS
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */

    public function rest_declaration()
    {
        return $this->hasMany('App\Models\rest_declaration');
    }
    /*
    |--------------------------------------------------------------------------
    | SCOPES
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | ACCESORS
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | MUTATORS
    |--------------------------------------------------------------------------
    */
}
`
bookcodes Controller has nothing

table rest_declaration
{
id int,
date1 date,
date2 date,
code //the foreign of bookcodes
}

rest_declaration Model

`<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use DB;

class rest_declaration extends Model
{
    use CrudTrait;

     /*
    |--------------------------------------------------------------------------
    | GLOBAL VARIABLES
    |--------------------------------------------------------------------------
    */

    protected $table = 'rest_declaration';
    protected $primaryKey = 'aa'; //is my id
    // public $timestamps = false;
    // protected $guarded = ['id'];
    protected $fillable = ['aa', 'code', 'date1', 'date2'];
    // protected $hidden = [];
    // protected $dates = [];

    /*
    |--------------------------------------------------------------------------
    | FUNCTIONS
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */

    public function bookcodes()
    {
        return $this->belongsTo('App\Models\bookcodes');
    }

    /*
    |--------------------------------------------------------------------------
    | SCOPES
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | ACCESORS
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | MUTATORS
    |--------------------------------------------------------------------------
    */
}

rest_declaration Controller

<?php

namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Http\Controllers\CrudController;

// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\rest_declarationRequest as StoreRequest;
use App\Http\Requests\rest_declarationRequest as UpdateRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class rest_declarationCrudController extends CrudController
{
    public function setup()
    {
		parent::__construct();

        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
        */
        $this->crud->setModel('App\Models\rest_declaration');
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/restdeclaration');
        $this->crud->setEntityNameStrings('rest declaration', 'rest declaration');
        $this->crud->setDefaultPageLength(10);
        $this->crud->allowAccess('show');
        $this->crud->addClause('where', 'code', '=', Auth::user()->name );

        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
        */

        // $this->crud->setFromDb();

        //Columns for Datatable
        

        // ------ CRUD FIELDS
        // $this->crud->addField($options, 'update/create/both');
        // $this->crud->addFields($array_of_arrays, 'update/create/both');
        // $this->crud->removeField('name', 'update/create/both');
        // $this->crud->removeFields($array_of_names, 'update/create/both');

        // ------ CRUD COLUMNS
        // $this->crud->addColumn(); // add a single column, at the end of the stack
        // $this->crud->addColumns(); // add multiple columns, at the end of the stack
        // $this->crud->removeColumn('column_name'); // remove a column from the stack
        // $this->crud->removeColumns(['column_name_1', 'column_name_2']); // remove an array of columns from the stack
        // $this->crud->setColumnDetails('column_name', ['attribute' => 'value']); // adjusts the properties of the passed in column (by name)
        // $this->crud->setColumnsDetails(['column_1', 'column_2'], ['attribute' => 'value']);

        // ------ CRUD BUTTONS
        // possible positions: 'beginning' and 'end'; defaults to 'beginning' for the 'line' stack, 'end' for the others;
        // $this->crud->addButton($stack, $name, $type, $content, $position); // add a button; possible types are: view, model_function
        // $this->crud->addButtonFromModelFunction($stack, $name, $model_function_name, $position); // add a button whose HTML is returned by a method in the CRUD model
        // $this->crud->addButtonFromView($stack, $name, $view, $position); // add a button whose HTML is in a view placed at resources\views\vendor\backpack\crud\buttons
        // $this->crud->removeButton($name);
        // $this->crud->removeButtonFromStack($name, $stack);
        // $this->crud->removeAllButtons();
        // $this->crud->removeAllButtonsFromStack('line');

        // ------ CRUD ACCESS
        // $this->crud->allowAccess(['list', 'create', 'update', 'reorder', 'delete']);
        // $this->crud->denyAccess(['delete']);

        // ------ CRUD REORDER
        // $this->crud->enableReorder('label_name', MAX_TREE_LEVEL);
        // NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('reorder');

        // ------ CRUD DETAILS ROW
        // $this->crud->enableDetailsRow();
        // NOTE: you also need to do allow access to the right users: $this->crud->allowAccess('details_row');
        // NOTE: you also need to do overwrite the showDetailsRow($id) method in your EntityCrudController to show whatever you'd like in the details row OR overwrite the views/backpack/crud/details_row.blade.php

        // ------ REVISIONS
        // You also need to use \Venturecraft\Revisionable\RevisionableTrait;
        // Please check out: https://laravel-backpack.readme.io/docs/crud#revisions
        // $this->crud->allowAccess('revisions');

        // ------ AJAX TABLE VIEW
        // Please note the drawbacks of this though:
        // - 1-n and n-n columns are not searchable
        // - date and datetime columns won't be sortable anymore
        $this->crud->enableAjaxTable();

        // ------ DATATABLE EXPORT BUTTONS
        // Show export to PDF, CSV, XLS and Print buttons on the table view.
        // Does not work well with AJAX datatables.
        $this->crud->enableExportButtons();
		

        //Fields for Create and Edit Forms
        $this->crud->addField([    // SELECT
            'label'                     => 'AA',
            'type'                      => 'text',
            'name'                      => 'aa1',
            'value'                     => $this->getmaxaa(),
            'attributes'                => 
                            ['disabled' => 'disabled',],
        ]);
        $this->crud->addField([    // SELECT
            'label'                     => 'AA',
            'type'                      => 'hidden',
            'name'                      => 'aa',
            'value'                     => $this->getmaxaa(),
        ]);

        $this->crud->addField([    // SELECT
            'label'                     => 'BookCode',
            //'type'                      => 'select',
            'name'                      => 'code',
            'entity'                    => 'bookcodes',
            'attribute'                 => 'title',
            'model'                     => 'App\Models\bookcodes',
        ]);

        $this->crud->addField([   // Date
            'name'                      => 'date1',
            'label'                     => 'From',
            'type'                      => 'date_picker',
            // optional:
            'date_picker_options'       => [
                        'todayBtn'      => false,
                        'format'        => 'dd-mm-yyyy',
                        'language'      => 'en',
                        'autoclose'     => true,
                        'showOnFocus'   => false,
            ],
        ]);

        $this->crud->addField([   // Date
            'name'                      => 'date2',
            'label'                     => 'To',
            'type'                      => 'date_picker',
            // optional:
            'date_picker_options'       => [
                        'todayBtn'      => false,
                        'format'        => 'dd-mm-yyyy',
                        'language'      => 'en',
                        'autoclose'     => true,
                        'showOnFocus'   => true,
            ],
        ]);

        $this->crud->addColumn([
            'label'                     => 'Άδεια',
            'type'                      => 'select',
            'name'                      => 'code',
            'entity'                    => 'bookcodes',
            'attribute'                 => 'title',
            'model'                     => 'App\Models\bookcodes',
        ]);

        $this->crud->addColumn([
                'name'                  => 'date1',
                'label'                 => 'From',
                'type'                  => 'date',
                'date_picker_options'   => [
                'todayBtn'              => false,
                'format'                => 'dd-mm-yyyy',
                'language'              => 'en',],
            ]);

        $this->crud->addColumn([
                'name'                  => 'date2',
                'label'                 => 'To',
                'type'                  => 'date',
                'date_picker_options'   => [
                'todayBtn'              => false,
                'format'                => 'dd-mm-yyyy',
                'language'              => 'en',],
            ]);

        // ------ ADVANCED QUERIES
        // $this->crud->addClause('active');
        // $this->crud->addClause('type', 'car');
        // $this->crud->addClause('where', 'name', '==', 'car');
        // $this->crud->addClause('whereName', 'car');
        // $this->crud->addClause('whereHas', 'posts', function($query) {
        //     $query->activePosts();
        // });
        // $this->crud->addClause('withoutGlobalScopes');
        // $this->crud->addClause('withoutGlobalScope', VisibleScope::class);
        // $this->crud->with(); // eager load relationships
        // $this->crud->orderBy('aa');
        // $this->crud->groupBy();
        // $this->crud->limit();
    }

    public function store(StoreRequest $request)
    {
        // your additional operations before save here
        $redirect_location = parent::storeCrud($request);
        // your additional operations after save here
        // use $this->data['entry'] or $this->crud->entry
        return $redirect_location;
    }

    public function update(UpdateRequest $request)
    {
        // your additional operations before save here
        $redirect_location = parent::updateCrud($request);
        // your additional operations after save here
        // use $this->data['entry'] or $this->crud->entry
        return $redirect_location;
    }

    public function getmaxaa()
    {
        $max = 'App\Models\AitisiAdeias'::max('aa');
        $max = $max + 1;
        return $max;
    }

}

What am i doing wrong?

Could you help me please??

@lloy0076
Copy link
Contributor

lloy0076 commented Oct 9, 2017

Comments in Code Removed for Clarity

Below you can see my code.

table book_code
{
code varchar(15) PK,
title varchar(50)
}

bookcodes Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class bookcodes extends Model
{
    use CrudTrait;

    protected $table = 'book_codes';
    protected $primaryKey = 'code';
    protected $fillable = ['title'];

    public function rest_declaration()
    {
        return $this->hasMany('App\Models\rest_declaration');
    }
}

bookcodes Controller has nothing

table rest_declaration
{
id int,
date1 date,
date2 date,
code //the foreign of bookcodes
}

rest_declaration Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;
use DB;

class rest_declaration extends Model
{
    use CrudTrait;

    protected $table = 'rest_declaration';
    protected $primaryKey = 'aa'; //is my id
    protected $fillable = ['aa', 'code', 'date1', 'date2'];
 
    public function bookcodes()
    {
        return $this->belongsTo('App\Models\bookcodes');
    }
}

rest_declaration Controller

<?php

namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Http\Controllers\CrudController;

// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\rest_declarationRequest as StoreRequest;
use App\Http\Requests\rest_declarationRequest as UpdateRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


class rest_declarationCrudController extends CrudController
{
    public function setup()
    {
		parent::__construct();

        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
        */
        $this->crud->setModel('App\Models\rest_declaration');
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/restdeclaration');
        $this->crud->setEntityNameStrings('rest declaration', 'rest declaration');
        $this->crud->setDefaultPageLength(10);
        $this->crud->allowAccess('show');
        $this->crud->addClause('where', 'code', '=', Auth::user()->name );

        $this->crud->allowAccess('details_row');
        $this->crud->enableAjaxTable();
        $this->crud->enableExportButtons();
	
        //Fields for Create and Edit Forms
        $this->crud->addField([    // SELECT
            'label'                     => 'AA',
            'type'                      => 'text',
            'name'                      => 'aa1',
            'value'                     => $this->getmaxaa(),
            'attributes'                => 
                            ['disabled' => 'disabled',],
        ]);
        $this->crud->addField([    // SELECT
            'label'                     => 'AA',
            'type'                      => 'hidden',
            'name'                      => 'aa',
            'value'                     => $this->getmaxaa(),
        ]);

        $this->crud->addField([    // SELECT
            'label'                     => 'BookCode',
            //'type'                      => 'select',
            'name'                      => 'code',
            'entity'                    => 'bookcodes',
            'attribute'                 => 'title',
            'model'                     => 'App\Models\bookcodes',
        ]);

        $this->crud->addField([   // Date
            'name'                      => 'date1',
            'label'                     => 'From',
            'type'                      => 'date_picker',
            // optional:
            'date_picker_options'       => [
                        'todayBtn'      => false,
                        'format'        => 'dd-mm-yyyy',
                        'language'      => 'en',
                        'autoclose'     => true,
                        'showOnFocus'   => false,
            ],
        ]);

        $this->crud->addField([   // Date
            'name'                      => 'date2',
            'label'                     => 'To',
            'type'                      => 'date_picker',
            // optional:
            'date_picker_options'       => [
                        'todayBtn'      => false,
                        'format'        => 'dd-mm-yyyy',
                        'language'      => 'en',
                        'autoclose'     => true,
                        'showOnFocus'   => true,
            ],
        ]);

        $this->crud->addColumn([
            'label'                     => 'Άδεια',
            'type'                      => 'select',
            'name'                      => 'code',
            'entity'                    => 'bookcodes',
            'attribute'                 => 'title',
            'model'                     => 'App\Models\bookcodes',
        ]);

        $this->crud->addColumn([
                'name'                  => 'date1',
                'label'                 => 'From',
                'type'                  => 'date',
                'date_picker_options'   => [
                'todayBtn'              => false,
                'format'                => 'dd-mm-yyyy',
                'language'              => 'en',],
            ]);

        $this->crud->addColumn([
                'name'                  => 'date2',
                'label'                 => 'To',
                'type'                  => 'date',
                'date_picker_options'   => [
                'todayBtn'              => false,
                'format'                => 'dd-mm-yyyy',
                'language'              => 'en',],
            ]);
    }

    public function store(StoreRequest $request)
    {
        $redirect_location = parent::storeCrud($request);
        return $redirect_location;
    }

    public function update(UpdateRequest $request)
    {
        $redirect_location = parent::updateCrud($request);
        return $redirect_location;
    }

    public function getmaxaa()
    {
        $max = 'App\Models\AitisiAdeias'::max('aa');
        $max = $max + 1;
        return $max;
    }

}

What am i doing wrong?

Could you help me please??

@lloy0076
Copy link
Contributor

lloy0076 commented Oct 9, 2017

This could be a bug but it's unlikely - it may be worth asking the question on Stack Overflow too.

@maryxis
Copy link
Author

maryxis commented Oct 11, 2017

The problem with deletion was in iis and that fixed as i inserted a new handler mapping to match my app with the correct php version (7.1) and used all verbs. That fixed this problem.

Is there anyone else that is using backpack with ms sql server? I think that something is not working with relationships.
I cannot find what is going wrong. If i use the same installation in mysql everything is working but in ms sql server is not working.

@maryxis
Copy link
Author

maryxis commented Oct 13, 2017

Finally,

I found what the problem was, but i need help here..
The problem is that the key is not integer but it is nvarchar(50). Could you please help me find solution on this because i cannot change the schema of the database.

@lloy0076
Copy link
Contributor

I'm confused as to whether you think the problem is with Backpack or with the MySQL vs SQL Server - if it's a problem with SQL Server and its interpretation of data then the problem is in the database - and not with Backpack.

In fact, what is the problem again?

@tabacitu
Copy link
Member

Hi guys - sorry, going to close this, as it's not a bug, but a StackOverflow question.

@maryxis , would you mind moving the conversation there? It's important to us to keep Github for bugs only, otherwise it's really difficult to separate Backpack issues from implementation issues.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants