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

Unable to add inline button #612

Open
NeguinhoAdmin opened this issue Jul 1, 2024 · 4 comments
Open

Unable to add inline button #612

NeguinhoAdmin opened this issue Jul 1, 2024 · 4 comments
Assignees

Comments

@NeguinhoAdmin
Copy link

I am trying to add inline add button while inserting but button not appear

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\MotorbikeRepairRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
use App\Models\Motorbike;

class MotorbikeRepairCrudController extends CrudController
{
    use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;
    use \Backpack\CRUD\app\Http\Controllers\Operations\InlineCreateOperation;


    public function setup()
    {
        CRUD::setModel(\App\Models\MotorbikeRepair::class);
        CRUD::setRoute(config('backpack.base.route_prefix') . '/motorbike-repair');
        CRUD::setEntityNameStrings('motorbike repair', 'motorbike repairs');
    }

    protected function setupListOperation()
    {
        CRUD::column('motorbike.reg_no')->label('Registraction No');

        CRUD::setFromDb();

        CRUD::addFilter(
            [
                'name'  => 'motorbike_reg_no',
                'type'  => 'text',
                'label' => 'Registration No'
            ],
            false,
            function ($value) {
                $this->crud->addClause('whereHas', 'motorbike', function ($query) use ($value) {
                    $query->where('reg_no', 'LIKE', "%$value%");
                });
            }
        );

        CRUD::removeColumn('motorbike_id');
    }

    protected function setupCreateOperation()
    {
        CRUD::setValidation(MotorbikeRepairRequest::class);
        CRUD::setOperationSetting('contentClass', 'col-md-12');
        CRUD::setFromDb();

        CRUD::field('motorbikes')->ajax(true)->minimum_input_length(0)->inline_create(true);

        CRUD::field('fullname')->label('Full Name');
        CRUD::field('email')->label('Email');
        CRUD::field('phone')->label('Phone');

        // CRUD::field('motorbike_id')->label('Motorbike')
        //     ->type('select2')
        //     ->entity('motorbike')
        //     ->attribute('reg_no')
        //     ->model(Motorbike::class)->ajax(true)->minimum_input_length(0)->inline_create(true);

        CRUD::addField([
            'name' => 'motorbike_id',
            'label' => 'Motorbike',
            'type' => 'relationship',
            'entity' => 'motorbike',
            'attribute' => 'reg_no',
            'model' => Motorbike::class,
            'ajax' => true,
            'inline_create' => [
                'entity' => 'motorbike',
                'force_select' => true,
                'modal_class' => 'modal-dialog modal-xl',
            ],
        ]);

        CRUD::field('arrival_date')->label('Arrival Date')->type('datetime');
        CRUD::field('motorbike_id')->label('Motorbike')->type('select2')->entity('motorbike')->attribute('reg_no')->model(Motorbike::class);

        CRUD::field('notes')->label('Notes')->type('textarea');
        CRUD::field('is_repaired')->label('Is Repaired')->type('checkbox');
        CRUD::field('repaired_date')->label('Repaired Date')->type('date');
        CRUD::field('is_returned')->label('Is Returned')->type('checkbox');
        CRUD::field('returned_date')->label('Returned Date')->type('datetime');

        CRUD::addField([
            'name' => 'updates',
            'label' => 'Repair Updates',
            'type' => 'repeatable',
            'fields' => [
                [
                    'name' => 'motorbike_repair_id',
                    'label' => 'Motorbike Repair ID',
                    'type' => 'hidden',
                ],
                [
                    'name' => 'job_description',
                    'label' => 'Job Description',
                    'type' => 'text',
                    'wrapper' => ['class' => 'form-group col-md-3'],
                ],
                [
                    'name' => 'price',
                    'label' => 'Price',
                    'type' => 'number',
                    'wrapper' => ['class' => 'form-group col-md-3'],
                ],
                [
                    'name' => 'note',
                    'label' => 'Note',
                    'type' => 'textarea',
                    'wrapper' => ['class' => 'form-group col-md-3'],
                ],
            ],
        ]);
    }

    protected function setupUpdateOperation()
    {
        $this->setupCreateOperation();
    }
}

Copy link

welcome bot commented Jul 1, 2024

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication mediums:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (How do I do X) - Gitter Chatroom;
  • Long questions (I have done X and Y and it won't do Z wtf) - Stackoverflow, using the backpack-for-laravel tag;

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

--
Justin Case
The Backpack Robot

@jcastroa87
Copy link
Member

Hello @NeguinhoAdmin

You need to set as a relationship field as our docs say.

CRUD::field('motorbikes')->type('relationship')->ajax(true)->minimum_input_length(0)->inline_create(true);

How did you setup the relation in models?

Cheers.

@NeguinhoAdmin
Copy link
Author

NeguinhoAdmin commented Jul 3, 2024

I have added line; it says -> Undefined array key "relation_type"

Model

namespace App\Models;

use Backpack\CRUD\app\Models\Traits\CrudTrait;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MotorbikeRepair extends Model
{
    use CrudTrait;
    use HasFactory;

    protected $table = 'motorbikes_repair';

    protected $fillable = [
        'arrival_date',
        'motorbike_id',
        'notes',
        'is_repaired',
        'repaired_date',
        'is_returned',
        'returned_date',
        'fullname',
        'email',
        'phone',
        'branch_id',
        'user_id',
    ];

    public function motorbike()
    {
        return $this->belongsTo(Motorbike::class);
    }

    public function updates()
    {
        return $this->hasMany(MotorbikeRepairUpdate::class);
    }

    public function branch()
    {
        return $this->belongsTo(Branch::class);
    }
}

        Schema::create('motorbikes_repair', function (Blueprint $table) {
            $table->id();
            $table->datetime('arrival_date')->nullable(false);
            $table->unsignedBigInteger('motorbike_id')->nullable(false);
            $table->foreign('motorbike_id')->references('id')->on('motorbikes')->onDelete('restrict');
            $table->text('notes')->nullable(false);
            $table->boolean('is_repaired')->default(false);
            $table->datetime('repaired_date')->nullable();
            $table->boolean('is_returned')->default(false);
            $table->datetime('returned_date')->nullable();
            $table->timestamps();
        });

I have added line; it says -> Undefined array key "relation_type"

@jcastroa87
Copy link
Member

Hello @NeguinhoAdmin

First, the "InlineCreateOperation" should be in Motorbike Controller.
Dont forget the fetch if you are using ajax.
Then in MotorbikeRepair Controller the field could be like this

CRUD::field([
            'type' => "relationship",
            'name' => 'motorbike',
            'ajax' => false,
            'inline_create' => true
        ]);

Then in your controller you have 3 times the motorbike_id field:

CRUD::field('motorbikes')->ajax(true)->minimum_input_length(0)->inline_create(true);
CRUD::addField([
            'name' => 'motorbike_id',
            'label' => 'Motorbike',
            'type' => 'relationship',
            'entity' => 'motorbike',
            'attribute' => 'reg_no',
            'model' => Motorbike::class,
            'ajax' => true,
            'inline_create' => [
                'entity' => 'motorbike',
                'force_select' => true,
                'modal_class' => 'modal-dialog modal-xl',
            ],
        ]);
CRUD::field('motorbike_id')->label('Motorbike')->type('select2')->entity('motorbike')->attribute('reg_no')->model(Motorbike::class);

Cheers.

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

No branches or pull requests

2 participants