public function checkGroupEditable($groupid, $userid) { $group = $this->model->find($groupid); $moderators = $group->moderators()->lists('user_id'); if (in_array($userid, $moderators)) { return true; } return false; }
public function delete() { $coso = self::$model->firstOrNew(rq('id')); if ($coso->exists) { $coso->update(); } else { } self::response($coso); }
/** * Resource edit action * PUT/PATCH /resource/{id} * @param int $id * @return Response */ public function update($id) { // Get all form data. $data = Input::all(); // Create validation rules $rules = array('email' => 'required|email|' . $this->unique('email', $id), 'password' => 'alpha_dash|between:6,16|confirmed', 'is_admin' => 'in:1'); // Custom validation message $messages = $this->validatorMessages; // Begin verification $validator = Validator::make($data, $rules, $messages); if ($validator->passes()) { // Verification success // Update resource $model = $this->model->find($id); $model->password = Input::get('password'); $model->email = Input::get('email'); $model->is_admin = (int) Input::get('is_admin', 0); if ($model->save()) { // Update success return Redirect::back()->with('success', '<strong>' . $this->resourceName . '更新成功:</strong>您可以继续编辑' . $this->resourceName . ',或返回' . $this->resourceName . '列表。'); } else { // Update fail return Redirect::back()->withInput()->with('error', '<strong>' . $this->resourceName . '更新失败。</strong>'); } } else { // Verification fail return Redirect::back()->withInput()->withErrors($validator); } }
/** * Action: Add resource images * @return Response */ public function postUpload($id) { $input = Input::all(); $rules = array('file' => 'image|max:3000'); $validation = Validator::make($input, $rules); if ($validation->fails()) { return Response::make($validation->errors->first(), 400); } $file = Input::file('file'); $destinationPath = 'uploads/travel/'; $ext = $file->guessClientExtension(); // Get real extension according to mime type $fullname = $file->getClientOriginalName(); // Client file name, including the extension of the client $hashname = date('H.i.s') . '-' . md5($fullname) . '.' . $ext; // Hash processed file name, including the real extension $picture = Image::make($file->getRealPath()); // crop the best fitting ratio and resize image $picture->fit(1024, 683)->save(public_path($destinationPath . $hashname)); $picture->fit(430, 645)->save(public_path('uploads/travel_small_thumbnails/' . $hashname)); $picture->fit(585, 1086)->save(public_path('uploads/travel_large_thumbnails/' . $hashname)); $model = $this->model->find($id); $oldThumbnails = $model->thumbnails; $model->thumbnails = $hashname; File::delete(public_path('uploads/travel_small_thumbnails/' . $oldThumbnails), public_path('uploads/travel_large_thumbnails/' . $oldThumbnails)); $models = new TravelPictures(); $models->filename = $hashname; $models->travel_id = $id; $models->user_id = Auth::user()->id; if ($model->save() && $models->save()) { return Response::json('success', 200); } else { return Response::json('error', 400); } }
/** * Resource edit action * PUT/PATCH /resource/{id} * @param int $id * @return Response */ public function update($id) { // Get all form data. $data = Input::all(); // Create validation rules $rules = array('name' => 'required|' . $this->unique('name', $id), 'sort_order' => 'required|integer'); // Custom validation message $messages = $this->validatorMessages; // Begin verification $validator = Validator::make($data, $rules, $messages); if ($validator->passes()) { // Verification success // Update resource $model = $this->model->find($id); $model->name = e($data['name']); $model->sort_order = e($data['sort_order']); if ($model->save()) { // Update success return Redirect::back()->with('success', '<strong>' . $this->resourceName . '更新成功:</strong>您可以继续编辑' . $this->resourceName . ',或返回' . $this->resourceName . '列表。'); } else { // Update fail return Redirect::back()->withInput()->with('error', '<strong>' . $this->resourceName . '更新失败。</strong>'); } } else { // Verification fail return Redirect::back()->withInput()->withErrors($validator); } }
/** * 资源编辑动作 * PUT/PATCH /resource/{id} * @param int $id * @return Response */ public function update($id) { // 获取所有表单数据. $data = Input::all(); // 创建验证规则 $rules = array('name' => 'required|' . $this->unique('name', $id), 'sort_order' => 'required|integer'); // 自定义验证消息 $messages = $this->validatorMessages; // 开始验证 $validator = Validator::make($data, $rules, $messages); if ($validator->passes()) { // 验证成功 // 更新资源 $model = $this->model->find($id); $model->name = e($data['name']); $model->sort_order = e($data['sort_order']); if ($model->save()) { // 更新成功 return Redirect::back()->with('success', '<strong>' . $this->resourceName . '更新成功:</strong>您可以继续编辑' . $this->resourceName . ',或返回' . $this->resourceName . '列表。'); } else { // 更新失败 return Redirect::back()->withInput()->with('error', '<strong>' . $this->resourceName . '更新失败。</strong>'); } } else { // 验证失败 return Redirect::back()->withInput()->withErrors($validator); } }
/** * 资源编辑动作 * PUT/PATCH /resource/{id} * @param int $id * @return Response */ public function update($id) { // 获取所有表单数据. $data = Input::all(); // 创建验证规则 $rules = array('email' => 'required|email|' . $this->unique('email', $id), 'password' => 'alpha_dash|between:6,16|confirmed', 'is_admin' => 'in:1'); // 自定义验证消息 $messages = $this->validatorMessages; // 开始验证 $validator = Validator::make($data, $rules, $messages); if ($validator->passes()) { // 验证成功 // 更新资源 $model = $this->model->find($id); $model->email = Input::get('email'); $model->is_admin = (int) Input::get('is_admin', 0); if ($model->save()) { // 更新成功 return Redirect::back()->with('success', '<strong>' . $this->resourceName . '更新成功:</strong>您可以继续编辑' . $this->resourceName . ',或返回' . $this->resourceName . '列表。'); } else { // 更新失败 return Redirect::back()->withInput()->with('error', '<strong>' . $this->resourceName . '更新失败。</strong>'); } } else { // 验证失败 return Redirect::back()->withInput()->withErrors($validator); } }
/** * Resource edit action * PUT/PATCH /resource/{id} * @param int $id * @return Response */ public function update($id) { // Get all form data. $data = Input::all(); // Create validation rules $rules = array('title' => 'required|' . $this->unique('title', $id), 'slug' => 'required|' . $this->unique('slug', $id), 'content' => 'required', 'excerpt' => 'required', 'category' => 'exists:article_categories,id'); // Custom validation message $messages = $this->validatorMessages; // Begin verification $validator = Validator::make($data, $rules, $messages); if ($validator->passes()) { // Verification success // Update source $model = $this->model->find($id); $model->category_id = $data['category']; $model->title = e($data['title']); $model->slug = e($data['slug']); $model->article_icon = e($data['article_icon']); $model->content = e($data['content']); $model->excerpt = e($data['excerpt']); $model->meta_title = e($data['meta_title']); $model->meta_description = e($data['meta_description']); $model->meta_keywords = e($data['meta_keywords']); if ($model->save()) { // Update success return Redirect::back()->with('success', '<strong>' . $this->resourceName . '更新成功:</strong>您可以继续编辑' . $this->resourceName . ',或返回' . $this->resourceName . '列表。'); } else { // Update fail return Redirect::back()->withInput()->with('error', '<strong>' . $this->resourceName . '更新失败。</strong>'); } } else { // Verification fail return Redirect::back()->withInput()->withErrors($validator); } }
/** * 资源编辑动作 * PUT/PATCH /resource/{id} * @param int $id * @return Response */ public function update($id) { // 获取所有表单数据. $data = Input::all(); // 创建验证规则 $rules = array('title' => 'required|' . $this->unique('title', $id), 'slug' => 'required|' . $this->unique('slug', $id), 'content' => 'required', 'category' => 'exists:article_categories,id'); // 自定义验证消息 $messages = $this->validatorMessages; // 开始验证 $validator = Validator::make($data, $rules, $messages); if ($validator->passes()) { // 验证成功 // 更新资源 $model = $this->model->find($id); $model->category_id = $data['category']; $model->title = e($data['title']); $model->slug = e($data['slug']); $model->content = e($data['content']); $model->meta_title = e($data['meta_title']); $model->meta_description = e($data['meta_description']); $model->meta_keywords = e($data['meta_keywords']); if ($model->save()) { // 更新成功 return Redirect::back()->with('success', '<strong>' . $this->resourceName . '更新成功:</strong>您可以继续编辑' . $this->resourceName . ',或返回' . $this->resourceName . '列表。'); } else { // 更新失败 return Redirect::back()->withInput()->with('error', '<strong>' . $this->resourceName . '更新失败。</strong>'); } } else { // 验证失败 return Redirect::back()->withInput()->withErrors($validator); } }
/** * Mock some of the internals of DB connection * used for proper DateTime format. Assuming MySQL, but * any can be used, as long as its DateTIme format is consistent */ protected function _mockConnection() { Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver = m::mock('Illuminate\\Database\\ConnectionResolverInterface')); $resolver->shouldReceive('connection')->andReturn($mockConnection = m::mock('Illuminate\\Database\\ConnectionInterface')); $mockConnection->shouldreceive('getPostProcessor')->andReturn(m::mock('Illuminate\\Database\\Query\\Processors\\Processor')); $mockConnection->shouldReceive('getQueryGrammar')->andReturn($queryGrammar = m::mock('Illuminate\\Database\\Query\\Grammars\\Grammar')); $queryGrammar->shouldReceive('getDateFormat')->andReturn('Y-m-d H:i:s'); }
/** * Action: Customer checkout order * @return Response */ public function checkout() { if (Input::get('id')) { $product_order = $this->model->find(Input::get('id')); $product_order->is_checkout = true; $product_order->save(); return Redirect::back()->with('success', '确认收货成功!欢迎再次使用时光碎片尚品汇购物。'); } else { return Redirect::back()->with('error', '确认收货失败,请重新尝试。'); } }
/** * Resource destory action * DELETE /resource/{id} * @param int $id * @return Response */ public function destroy($id) { $data = $this->model->find($id); if (is_null($data)) { return Redirect::back()->with('error', '没有找到对应的' . $this->resourceName . '。'); } elseif ($data->delete()) { return Redirect::back()->with('success', $this->resourceName . '删除成功。'); } else { return Redirect::back()->with('warning', $this->resourceName . '删除失败。'); } }
public function destroy($id) { if ($this->readOnly) { return $this->response->build(['error' => 'method_not_allowed'], SymfonyResponse::HTTP_METHOD_NOT_ALLOWED); } $entity = $this->model->find($id); if (!$entity) { return $this->response->build(['error' => 'not_found'], SymfonyResponse::HTTP_NOT_FOUND); } $entity->delete(); return $this->response->build(null, SymfonyResponse::HTTP_NO_CONTENT); }
/** * Get the cache key for save the token * * @param void * @return string */ protected function getCacheKey() { $model = new \ReflectionClass($this->model); $namespace = Str::slug($model->getNamespaceName()); $class = Str::slug($model->getShortName()); $type = Str::slug($this->type); $key = $this->model->getKey(); if (empty($key)) { throw new Exception("[Token] Error: Empty value for " . $model->getShortName() . "'s attribute '" . $this->model->getKeyName() . "'.", 1); } return 'malahierba-token' . '---' . $namespace . '---' . $class . '---' . $type . '---' . $key; }
/** * Get the path to the compiled version of a view. * * @param Illuminate\Database\Eloquent\Model $model * @return string */ public function getCompiledPath($model) { /* * A unique path for the given model instance must be generated * so the view has a place to cache. The following generates a * path using almost the same logic as Blueprint::createIndexName() * * e.g db_table_name_id_4 */ $field = $this->config->get('db-blade-compiler.model_property'); $path = 'db_' . $model->getTable() . '_' . $model->{$field} . '_'; if (is_null($model->primaryKey)) { $path .= $model->id; } else { if (is_array($model->primaryKey)) { $path .= implode('_', $model->primaryKey); } else { $path .= $model->primaryKey; } } $path = strtolower(str_replace(array('-', '.'), '_', $path)); return $this->cachePath . '/' . md5($path); }
/** * Show search result * @return response */ public function search() { $query = $this->model->orderBy('created_at', 'desc'); $categories = JobCategories::orderBy('sort_order')->get(); // Get search conditions switch (Input::get('target')) { case 'title': $title = Input::get('like'); break; } // Construct query statement isset($title) and $query->where('title', 'like', "%{$title}%")->orWhere('content', 'like', "%{$title}%"); $datas = $query->paginate(6); return View::make('job.search')->with(compact('datas', 'categories')); }
/** * Action: Delete resource images * @return Response */ public function deleteUpload($id) { // Only allow delete operations to the picture on the cover of the current resource $model = $this->model->find($id); $thumbnails = $model->thumbnails; if (is_null($thumbnails)) { return Redirect::back()->with('error', '没有找到对应的图片'); } elseif ($thumbnails) { File::delete(public_path('uploads/job_category_thumbnails/' . $thumbnails)); $model->thumbnails = NULL; $model->save(); return Redirect::back()->with('success', '图片删除成功。'); } else { return Redirect::back()->with('warning', '图片删除失败。'); } }
/** * Action: Delete resource images * @return Response */ public function deleteUpload($id) { // Delete picture of current post $model = $this->model->find($id); $thumbnails = $model->thumbnails; if (is_null($thumbnails)) { return Redirect::back()->with('error', '没有找到对应的图片'); } elseif ($thumbnails) { File::delete(public_path('uploads/category_thumbnails/' . $thumbnails)); $model->thumbnails = NULL; $model->save(); return Redirect::back()->with('success', '图片删除成功。'); } else { return Redirect::back()->with('warning', '图片删除失败。'); } }
/** * Get the hydrated models without eager loading. * * @param array $columns * @return array */ public function getModels($columns = array('*')) { // First, we will simply get the raw results from the query builders which we // can use to populate an array with Eloquent models. We will pass columns // that should be selected as well, which are typically just everything. $results = $this->query->get($columns); $connection = $this->model->getConnectionName(); $models = array(); // Once we have the results, we can spin through them and instantiate a fresh // model instance for each records we retrieved from the database. We will // also set the proper connection name for the model after we create it. foreach ($results as $result) { $models[] = $model = $this->model->newExisting(); $model->setRawAttributes((array) $result, true); $model->setConnection($connection); } return $models; }
/** * Action: Delete resource images * @return Response */ public function deleteUpload($id) { // Only allows you to share pictures on the cover of the current resource being deleted $filename = ProductPictures::where('id', $id)->where('user_id', Auth::user()->id)->first(); $oldImage = $filename->filename; $model = $this->model->find($filename->product_id); $oldThumbnails = $model->thumbnails; if (is_null($filename)) { return Redirect::back()->with('error', '没有找到对应的图片'); } elseif ($filename->delete()) { destoryUploadImages($this->destinationPath, $oldImage); if ($oldImage == $oldThumbnails) { $model->thumbnails = NULL; $model->save(); destoryUploadImages($this->thumbnailsPath, $oldThumbnails); } return Redirect::back()->with('success', '图片删除成功。'); } else { return Redirect::back()->with('warning', '图片删除失败。'); } }
<?php if (!defined('BASEPATH')) { exit('No direct script access allowed'); } $CI =& get_instance(); $config = $CI->db; // Get the DB object // https://gist.github.com/4417395 $resolver = new Illuminate\Database\ConnectionResolver(); $resolver->setDefaultConnection('default'); $factory = new Illuminate\Database\Connectors\ConnectionFactory(); $connection = $factory->make(array('host' => $config->hostname, 'database' => $config->database, 'username' => $config->username, 'password' => $config->password, 'collation' => $config->dbcollat, 'driver' => 'mysql', 'prefix' => $config->dbprefix)); $resolver->addConnection('default', $connection); Illuminate\Database\Eloquent\Model::setConnectionResolver($resolver);
/** * Get Model by id. * * @param int $id * @return App\Models\Model */ public function getById($id) { return $this->model->findOrFail($id); }
/** * Remove the specified resource from storage * * @api * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { $this->item = call_user_func($this->model . '::findOrFail', $id); $this->beforeDestroy(); $this->item->delete(); return $this->redirector->getRedirect('destroy')->with('success', $this->name_singular . ' has been deleted.'); }
public function __construct(array $attributes = array()) { static::$booted[get_class($this)] = true; parent::__construct($attributes); }
/** * View: User timeline * @return Respanse */ public function getTimeline($id) { $timeline = $this->model->where('user_id', $id)->paginate(8); $user = User::where('id', $id)->first(); return View::make('timeline.timeline')->with(compact('timeline', 'user')); }
/** * Remove a selected record. * * @param string $key * @return boolean */ public function remove($key) { $record = $this->model->where($this->model->getKeyName(), $key)->findOrFail($key); return $record->delete(); }
/** * Action: Show page post action * @return Response */ public function postAction($slug) { $postComment = e(Input::get('postComment')); if ($postComment) { // Get comment $content = e(Input::get('content')); // Check word if (mb_strlen($content) < 3) { return Redirect::back()->withInput()->withErrors($this->messages->add('content', '评论不得少于3个字符。')); } // Find article $product = $this->model->where('slug', $slug)->first(); // Create comment $comment = new ProductComment(); $comment->content = $content; $comment->product_id = $product->id; $comment->user_id = Auth::user()->id; if ($comment->save()) { // Create success // Updated comments $product->comments_count = $product->comments->count(); $product->save(); // Return success return Redirect::back()->with('success', '评论成功。'); } else { // Create fail return Redirect::back()->withInput()->with('error', '评论失败。'); } } else { $data = Input::all(); $rules = array('quantity' => 'required|integer', 'product_id' => 'required', 'price' => 'required', 'seller_id' => 'required', 'inventory' => 'required'); if (e($data['inventory']) < e($data['quantity'])) { return Redirect::back()->with('error', '<strong>请输入正确的' . $this->resourceName . '购买数量</strong>'); } elseif (Auth::user()->id == e($data['seller_id'])) { return Redirect::back()->with('error', '<strong>您不能购买自己出售的商品</strong>'); } else { // Custom validation message $messages = $this->validatorMessages; // Begin verification $validator = Validator::make($data, $rules, $messages); if ($validator->passes()) { // Verification success // Add recource $model = new ShoppingCart(); $model->buyer_id = Auth::user()->id; $model->quantity = e($data['quantity']); $model->product_id = e($data['product_id']); $model->price = e($data['price']); $model->payment = e($data['quantity']) * e($data['price']); $model->seller_id = e($data['seller_id']); if ($model->save()) { // Add success return Redirect::back()->with('success', '<strong>' . $this->resourceName . '已添加到购物车:</strong>您可以继续选购' . $this->resourceName . ',或立即结算。'); } else { // Add fail return Redirect::back()->withInput()->with('error', '<strong>' . $this->resourceName . '添加到购物车失败。</strong>'); } } else { // Verification fail return Redirect::back()->withInput()->withErrors($validator); } } } }
public function __set($key, $value) { $this->model->setAttribute($key, $value); }
public function getSent() { return $this->model->whereNotNull('sent_at')->get(); }
/** * Get a new instance * * @param array $attributes * * @return \Illuminate\Database\Eloquent\Model */ public function instance(array $attributes = []) { return $this->model->newInstance($attributes); }