<file_sep><?php

return [
	"addtitle" 	=> "New Pet",
	"add" 		=> "Add a new Pet",
	"petname" 	=> "<NAME>",
	"kind" 		=> "Kind",
	"type" 		=> "Type",
	"sex" 		=> "Sex",
	"born" 		=> "Born",
	"tall" 		=> "Tall",
	"weight" 	=> "Weight",
	"pet_users" 	=> "Pet Users",
];
<file_sep><?php namespace App;
use DB;
use Illuminate\Database\Eloquent\Model;

class Pet extends Model {
	
	public function users() {
		return $this->belongsToMany('App\User')->withPivot('admin');
	}

	public function kind() {
		return $this->belongsTo('App\Kind');
	}

	public function type() {
		return $this->belongsTo('App\Type');
	}

	public function recodes() {
		return $this->hasMany('App\Recode')->orderBy('created_at','desc');
	}

	public function admin($user_id) {
		$result = DB::select('select admin from pet_user where `pet_id` = ? and `user_id` = ? ', [$this->id,$user_id]);
		return $result[0]->admin;
	}


	/**
	 * To check this dont have other reservation 
	 *
	 * @var boolean
	 */
	public function reserved($date) {
		echo $date;
		$status = 5;
		$result = DB::select("select * from recodes where `pet_id` = ? AND `service_date` = ? AND `status` != ?", [$this->id,$date,$status]);
		
		if($result)
			return true;
		else
			return false;
	}
}
<file_sep>folk from Laravel Framework 5  Bootstrap 3 Starter Site<file_sep><?php namespace App;

use Illuminate\Database\Eloquent\Model;
use Eloquent;
class Recode extends Model {

	// protected $pet;
	// protected $auth;
	static public $status = [
				0 =>'預約中',
				1 =>'預約成功',
				2 =>'處理中',
				3 =>'完成',
				4 =>'已領走',
				5 =>'取消',
				6 =>'逾時系統取消',
				7 =>'預約已滿',
			];
	static public $services =[
				0=>'cut',
				1=>'wash',
				2=>'cut and wash',
			];

	static public $payment_method = [
				0=>'cash',
				1=>'cash 23',
			];

	static public $max_reserve_count = 4;

	public function pet(){
		return $this->belongsTo('App\Pet');
	}

	public function user(){
		return $this->belongsTo('App\User');
	}

	/**
	*	update recode status, (not necessary
	*
	*@return boolean
	*/
	public function update_status($status) {
		$this->status = $status;
		$this->save();
	}
}
<file_sep><?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

use App\User;
use App\Pet;
use App\Recode;
use App\Kind;
use App\Type;
Route::get('test', function(){
	// $user = new User;
	// $user->name = 'test1';
	// $user->username = 'test1';
	// $user->email = '<EMAIL>';
	// $user->save();

	// $Type = new Type;
	// $Type->name = '黃金';
	// $Type->kind_id = 1;
	// $Type->save();

	$pet = Pet::find(1);
	// echo '================<pre>';
	// var_dump($pet->users);
	// echo '</pre>================';
	echo '================<pre>';
	$kind = $pet->kind;
	var_dump($pet->kind->name);
	echo '</pre>================';
	echo '================<pre>';
	$type = $pet->type;
	var_dump($type->name);
	var_dump($pet->type->name);
	echo '</pre>================';
	exit;
});


Route::get('/', 'HomeController@index');
Route::get('home', 'HomeController@index');
Route::get('about', 'PagesController@about');
Route::get('contact', 'PagesController@contact');

Route::pattern('id', '[0-9]+');
Route::get('news/{id}', 'ArticlesController@show');
Route::get('video/{id}', 'VideoController@show');
Route::get('photo/{id}', 'PhotoController@show');

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => '<PASSWORD>',
]);

Route::controllers(['pet' => 'PetController']);

Route::controllers(['recode' => 'RecodeController']);

