1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
<?php namespace App\Models; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Config; use Illuminate\Database\Eloquent\Model; use Carbon\Carbon; class Members extends Model{ protected $table = 'Test_Users'; public static $table_name = "Test_Users";//For static functions protected $primaryKey = 'id'; public $timestamps = true; const CREATED_AT = 'created_time'; const UPDATED_AT = 'updated_time'; //protected $dateFormat = 'M j Y h:i:s:000A'; //$member->toArray() will include these relations automatically. protected $with = array('user_type_data', 'certs'); const DT_TOTAL = 1; const DT_FILTERED = 2; //Fix for MSSQL Server public function getDateFormat() { return 'Y-m-d H:i:s.u'; } //Fix for MSSQL Server public function fromDateTime($value) { return substr(parent::fromDateTime($value), 0, -3); } static function dt_items($options, $return_type=0){ $result = DB::table(self::$table_name . ' as a') ->select( 'a.*' ); if($return_type == self::DT_TOTAL){ return $result->count(); } $keyword = val($options, 'keyword'); if($keyword != false){ $result = $result->where(function($query) use ($keyword){ $find = '%' . $keyword . '%'; $query->orWhere('a.user_name', 'like', $find); $query->orWhere('a.gid', 'like', $find); $query->orWhere('a.english_name', 'like', $find); }); } if($return_type == self::DT_FILTERED){ return $result->count(); } if(val($options, 'sort') != false){ $sorting = val($options, 'sort'); foreach ($sorting as $key => $value) { $result = $result->orderBy($key, $value); } }else{ $result = $result->orderBy('a.created_time', 'desc'); } $result = $result->skip($options['start']) ->take($options['limit']) ->get(); return $result; } static function datatable_sorting($columns, $order){ $sort = array(); foreach($order as $key => $item){ $col_index = $item['column']; $dir = $item['dir']; $column = null; $column_key = null; $i = 0; foreach ($columns as $key => $item) { if($i == $col_index){ $column = $item; $column_key = $key; break; } $i++; } $sort[$column_key] = $dir; } return $sort; } static function datatable_data($data){ $fields = Config::get('admin.member_table'); $new_data = array(); foreach($data as $item){ $item_values = []; foreach ($fields as $key => $field) { $value = val($item, $key); if($key == 'status'){ $value = trans('front.order_status.' . $item->status); } if($field[2] == 'date'){ $value = Carbon::parse($value)->toDateString(); } if($key == 'action'){ $value = '<div class="dropdown"> <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 選擇 <span class="caret"></span> </button> <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> <li><a href="#" class="action edit" data-id="' . $item->id . '">Edit</a></li>' . '<li><a href="#" class="action certs" data-id="' . $item->id . '">Certifications</a></li>' . '<li><a href="#" class="action delete" data-id="' . $item->id . '">Delete</a></li>'; $value .= '</ul> </div>'; } $item_values[] = $value; } array_push($new_data, $item_values); } return $new_data; } //By array static function new_data($data){ $data[self::CREATED_AT] = Carbon::now()->toDateTimeString(); $result = DB::table(self::$table_name) ->insertGetId($data); return $result; } //By array static function update_data($id, $data){ $data[self::UPDATED_AT] = Carbon::now()->toDateTimeString(); $result = DB::table(self::$table_name) ->where('id', $id) ->update($data); return $result; } //Relation. Function name must not in Member table columns. public function user_type_data() { return $this->belongsTo( 'App\Models\UserType', 'user_type'//default: usertype_id in table Test_Users ); } public function certs() { return $this->hasMany( 'App\Models\MemberCerts', //Foreign key in destination table //Default is "members_id" (by class name) 'user_id' ); } //https://laravel.com/docs/5.4/eloquent-relationships#one-to-many public function roles() { return $this->belongsToMany( 'App\Models\RoleM', // Map to role table 'user_roles', // Bridge table between users and roles 'user_id', // foreign key for table users in table user_roles 'user_group_id' // foreign key for table roles in table user_roles ); } } |
Notes to get belongsToMany Data
Do not just use this script to retrieve data:
1 |
Auth::user()->roles(); |
Correct script:
1 2 3 |
Auth::user()->roles()->get(); Auth::user()->roles()->first() |