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();
}