if (Request::is('admin/*'))
{
    require __DIR__.'/admin_routes.php';
}
<file_sep><?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePetsTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('pets', function(Blueprint $table)
		{
			$table->increments('id');
			$table->string('name');
			$table->integer('kind_id');		//1=dog,2=cat
			$table->integer('type_id');		//1=mixd,2=maltese..others
			$table->string('sex');		
			$table->date('born');
			$table->integer('tall');
			$table->integer('weight');
			$table->boolean('status')->default(1);
			$table->timestamps();
		});

		//many to many rel
		Schema::create('pet_user', function(Blueprint $table)
		{
			$table->integer('user_id');
			$table->integer('pet_id');
			$table->boolean('admin');
		});
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::dropIfExists('pets');
		Schema::dropIfExists('pet_user');
	}

}
<file_sep><?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Pet;
use App\User;
use App\Kind;
use App\Type;
use App\Recode;

use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;

class PetController extends Controller {

	protected $pet;
	protected $auth;

	public function getIndex(Guard $auth)
	{
		$user = $auth->user();
		$mypets = $user->pets;
		

		return view('pet.index',compact('mypets'));
	}

	public function getCreate()
	{
		//kind
		$kinds = Kind::all();
		$types = Type::all();
		return view('pet.create',compact('kinds','types'));
	}

	public function postCreate(Request $request,Guard $auth)
	{
		/**
		 *	1.add pet 	
		 *	2.add the relationship user_pet	
		 */
		
		$this->pet = new Pet;
		$this->pet->name 	= $request->input('petname');
		$this->pet->kind_id 	= $request->input('kind_id');
		$this->pet->type_id 	= $request->input('type_id');
		$this->pet->sex 	= $request->input('sex');
		$this->pet->born 	= date("Y-m-d",strtotime($request->input('born')));
		$this->pet->tall 	= $request->input('tall');
		$this->pet->weight 	= $request->input('weight');
		$this->pet->save();

		$this->auth = $auth;
		$this->pet->users()->attach($this->auth->user()->id,['admin' => 1]);
		return redirect($this->redirectPath());
	}

	public function getShow(Guard $auth,$id)
	{
		$pet = Pet::find($id);
		$admin = $pet->admin($auth->user()->id);

		$status = Recode::$status;
		$services = Recode::$services;
		$payment_method = Recode::$payment_method;
		return view('pet.show',compact('pet','admin','status','services','payment_method'));
	}

	public function getAdduser($id,Guard $auth)
	{
		$pet = Pet::find($id);
		
		$admin = $pet->admin($auth->user()->id);
		if($admin){
			$users = User::all();
			foreach ($users as $index => $user) {
				foreach ($pet->users as $selected_user) {
					if($user->id == $selected_user->id){
						unset($users[$index]);
						break;
					}
				}
			}
		}else {
			$user = array();
		}

		return view('pet.adduser',compact('pet','users','admin'));
	}


	public function postAdduser(Request $request,$id)
	{
		$this->pet = Pet::find($id);
		$this->pet->users()->attach($request->input('user_id'));
		return redirect($this->redirectPath());
	}

	public function getDeluser($id,Guard $auth)
	{
		$pet = Pet::find($id);
		
		$admin = $pet->admin($auth->user()->id);
		
		$users = array();
		foreach ($pet->users as $user) {
			if($user->id != $auth->user()->id) {
				$users[]  = $user;
			}
		}
		
		return view('pet.deluser',compact('pet','users','admin'));
	}


	public function postDeluser(Request $request,$id)
	{
		$this->pet = Pet::find($id);
		$this->pet->users()->detach($request->input('user_id'));
		return redirect($this->redirectPath());
	}

	public function getEdit()
	{
		// $users = User::all();
		// return view('pet.edituser',compact('users'));
	}


	public function postEdit()
	{
		// $this->pet->users()->attach($this->auth->user()->id);
		// return redirect($this->redirectPath());
	}

	/**
	 * Get the post register / login redirect path.
	 *
	 * @return string
	 */
	public function redirectPath()
	{
		if (property_exists($this, 'redirectPath'))
		{
			return $this->redirectPath;
		}

		return property_exists($this, 'redirectTo') ? $this->redirectTo : '/pet';
	}
}
<file_sep><?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRecodesTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('recodes', function(Blueprint $table)
		{
			$table->increments('id');
			$table->integer('user_id');	//foreign key from user
			$table->integer('pet_id');	//foreign key from pet
			$table->integer('status');
			$table->integer('service');
			$table->integer('price');
			$table->integer('payment');	//how to pay ,now should be only cash
			$table->boolean('paied');
			$table->date('service_date');	//how to pay ,now should be only cash
			$table->timestamps();
		});
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('recodes');
	}

}
<file_sep><?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Pet;
use App\User;
use App\Recode;

