<?php namespace App\Console\Commands; use Illuminate\Console\Command; use DB; class BookAdjustOne extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'book:adjustone {--bid= : the id of a book}'; /** * The console command description. * * @var string */ protected $description = '调整所有章节顺序prev_cid和next_cid'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $bid = $this->option('bid'); if(empty($bid)) return 0; $this->adjustSequentOne($bid); } /** * 调整单本书的顺序 * @param $bid */ public function adjustSequentOne($bid){ $chapter_list = DB::table('chapters')->orderBy('sequence')->where('bid',$bid)->select('id')->get(); $prev = 0; foreach ($chapter_list as $chapter){ if($prev){ DB::table('chapters')->where('id',$chapter->id)->update(['prev_cid'=>$prev]); DB::table('chapters')->where('id',$prev)->update(['next_cid'=>$chapter->id]); } $prev = $chapter->id; } } }