使用Laravel的订单项逻辑

使用Laravel的订单项逻辑,laravel,laravel-5,laravel-5.7,Laravel,Laravel 5,Laravel 5.7,对于我的Laravel应用程序,我实现了一个排序功能。在选项列表中,我显示了两个向上和向下按钮,用于触发OptionController中的向上和向下功能,请参见下文 问题1 目前,我正在调整数据库中排序列的小数点30,15字段。我随机选择30,15。你能给我一个建议吗,哪个小数点?,?此排序字段的最佳值是多少 问题2 我想将上下逻辑移动到一个地方,在那里我可以在具有通用模型的不同控制器中使用它,例如Sort::up$models,$item。什么是放置这种逻辑的正确位置?服务辅助函数 问题3

对于我的Laravel应用程序,我实现了一个排序功能。在选项列表中,我显示了两个向上和向下按钮,用于触发OptionController中的向上和向下功能,请参见下文

问题1

目前,我正在调整数据库中排序列的小数点30,15字段。我随机选择30,15。你能给我一个建议吗,哪个小数点?,?此排序字段的最佳值是多少

问题2

我想将上下逻辑移动到一个地方,在那里我可以在具有通用模型的不同控制器中使用它,例如Sort::up$models,$item。什么是放置这种逻辑的正确位置?服务辅助函数

问题3

当我创建一个新项目时,例如下面示例中的选项,我需要将排序自动设置为最后一个项目的排序+1。当然,在存储控制器时,我可以在控制器中执行此操作,但是我可以将此逻辑放入模型本身吗?并且:我可以把这个逻辑放在哪里,以便在多个模型中使用它而不重复代码


你可以用一个特质来做这个。有关更多详细信息,请参阅

namespace App\Http\Controllers;


use App\Models\Option;
use App\Models\Attribute;

class OptionController extends Controller
{

    public function up($id, $attributeId) {
      $options = Attribute::findOrFail($attributeId)->options;
      $option = Option::findOrFail($id);

      foreach ($options as $index => $o) {

        // Search for the current position of the
        // option we have to move.
        if( $option->id == $o->id ) {

          // Will be first element?
          if( $index == 1) {

            // Set the sort to current first element sort - 1
            $option->sort = $options[0]->sort-1;

          } else if( $index > 1) {

            // Get the previous and the pre-previous items from the options
            $pre = $options[$index-1]->sort;
            $prepre = $options[$index-2]->sort;

            $diff = ($pre - $prepre) / 2;

            $option->sort = $prepre + $diff;
          }

          break;
        }
      }

      $option->save();

            Session::flash('message', __(':option moved up.', [ 'option' => $option->name ]));
      Session::flash('message-type', 'success');

      return redirect()->back();
    }

    public function down($id, $attributeId) {
      $options = Attribute::findOrFail($attributeId)->options;
      $option = Option::findOrFail($id);

      foreach ($options as $index => $o) {

        // Search for the current position of the
        // option we have to move.
        if( $option->id == $o->id ) {

          // Will be last element?
          if( $index == count($options)-2 ) {

            // Set the sort to current last element sort + 1
            $option->sort = $options[count($options)-1]->sort+1;

          } else if( $index < count($options)-2) { // ???

            // Get the previous and the pre-previous items from the options
            $next = $options[$index+1]->sort;
            $nextnext = $options[$index+2]->sort;

            $diff = ($nextnext - $next) / 2;

            $option->sort = $next + $diff;
          }

          break;
        }
      }

      $option->save();

      Session::flash('message', __(':option moved down.', [ 'option' => $option->name ]));
      Session::flash('message-type', 'success');

      return redirect()->back();
    }
}