use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;

use Illuminate\Support\Facades\Session;
class RecodeController extends Controller {

	

	public function getIndex(Guard $auth)
	{
		$user = $auth->user();
		$status = Recode::$status;
		$services = Recode::$services;
		$payment_method = Recode::$payment_method;
		return view('recode.index',compact('user','status','services','payment_method'));
	}

	public function getCreate(Guard $auth)
	{
		$pets = $auth->user()->pets;
		$services = Recode::$services;
		$payment_method = Recode::$payment_method;
		return view('recode.create',compact('pets','services','payment_method'));
	}

	public function postCreate(Request $request,Guard $auth)
	{
		/**
		 *	add recode
		 */

		$recode = new Recode;
		$recode->user_id	= $auth->user()->id;
		$recode->pet_id 	= $request->input('pet_id');
		$recode->service 	= $request->input('service');
		$recode->payment 	= $request->input('payment');	
		$recode->service_date 	= date("Y-m-d",strtotime($request->input('service_date')));
		$recode->status 	= array_search('預約中',Recode::$status);
		

		/**
		*	service_date is in 28 days
		*/
		if(strtotime($recode->service_date) > strtotime("+28 day",strtotime(date("Y-m-d")))) {
			return redirect($this->redirectPath())->with("error"," Chosen date needs in 28 days");
		}	


		/**
		*	check the selected pet dont have other alive reserve today
		*/
		$pet = Pet::find($recode->pet_id);	
		if($pet->reserved($recode->service_date))
			return redirect($this->redirectPath())->with("error","$pet->name already has a reservetion on $recode->service_date. ");

		$recode->save();	
		return redirect($this->redirectPath());
	}

	
	/*
	*  use to cancel a recode
	*/
	public function anyCancel($recode_id,Guard $auth)
	{
		$recode  = Recode::find($recode_id);
		if($recode->user_id != $auth->user()->id || !in_array($recode->status, array(array_search('預約中',Recode::$status), array_search('預約成功',Recode::$status)))) 
			return redirect('recode')->with('error', 'You can not cancel this ! ');
		
		/*
			change status to cancel
		*/
		$recode->status = array_search("取消", Recode::$status);
		$recode->save();

		return redirect('recode')->with('success', 'Your Reserve is Canceled! ');
	}

	/*
	* won't use this
	*/
	public function getShow($id,Guard $auth)
	{
		$pet = Pet::find($id);
		$admin = $pet->admin($auth->user()->id);

		return view('pet.show',compact('pet','admin'));
	}

	public function getEdit($recode_id,Guard $auth)
	{
		$recode = Recode::find($recode_id);
		if(empty($recode))
			return redirect('recode')->with('error', 'No Such Recode !');

		if($recode->user_id != $auth->user()->id || !in_array($recode->status,array(array_search('預約中',Recode::$status),array_search('預約成功',Recode::$status))))
			return redirect('recode')->with('error', 'You can not edit this recode! ');

		$services = Recode::$services;
		$payment_method = Recode::$payment_method;
		return view('recode.edit',compact('recode','services','payment_method'));
	}


	public function postEdit(Request $request,Guard $auth,$recode_id)
	{
		if($recode->user_id != $auth->user()->id) 
			return redirect('recode')->with('error', 'You can not edit this recode! ');
		
		$recode = Recode::find($recode_id);
		if(empty($recode))
			return redirect('/admin/recodes/'.date('Y-m-d'))->with('error','No Such Recode');	

		$recode->service 	= $request->input('service');
		$recode->payment 	= $request->input('payment');	
		$recode->service_date 	= date("Y-m-d",strtotime($request->input('service_date')));

		return redirect($this->redirectPath());
	}

	public function getAdmin(Guard $auth,$date='')
	{
		if($auth->user()->admin != 1 )
			return redirect($this->redirectPath());	
		if(!preg_match("/[0-9]{4}-[0-9]{2}-[0-9]{2}/",$date)) 
			return redirect("/admin/recodes/".date('Y-m-d'));	
		
		$recodes = Recode::where("service_date","=",$date)->get();
		$status 	= Recode::$status;
		$services = Recode::$services;
		$payment_method = Recode::$payment_method;

		return view('recode.admin',compact('recodes','status','services','payment_method','date'));
	}

