Laravel backpack crud for update and list with relationship

2k views Asked by At

I've got 2 tables with one to many relationship.

  1. portfolios
    id
    category_id
    status
    image

  2. portfolios_lang
    id
    portfolio_id
    title
    text
    lang

I used backpack crud for portfolios_lang and I was able to create records. I added my logic to store() in my PortfolioLangCrudController. But when I want to update a record I can't make it so fields from portfolios were assigned depending on portfolios_lang I selected. Here is screenshot http://joxi.ru/L21x31Vt08aVgm

The same goes for list http://joxi.ru/LmGP76vHleDbO2

I couldn't find anything in documentation. Is there a way to do those things?

EDIT

class PortfolioLangCrudController 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;

/**
 * Configure the CrudPanel object. Apply settings to all operations.
 *
 * @return void
 */
public function setup()
{
    CRUD::setModel(\App\Models\PortfolioLang::class);
    CRUD::setRoute(config('backpack.base.route_prefix') . '/portfoliolang');
    CRUD::setEntityNameStrings('portfoliolang', 'portfolio_lang');
}

/**
 * Define what happens when the List operation is loaded.
 *
 * @see  https://backpackforlaravel.com/docs/crud-operation-list-entries
 * @return void
 */
protected function setupListOperation()
{
    // CRUD::setFromDb(); // columns
    $this->crud->addColumn(['name' => 'portfolio_id', 'type' => 'text', 'label' => 'Portfolio Id']);
    $this->crud->addColumn(['name' => 'category_id', 'type' => 'text', 'label' => 'Category']);
    /* $this->crud->addColumn(['name' => 'category_id', 'type' => 'text', 'label' => 'Category',
         'entity' => 'category',
         'model' => "App\Categories", // related model
         'attribute' => 'name', // foreign key attribute that is shown to user
     ]);*/
    $this->crud->addColumn(['name' => 'title', 'type' => 'text', 'label' => 'Title']);
    $this->crud->addColumn(['name' => 'text', 'type' => 'text', 'label' => 'Text']);
    $this->crud->addColumn(['name' => 'lang', 'type' => 'text', 'label' => 'Lang']);

    $this->crud->addColumn(['name' => 'status', 'type' => 'text', 'label' => 'Status']);

    $this->crud->setHeading('Portfolio', 'list');
    /**
     * Columns can be defined using the fluent syntax or array syntax:
     * - CRUD::column('price')->type('number');
     * - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
     */
}

/**
 * Define what happens when the Create operation is loaded.
 *
 * @see https://backpackforlaravel.com/docs/crud-operation-create
 * @return void
 */
protected function setupCreateOperation()
{
    CRUD::setValidation(PortfolioLangRequest::class);

    $statuses = PortfolioLang::$statuses;
    $langs = Langs::getLangsArray();

    $this->crud->addField(['name' => 'category_id', 'type' => 'select', 'label' => 'Category',
        'entity' => 'categories',
        'model' => "App\Categories", // related model
        'attribute' => 'name', // foreign key attribute that is shown to user
    ]);
    $this->crud->addField(['name' => 'title', 'type' => 'text', 'label' => 'Title']);
    $this->crud->addField(['name' => 'text', 'type' => 'ckeditor', 'label' => 'Text']);
    $this->crud->addField(['name' => 'lang', 'label' => 'Lang',
        'type' => 'select_from_array',
        'options' => $langs,
        'allows_null' => false,
        //'default' => 1,

    ]);
    $this->crud->addField(['name' => 'portfolio_id', 'type' => 'hidden', 'label' => 'Portfolio']);
    $this->crud->addField(['name' => 'status', 'label' => 'Status',
        'type' => 'select_from_array',
        'options' => $statuses,
        'allows_null' => false,
        'default' => 1,
    ]);
    $this->crud->addField(['name' => 'image', 'type' => 'image', 'label' => 'Image',
        'crop' => true, // set to true to allow cropping, false to disable
       // 'aspect_ratio' => 1, // ommit or set to 0 to allow any aspect ratio
    ]);

    $this->crud->setHeading('Portfolio', 'create');

    //CRUD::setFromDb(); // fields

    /**
     * Fields can be defined using the fluent syntax or array syntax:
     * - CRUD::field('price')->type('number');
     * - CRUD::addField(['name' => 'price', 'type' => 'number']));
     */
}

