class CreateRoomTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('rooms', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('price');
$table->timestamps();
$table->engine = 'InnoDB';
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
Schema::create('reserve_management_room', function (Blueprint $table) {
$table->increments('id');
$table->integer('reserve_management_id')
->foreign('reserve_management_id')
->references('id')->on('reserve_managements')
->onDelete('cascade');
$table->integer('room_id')
->foreign('room_id')
->references('id')->on('rooms')
->onDelete('cascade');
$table->timestamps();
$table->engine = 'InnoDB';
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('reserve_management_room');
Schema::dropIfExists('rooms');
}
}
このテーブルに対するCURD機能を作成します。
ここらへんは予約一覧がすでに出来上がっているので、これをベースにすれば簡単に出来上がります。
モデル
class Room extends Model
{
protected $table = 'rooms';
public function reserveManagement()
{
return $this->belongsToMany('App\Model\ReserveManagement');
}
}
リポジトリ
class RoomRepository
{
private $paramNames = ['name', 'price'];
/**
* 部屋一覧を取得する
*
* @return Room[]
*/
public function getList()
{
return Room::get();
}
/**
* 部屋を登録する
*
* @return void
*/
public function add($param)
{
$model = new Room;
foreach($this->paramNames as $name)
{
$model->$name = $param[$name];
}
$model->save();
}
/**
* 予約を更新する
*
* @return void
*/
public function updateById($id, $param)
{
$model = $this->getItemById($id);
foreach($this->paramNames as $name)
{
$model->$name = $param[$name];
}
$model->save();
}
/**
* 予約を削除する
*
* @return void
*/
public function deleteById($id)
{
$model = $this->getItemById($id);
$model->delete();
}
/**
* IDから部屋情報を1件取得する
*
* @return Room
*/
public function getItemById($id)
{
return Room::where(['id' => $id])->first();
}
public function getParam()
{
return $this->paramNames;
}
}
リクエスト
class RoomRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string',
'price' => 'required|numeric',
];
}
}
コントローラー
class RoomController extends Controller
{
protected $room;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->room = new RoomRepository();
}
/**
* 部屋一覧の表示
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('room.index', ['roomLists' => $this->room->getList()]);
}
/**
* 入力フォーム
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('room.create');
}
/**
* 登録処理
*/
public function store(RoomRequest $request)
{
$param = $this->room->getParam();
$this->room->add([
$param[0] => $request->name,
$param[1] => $request->price,
]);
return redirect('room');
}
/**
* 編集処理
*
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
return view('room.edit', ['item' => $this->room->getItemById($id)]);
}
/**
* 更新処理
*/
public function update(RoomRequest $request)
{
$param = $this->room->getParam();
$this->room->updateById($request->id,
[
$param[0] => $request->name,
$param[1] => $request->price,
]);
return redirect('room');
}
/**
* 削除確認
*
* @return \Illuminate\Http\Response
*/
public function conform($id)
{
return view('room.conform', ['item' => $this->room->getItemById($id)]);
}
/**
* 削除処理
*/
public function delete(Request $request)
{
$this->room->deleteById($request->id);
return redirect('room');
}
}
class AddSubmit extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('reserve_managements', function (Blueprint $table) {
$table->boolean('lodging');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('reserve_managements', function (Blueprint $table) {
$table->dropColumn('lodging');
});
}
}
/**
* 月毎に集計する
*/
public function countByMonthly()
{
return ReserveDayList::select(DB::raw('DATE_FORMAT(day, "%Y-%m") as yearmonth'), DB::raw('count(*) as count'), DB::raw('count(*) * 2000 as total'))
->groupby('yearmonth')
->get();
}
/**
* indexの月別表示
*
* @return \Illuminate\Http\Response
*/
public function indexToMonthly(Request $request)
{
return redirect('management?year='.$request->year.'&month='.$request->month);
}
クエリパラメータ付きURLを作成し、リダイレクトを行っています。
これしか方法が思い浮かばなかった。
次はリポジトリ。
/**
* 月別予約一覧を取得する
*
* @return ReserveManagement[]
*/
public function getListByMonth($year, $month)
{
return ReserveManagement::where('start_day', '>=', date('Y-m-d', strtotime('first day of '.$year.'-'.$month)))
->where('start_day', '<=', date('Y-m-d', strtotime('last day of '.$year.'-'.$month)))
->get();
}
class ManagementRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string',
'address' => 'required|string',
'phone' => 'required|digits:11',
'num' => 'required|numeric|digits_between:1,2',
'days' => 'required|numeric|digits_between:1,4',
'start_day' => 'required|date',
];
}
}
use App\Http\Requests\ManagementRequest;
/**
* 登録処理
*/
public function store(ManagementRequest $request)
/**
* 更新処理
*/
public function update(ManagementRequest $request)