	public function getAdminedit(Guard $auth,$recode_id)
	{
		if($auth->user()->admin != 1)
			return redirect($this->redirectPath());	

		$recode = Recode::find($recode_id);
		if(empty($recode))
			return redirect('/admin/recodes/'.date('Y-m-d'))->with('error','No Such Recode');	
		if(in_array($recode->status,array(4,5,6)))
			return redirect('/admin/recodes/'.date('Y-m-d'))->with('error','You can not edit a finish recode');
		$services = Recode::$services;
		$payment_method = Recode::$payment_method;
		$status = Recode::$status;
		return view('recode.adminedit',compact('recode','services','payment_method','status'));
	}

	public function postAdminedit(Request $request,Guard $auth,$recode_id)
	{
		if($auth->user()->admin != 1)
			return redirect($this->redirectPath());	

		$recode = Recode::find($recode_id);
		if(empty($recode))
			return redirect('/admin/recodes/'.date('Y-m-d'))->with('error','No Such Recode');	
		
		$recode->service 	= $request->input('service');
		$recode->payment 	= $request->input('payment');	
		$recode->service_date 	= date("Y-m-d",strtotime($request->input('service_date')));

		/*admin only*/
		$recode->status 	= $request->input('status');
		$recode->price 	 	= $request->input('price');
		$recode->paied 	= $request->input('paied');

		if($recode->save())
			return  redirect('/admin/recodes/'.$recode->service_date)->with('success','Edit Success');
		else
			return  redirect('/admin/recodes/'.$recode->service_date)->with('error','Something wrong  when you edit recode');
	}

	public function getAdmincreate(Guard $auth)
	{
		if($auth->user()->admin != 1)
			return redirect($this->redirectPath());	
		
		$services = Recode::$services;
		$payment_method = Recode::$payment_method;
		
		//just show the status we need
		$status = array();
		$status[1] = Recode::$status[1];
		$status[2] = Recode::$status[2];
		$status[3] = Recode::$status[3];
		$status[4] = Recode::$status[4];
		$status[5] = Recode::$status[5];

		return view('recode.admincreate',compact('services','payment_method','status'));
	}

	public function postAdmincreate(Request $request,Guard $auth)
	{
		if($auth->user()->admin != 1)
			return redirect($this->redirectPath());	
		
		$recode = new Recode;		
		$recode->user_id 	= $auth->user()->id;
		$recode->pet_id 	= $request->input('pet_id');
		$recode->service 	= $request->input('service');
		$recode->payment 	= $request->input('payment');	
		$recode->service_date 	= date("Y-m-d",strtotime($request->input('service_date')))	;

		/*admin only*/
		$recode->status 	= $request->input('status');
		$recode->paied 	= ($request->input('paied')) ?1:0;

		if($recode->save())
			return  redirect('/admin/recodes/'.$recode->service_date)->with('success','Create Success');
		else
			return  redirect('/admin/recodes/'.$recode->service_date)->with('error','Something wrong  when you create recode');
	}

	public function postFinduser(Request $request,Guard $auth)
	{
		if($auth->user()->admin != 1)
			return redirect($this->redirectPath());	
		
		$method 	= $request->input('method');
		$data 		= $request->input('data');
		
		if(empty($data))
			return json_encode(array('msg'=>'no data to search'));


		$users = User::where($method,"like","%$data%")->get();
		if(empty($users))
			return json_encode(array('msg'=>'no such user'));
	
		return json_encode($users);
	}

	public function postFindpet(Request $request,Guard $auth)
	{
		if($auth->user()->admin != 1)
			return redirect($this->redirectPath());	
		
		$user_id = $request->input('user_id');
		$user = User::find($user_id);

		if(empty($user_id))
			return json_encode(array('msg'=>'no user selected or no such user'));

		$pets = $user->pets;

		if(empty($pets))
			return json_encode(array('msg'=>'no pet for this user'));

		return json_encode($pets);
	}