/**
 * Define what happens when the Update operation is loaded.
 *
 * @see https://backpackforlaravel.com/docs/crud-operation-update
 * @return void
 */
protected function setupUpdateOperation()
{

    CRUD::setValidation(PortfolioLangRequest::class);

    $statuses = PortfolioLang::$statuses;
    $langs = Langs::getLangsArray();

    $this->crud->addField(['name' => 'category_id', 'type' => 'select', 'label' => 'Category',
        'entity' => 'categories',
        'model' => "App\Categories", // related model
        'attribute' => 'name', // foreign key attribute that is shown to user
    ]);
    $this->crud->addField(['name' => 'title', 'type' => 'text', 'label' => 'Title']);
    $this->crud->addField(['name' => 'slug', 'type' => 'text', 'label' => 'Slug']);
    $this->crud->addField(['name' => 'text', 'type' => 'ckeditor', 'label' => 'Text',]);

    $this->crud->addField(['name' => 'lang', 'label' => 'Lang',
        'type' => 'select_from_array',
        'options' => $langs,
        'allows_null' => false,
       // 'default' => 1,

    ]);


    $this->crud->addField(['name' => 'portfolio_id', 'type' => 'hidden', 'label' => 'Portfolio']);
    $this->crud->addField(['name' => 'status', 'label' => 'Status',
        'type' => 'select_from_array',
        'options' => $statuses,
        'allows_null' => false,
        'default' => 1,
    ]);
    $this->crud->addField(['name' => 'image', 'type' => 'image', 'label' => 'Image',
        'crop' => true, // set to true to allow cropping, false to disable
       // 'aspect_ratio' => 1, // ommit or set to 0 to allow any aspect ratio
    ]);


    $this->crud->setHeading('Portfolio', 'update');

    // $this->setupCreateOperation();
}


public function store(PortfolioLangRequest $request)
{
    $input = $request->all();
    // var_dump($input['category_id']);

    $portfolios = new Portfolios();
    $portfolios->category_id = $input['category_id'];
    $portfolios->status = $input['status'];
    $portfolios->image = $input['image'];
    $portfolios->save();


    $portfolio_lang = PortfolioLang::where(
        'lang', '=', $input['lang']
    )->where('portfolio_id', '=', $portfolios->id)->first();

    if (empty($portfolio_lang)) {
        $portfolio_lang = new PortfolioLang();
    }
    $portfolio_lang->title = $input['title'];
    $portfolio_lang->text = $input['text'];
    $portfolio_lang->lang = $input['lang'];
    $portfolio_lang->portfolio_id = $portfolios->id;

    $portfolio_lang->save();
    return redirect('admin/portfoliolang');
}

public function update(PortfolioLangRequest $request)
{
    $input = $request->all();
    $portfolio_id = $request->input('portfolio_id');
    $portfolio_lang = PortfolioLang::where(
        'lang', '=', $input['lang']
    )->where('portfolio_id', '=', $portfolio_id)->first();

    if (empty($portfolio_lang)) {
        $portfolio_lang = new PortfolioLang();
    }
    $portfolio_lang->title = $input['title'];
    $portfolio_lang->text = $input['text'];
    $portfolio_lang->lang = $input['lang'];
    $portfolio_lang->portfolio_id = $portfolio_id;

    $portfolio_lang->save();
    return redirect('admin/portfoliolang');
}
}

EDIT 2 Thank you, I was able to show categories in list

 $this->crud->addColumn(['name' => 'portfolio_id', 'type' => 'select',
            'label' => 'Category', 'model' => "App\Categories",'attribute' => 'name','entity' => 'portfolio.category',
            'key'=>'categoryName']);

But is there no way I can edit related fields from one form?

0

There are 0 answers