0

I use locations using nested tree and use a "parent" field to select parent location (that i created using reorder records). But i need "parent" field to show me only parents, only locations with parent_id equals to NULL.

On location model fields.yaml i have:

 fields:
    name:
        label: Name
        oc.commentPosition: ''
        span: left
        required: 1
        type: text
    slug:
        label: Slug
        oc.commentPosition: ''
        span: right
        required: 1
        preset:
            field: name
            type: slug
        type: text
    parent:
        label: Parent Location
        oc.commentPosition: ''
        nameFrom: name
        descriptionFrom: description
        span: left
        type: relation
        placeholder: 'select parent location'
    description:
        label: Description
        size: large
        oc.commentPosition: ''
        span: right
        type: textarea

But now i get all locations on backend field "parent". Is there a way to get only parent locations?

Table schema:

Entries:

            $table->engine = 'InnoDB';
            $table->increments('id')->unsigned();
            $table->integer('user_id')->nullable()->unsigned();
            $table->string('name')->nullable();
            $table->string('slug');
            $table->text('content')->nullable();
            $table->integer('location_id')->nullable();
            $table->timestamp('published_at')->nullable();
            $table->boolean('published')->default(0);
            $table->timestamp('created_at')->nullable();
            $table->timestamp('updated_at')->nullable();

Locations:

        $table->engine = 'InnoDB';
        $table->increments('id')->unsigned();
        $table->string('name')->nullable();
        $table->string('slug')->nullable();
        $table->text('description')->nullable();
        $table->integer('parent_id')->nullable()->unsigned();
        $table->integer('nest_left')->nullable();
        $table->integer('nest_right')->nullable();
        $table->integer('nest_depth')->nullable();
        $table->timestamp('created_at')->nullable();
        $table->timestamp('updated_at')->nullable();
Charis
  • 117
  • 2
  • 4
  • 12
  • means you want to show `drop-down` with records whose `parent_id IS NULL` and don't care about relational data means it's one to one relation right ? – Hardik Satasiya Feb 02 '18 at 21:25
  • On Entry model i use `belongsTo`, and on Location model i use `hasMany` – Charis Feb 04 '18 at 15:22

1 Answers1

0

Hmm, I am not sure we can pass condition with relation widget and make filtered list from database, instead I suggest to use manually drop-down way to have more control.

To do that , change your relational field parent in configuration to parent_id (as this will be your db field)

New config will look like this

parent_id:
    label: Parent Location
    oc.commentPosition: ''
    span: left
    type: dropdown
    placeholder: 'select parent location'

Now inside your location model you need to provide source

public function getParentIdOptions(){
    return \Locations::->whereNull('parent_id')->lists('name', 'id');
}

this will return array of location (id, name key => value pair) where parent_id IS NULL or your based on your custom condition.

please try this if you face any issue please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • I had tried dropdown method but with weird results. When i try to create one location and select parent_id field i get correct parent_id names, but when i save location and reopen it there is no parent_id selected in dropdown. At least is not shown it. – Charis Feb 04 '18 at 21:52
  • if possible can you share your table schema for both – Hardik Satasiya Feb 05 '18 at 05:45
  • oh i get it its because next time you visit record , may be that record's parent_id <- this record has assigned parent_id so now he may be not visible to drop down because of `IS NULL` condition MAY BE, can you check that and confirm me so i can think about solution – Hardik Satasiya Feb 05 '18 at 15:39
  • for drop-downs if active item is not there then it will show first most item if place holder is not there and we think its selected value in that case, i guess – Hardik Satasiya Feb 05 '18 at 16:05
  • Ok the problem was that i used `return Location::whereNull('parent_id')->lists('name');` instead of `return Location::whereNull('parent_id')->lists('name', 'id');`. Now everything is working but when i open parent location i get placeholder message and thats ok, but i steal can choose parents. The correct way should be to get zero results on parent_id field. – Charis Feb 05 '18 at 18:17
  • If i open a parent location i can even choose the same location as parent_id, and then i get `A node cannot be moved to a descendant of itself`. – Charis Feb 05 '18 at 18:19
  • we can do teamviewer if you need extra help tomorrow – Hardik Satasiya Feb 05 '18 at 20:49
  • Skype : hardik_satasiya – Hardik Satasiya Feb 06 '18 at 06:01