LaravelDaily / quickadmin

Quick adminpanel builder package for Laravel 5
MIT License
600 stars 193 forks source link

Multiple relationships to one model issue #122

Closed Kazuto closed 6 years ago

Kazuto commented 7 years ago

When I try to add two relationships to one controller it will add two selectboxes on the second relationship and will result in a duplicate of use App\RelationshipModel inside the created controller.

szymon-szychulski commented 7 years ago

This is because Quick Admin Panel is used name of model while creating relationship, and if we want to create multiple relationship to one model, in code this two fields refer to the same variable and this causes conflict.

I solved this problem by giving these two relationships a different, unique names in code.

In Controller of this CRUD:

    ...

    public function index(Request $request)
    {
        $elements = CRUD1::with("first_reatlionship", "second_relationship")->get();

        return view('admin.crud1.index', compact('elements'));
    }

    ...

    public function create()
    {
        $first_reatlionship  = CRUD2::pluck("name", "id")->prepend('Please select', 0);
        $second_relationship = CRUD2::pluck("name", "id")->prepend('Please select', 0);

        return view('admin.crud1.create', compact('first_reatlionship', 'second_relationship'));
    }

    ...

    public function edit($id)
    {
        $element = CRUD1::find($id);

        $first_reatlionship  = CRUD2::pluck("name", "id")->prepend('Please select', 0);
        $second_relationship = CRUD2::pluck("name", "id")->prepend('Please select', 0);

        return view('admin.crud1.edit', compact('element', 'first_reatlionship', 'second_relationship'));
    }

    ...

In Model file:

    ...

    protected $fillable = [
        ...
        'first_reatlionship',
        'second_relationship',
        ...
    ];

    ...

    public function first_reatlionship()
    {
        return $this->hasOne('App\CRUD2', 'id', 'first_reatlionship');
    }

    public function second_relationship()
    {
        return $this->hasOne('App\CRUD2', 'id', 'second_relationship');
    }

    ...

In Requests directory files of this CRUD:

a) CreateCRUD1Request.php

    ...

    public function rules()
    {
        return [
            ...
            'first_reatlionship' => 'required', 
            'second_relationship' => 'required', 
            ...
        ];
    }

    ...

b) UpdateCRUD1Request.php

    ...

    public function rules()
    {
        return [
            ...
            'first_reatlionship' => 'required', 
            'second_relationship' => 'required', 
            ...
        ];
    }

    ...

In database migration file:

    ...

    public function up()
    {
        Model::unguard();
        Schema::create('websites',function(Blueprint $table){
            ...
            $table->integer("first_reatlionship")->references("id")->on("CRUD2");
            $table->integer("second_relationship")->references("id")->on("CRUD2");
            ...
        });
    }

    ...

In views files of this CRUD:

a) index.blade.php

    ... 
    <td>{{ isset($row->first_reatlionship->name) ? $row->first_reatlionship->name : '' }}</td>
    <td>{{ isset($row->second_relationship->name) ? $row->second_relationship->name : '' }}</td>
    ...

b) create.blade.php

    ...
    <div class="form-group">
        {!! Form::label('first_reatlionship', 'First Relationship*', array('class'=>'col-sm-2 control-label')) !!}
        <div class="col-sm-10">
        {!! Form::select('first_reatlionship', $first_reatlionship, old('first_reatlionship'), array('class'=>'form-control')) !!}

        </div>
    </div>
    <div class="form-group">
        {!! Form::label('second_relationship', 'Secondary Relationship*', array('class'=>'col-sm-2 control-label')) !!}
        <div class="col-sm-10">
        {!! Form::select('second_relationship', $second_relationship, old('second_relationship'), array('class'=>'form-control')) !!}

        </div>
    </div>
    ...

c) edit.blade.php

    ...
    <div class="form-group">
        {!! Form::label('first_reatlionship', 'First Relationship*', array('class'=>'col-sm-2 control-label')) !!}
        <div class="col-sm-10">
        {!! Form::select('first_reatlionship', $first_reatlionship, old('first_reatlionship',$CRUD1->first_reatlionship), array('class'=>'form-control')) !!}

        </div>
    </div>
    <div class="form-group">
        {!! Form::label('second_relationship', 'Secondary Relationship*', array('class'=>'col-sm-2 control-label')) !!}
        <div class="col-sm-10">
        {!! Form::select('second_relationship', $second_relationship, old('second_relationship',$CRUD1->second_relationship), array('class'=>'form-control')) !!}

        </div>
    </div>
    ...
PovilasKorop commented 7 years ago

@szymon-szychulski thanks a lot for the deep explanation. Yes, two relationship to one model is not supported in this package, we've added this functionality only to our online generator - at quickadminpanel.com