Cinzia Nicoletti
Cinzia Nicoletti

Reputation: 197

How to get the value of one of the attributes used by Backpack?

For example, I need to take the "id" value and use it to do a search for my model Article, but this value (id) which also appears in the URL: "/article/4/edit" and in the "setColumns" parameters, I don't have any idea how to get it.

I need your help. This is my sample code:

ArticleCrudController.php

public function __construct()
{
    parent::__construct();

    $this->crud->setModel('App\Models\Article');
    $this->crud->setRoute("admin/article");
    $this->crud->setEntityNameStrings('article', 'articles');

    $this->crud->setColumns(['id', 'title', 'text']);

    // WHERE ARE YOU ID?!?!!!
    $article = Article::findOrFail($id);
    $pictures = $article->picture()->first()->name;

    $this->crud->addFields([
        [
            'name' => 'title',
            'label' => 'Title',
            'type' => 'Text'
        ],
        [
            'name' => 'text',
            'label' => 'Text',
            'type' => 'ckeditor'
        ],
        [   // Browse
            'name' => 'image',
            'label' => 'Article immage',
            'type' => 'browse',
            'value' => $pictures //Obviously without id don't work :'(
        ]

    ]);

}

Upvotes: 2

Views: 2601

Answers (3)

KordDEM
KordDEM

Reputation: 215

As of 2023, Federico Liva's answer doesn't quite work. Swears at parent::edit($id);

Because the working version

public function setupUpdateOperation()
{
         $actual_model =$this->crud->getCurrentEntry();
         dd($actual_model->id);
}

Upvotes: 0

Balaji Dharma
Balaji Dharma

Reputation: 1423

Use getCurrentEntry to get the current model object in crud

$article_id = $this->crud->getCurrentEntry()->id;

Upvotes: 4

Federico Liva
Federico Liva

Reputation: 578

You could try to override the CrudController::edit method to which is passed the id as first parameter.

public function edit($id)
{
    $articlePicture = Article::findOrFail($id)->picture()->first()->name;

    $this->crud->addField([
        'name' => 'image',
        'value' => $articlePicture
    ]);

    return parent::edit($id);
}

This could be a solution but I'm not sure it's the right way to do what you want.

Upvotes: 3

Related Questions