	public function getAdminreceive(Guard $auth)
	{
		if($auth->user()->admin != 1)
			return redirect($this->redirectPath());	
		
		$services = Recode::$services;
		
		//just show the status we need
		$status = array();
		$status[1] = Recode::$status[1];
		$status[7] = Recode::$status[7];

		//find all receive
		$date = date("Y-m-d");
		$receive_recodes = Recode::where("service_date",">",$date)->where("status","in",array(0,1))->get();
		
		$recodes = array();
		foreach ($receive_recodes as $recode) {
			$recodes[$recode->service_date][] = $recode;
		}
		
		ksort($recodes);

		//count each days receive num
		$date_count = array();
		foreach ($recodes as $date =>$daterecodes) {
			$date_count[$date] = Recode::where("status","=","1")->where("service_date","=",$date)->count();
		}

		return view('recode.adminreceive',compact('recodes','services','status','date_count'));
	}

	public function postAdminreceive(Request $request,Guard $auth)
	{
		if($auth->user()->admin != 1)
			return redirect($this->redirectPath());	
		
		$status 	= $request->input('status');
		$recode_ids	= $request->input('checked_recode');

		foreach($recode_ids as $recode_id) {
			$recode = Recode::find($recode_id);

			$recode->status = $status;
			$recode->save();
		}

		return  redirect('/admin/recodes/receive')->with('success','Receive Check Success');
	}



	public function postReserveStatus(Request $request,Guard $auth)
	{	
		// $user_id = $request->input('user_id');
		// $user = User::find($user_id);

		// if(empty($user_id))
		// 	return json_encode(array('msg'=>'no user selected or no such user'));

		// $pets = $user->pets;

		// if(empty($pets))
		// 	return json_encode(array('msg'=>'no pet for this user'));

		$sFirstDate 	= $request->input('first_date');
		$iNumber 	= $request->input('number');

		$sDate = $sFirstDate;
		$sToday = date("Y-m-d");
		$sEndDate = date("Y-m-d",strtotime("+28 day"));  //	能預約的最後一天
		$iMaxReserve = Recode::$max_reserve_count;
		$aReserveStatus = array();
		for ($i=0; $i < $iNumber; $i++) {
			$aReserveStatus[$i]['date'] = $sDate;

			if(strtotime($sDate)<=strtotime($sToday)) {
				$aReserveStatus[$i]['title'] = "已過期";
				$aReserveStatus[$i]['color'] = "#BDBDBD";
			} elseif(strtotime($sDate)>strtotime($sEndDate)) {
				$aReserveStatus[$i]['title'] = "未開放預約";
				$aReserveStatus[$i]['color'] = "#BDBDBD";
			} else{
				$iCount = Recode::where("service_date","=",$sDate)->where("status","in",array(0,1))->count();
				$iRemainNumber = $iMaxReserve - $iCount;//剩餘數量
				if($iRemainNumber<=0) {
					$aReserveStatus[$i]['title'] = "預約已滿";
					$aReserveStatus[$i]['color'] = "red";
				} elseif($iRemainNumber<=3) {
					$aReserveStatus[$i]['title'] = "尚可預約數量：".$iRemainNumber;
					$aReserveStatus[$i]['color'] = "yellow";
				} else {
					$aReserveStatus[$i]['title'] = "尚可預約";
					$aReserveStatus[$i]['color'] = "blue";
				}
			}

			$sDate = date("Y-m-d",strtotime("+1 day",strtotime($sDate)));
		}
		return json_encode($aReserveStatus);
	}

	/**
	 * Get the post register / login redirect path.
	 *
	 * @return string
	 */
	public function redirectPath()
	{
		if (property_exists($this, 'redirectPath'))
		{
			return $this->redirectPath;
		}

		return property_exists($this, 'redirectTo') ? $this->redirectTo : '/recode';
	}
}
<file_sep><?php

return [
	"create" 	=> "Create",
	"service" 	=> "Service",
	"reserve" 	=> "Reserve",
	"pet" 		=> "Pet",
	"payment_method" 	=> "Payment Method",
	"edit_title" 	=> "Edit Recode",
	"edit" 		=> "Edit ",
	"status" 	=> "Status ",
	"cancel" 	=> "Cancel ",
	"date" 		=> "Date ",
	"paied" 	=> "Paied ",
	"price" 		=> "Price ",
	"user" 		=> "User ",
	"search_user_data" 	=> "Search Data ",
	"search_user_by" 	=> "Search User By ",
	"phone" 	=> "Phone ",
	"name" 	=> "Name ",
	"service_date" 	=> "Service Date ",
	"create_at" 	=> "Create Time ",
];
<file_sep><?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Kind extends Model {
	public function types(){
		return $this->hasMany('App\Type');
	}
}
