public function create_belongsTo_relation_with(Request $request, Table $toModel) { $model = $this; $from_field_name = snake_case($toModel->model) . '_id'; if ($request->has('from_field_id') && $request->from_field_id != '') { $from_field_name = Field::find($request->from_field_id)->name; } //dd($model); // $from_field_name = snake_case($toModel->model).'_id'; $to_field_name = 'id'; if ($toModel->hasPrimaries()) { $to_field_name = $toModel->getPrimaries()->first()->name; } // old --- $to_field_name = 'id'; $field = $model->findOrCreate(['name' => $from_field_name, 'is_unsigned' => true, 'field_type_id' => 1]); $toField = $toModel->findOrCreate(['name' => $to_field_name, 'is_primary' => true, 'is_incrementable' => true, 'field_type_id' => 1]); // if ( ! $field->has_relation($toField)) { $field->relations()->attach($toField, ['from_field_name' => $field->name, 'to_field_name' => $toField->name, 'laravel_name' => $request->laravel_name, 'pivot_table_name' => '']); // } // else { // Flash::error('A relation Already exists beetween this 2 fields'); // return false; // } $toField->relations()->attach($field, ['from_field_name' => $toField->name, 'to_field_name' => $field->name, 'laravel_name' => 'hasMany', 'pivot_table_name' => '']); // if ( ! $toField->has_relation($field)) { // } // else { // Flash::error('A relation Already exists beetween this 2 fields'); // return false; // } }