4

I'm using the Backpack for Laravel CRUD features & currently having an issue with the select field type as to which after saving the form, once I go back to try and edit it isn't pre-defining my selected option?

Here's my setup() code inside my Accounts controller:

$this->crud->setModel("App\Models\Accounts");
$this->crud->setRoute("admin/accounts");
$this->crud->setEntityNameStrings('Accounts', 'Account');

$this->crud->addField([
    'label' => "Proxy",
    'type' => 'select',
    'name' => 'proxy_id',
    'entity' => 'proxy',
    'attribute' => 'id',
    'model' => "App\Models\Proxies",
    'nullable' => true
]);

And here's my proxy() entity within the Accounts model:

public function proxy()
{
    return $this->hasOne(Proxies::class, 'id', 'proxy_id');
}

Here's the App\Models\Proxies class:

class Proxies extends Model
{

    use CrudTrait;

    public $timestamps = false;

    protected $table = 'proxies';

    protected $fillable = [
        'ip',
        'port',
        'username',
        'password',
        'status'
    ];

    public function getStatusValue()
    {
        switch($this->status) {
            case "0":
                return '<span class="label label-success">Offline</span>';
                break;
            case "1":
                return '<span class="label label-success">Online</span>';
                break;
        }
        return null;
    }

}

Here's my DB migration for the proxy_id column:

public function up()
{
    Schema::table('accounts', function(Blueprint $table)
    {
        $table->integer('proxy_id')->nullable();
    });
}

UPDATE I have added {!! $field['name'].' - '.old($field['name']).' - '.$connected_entity_entry->getKey() !!} to a newly created file: /resources/views/vendor/backpack/crud/fields/select.blade.php and the result I'm seeing is:

proxy_id -  - 1

So it appears the old() function isn't working correctly?

Curtis
  • 2,646
  • 6
  • 29
  • 53
  • `$this->hasOne(Proxies::class,'proxy_id' ,'id');` parameter is wrong. – Dilip Hirapara Oct 03 '19 at 12:15
  • @Laravel how? it works okay for me when it comes to accessing via `(Model)->proxy` – Curtis Oct 03 '19 at 12:24
  • https://laravel.com/docs/6.x/eloquent-relationships#one-to-one as you see here. `return $this->hasMany('App\Comment', 'foreign_key', 'local_key');` – Dilip Hirapara Oct 03 '19 at 12:26
  • @Laravel it doesn't have many? it has only 1 entity assigned to it – Curtis Oct 03 '19 at 12:28
  • Same for hasone as well `'foreign_key'` first will be foreign key always and second will be `local_key` – Dilip Hirapara Oct 03 '19 at 12:30
  • @Laravel as expected, you was wrong `Column not found: 1054 Unknown column 'proxies.proxy_id' in 'where clause' ` – Curtis Oct 03 '19 at 12:42
  • Can you verify that the `proxy_id` is properly saved in the database when saving the record? – Wesley Smith Oct 07 '19 at 16:35
  • Can you show what your frontend code looks like? Specifically, the code which renders the selected option? – Vince Oct 07 '19 at 19:28
  • @DelightedD0D yes, it appears that the `proxy_id` is updated with an integer & corresponds correctly; my only assumption that could be causing this is the fact the IP has dots which could be causing confusion with the `if` statement when validating if it's a match? – Curtis Oct 08 '19 at 10:52
  • Ahh, ok, are you saying that a value like `123.343.33.3` gets saved in the db like `123343333`? If so, you have 2 options, `1)` change your database column from an INT to a VARCHAR which will then store the periods with the numbers as a string. `2)` write a custom field that removes the periods before comparing the values. Sounds like you probably want `1` – Wesley Smith Oct 08 '19 at 12:38
  • If that doesnt get you what you need, please post the code you use to create the 2 tables in your migrations – Wesley Smith Oct 08 '19 at 12:40
  • @DelightedD0D I've gone ahead & updated my question! – Curtis Oct 08 '19 at 21:30
  • the output of `proxy_id - - 1` would seem to indicate that the proxy_id field is not on the incoming model. are you certain that the record in your DB has a value for `proxy_id`? – Wesley Smith Oct 09 '19 at 08:03
  • 1
    can you verify that `proxy_id` is in your `Accounts` model's `fillable` array, and _not_ in the model's `guarded` array? – Wesley Smith Oct 09 '19 at 08:05

0 Answers0