{"id":1360,"date":"2015-01-31T16:58:36","date_gmt":"2015-01-31T08:58:36","guid":{"rendered":"http:\/\/blog.turn.tw\/?page_id=1360"},"modified":"2015-01-31T16:58:36","modified_gmt":"2015-01-31T08:58:36","slug":"eloquent-crud%ef%bc%9afind","status":"publish","type":"page","link":"https:\/\/blog.turn.tw\/?page_id=1360","title":{"rendered":"Eloquent CRUD\uff1afind"},"content":{"rendered":"<p>\u6488\u51fa\u4e00\u7b46\u8cc7\u6599\u6642\uff0cEloquent\u662f\u5982\u4f55\u5c07\u8cc7\u6599mapping\u6210\u7269\u4ef6\u7684\u5462\uff1f<\/p>\n<pre>\r\n$user = User::find(1);\r\n<\/pre>\n<p>\u8cc7\u6599\u5eab\u6488\u51fa\u4f86\u7684\u61c9\u8a72\u662f\u4e00\u5927\u5806rows\uff0c\u8f49\u6210PHP\u7684\u9663\u5217\u9084\u7b97\u5bb9\u6613\u60f3\u50cf\uff0c\u4f46\u600e\u9ebc\u8f49\u6210Eloquent\u5be6\u9ad4\u5462\uff1f<\/p>\n<p>\u6211\u5011\u4f86\u770b\u770bfind\u65b9\u6cd5\u80cc\u5f8c\u505a\u4e86\u4ec0\u9ebc\u3002<\/p>\n<pre>\r\n\/\/ Illuminate\\Database\\Eloquent\\Model.php\r\n\t\/**\r\n\t * Find a model by its primary key.\r\n\t *\r\n\t * @param  mixed  $id\r\n\t * @param  array  $columns\r\n\t * @return \\Illuminate\\Support\\Collection|static\r\n\t *\/\r\n\tpublic static function find($id, $columns = array('*'))\r\n\t{\r\n\t\tif (is_array($id) && empty($id)) return new Collection;\r\n\r\n\t\t$instance = new static;\r\n\r\n\t\treturn $instance->newQuery()->find($id, $columns);\r\n\t}\r\n<\/pre>\n<p>\u5225\u88abnew static\u5687\u5230\u4e86\uff0c\u5176\u5be6\u5c31\u662fnew static()\uff0c\u4e5f\u5c31\u662f\u547c\u53eb\u7576\u524d\u985e\u5225\u7684\u5efa\u69cb\u5f0f\u800c\u5df2\u3002<br \/>\n\u63a5\u8457\u770b\u770bnewQuery\u65b9\u6cd5\u3002<\/p>\n<pre>\r\n\/\/ Illuminate\\Database\\Eloquent\\Model.php\r\n\t\/**\r\n\t * Get a new query builder for the model's table.\r\n\t *\r\n\t * @return \\Illuminate\\Database\\Eloquent\\Builder\r\n\t *\/\r\n\tpublic function newQuery()\r\n\t{\r\n\t\t$builder = $this->newEloquentBuilder(\r\n\t\t\t$this->newBaseQueryBuilder()\r\n\t\t);\r\n\r\n\t\t\/\/ Once we have the query builders, we will set the model instances so the\r\n\t\t\/\/ builder can easily access any information it may need from the model\r\n\t\t\/\/ while it is constructing and executing various queries against it.\r\n\t\t$builder->setModel($this)->with($this->with);\r\n\r\n\t\treturn $this->applyGlobalScopes($builder);\r\n\t}\r\n\r\n<\/pre>\n<p>newBaseQueryBuilder\u6703\u56de\u50b3\\Illuminate\\Database\\Query\\Builder\u5be6\u9ad4\u3002<br \/>\n\u9019\u6bb5\u91cd\u9ede\u5c31\u662f\u62ff\\Illuminate\\Database\\Query\\Builder\u7576\u53c3\u6578\u50b3\u5165\uff0c\u88fd\u4f5c\u51fa\\Illuminate\\Database\\Eloquent\\Builder\u5be6\u9ad4\u3002<br \/>\n\u4e00\u500b\u5c08\u9580\u88fd\u9020Query\uff0c\u4e00\u500b\u5c08\u9580\u88fd\u4f5cEloquent\uff0c\u5728\u60f3\u50cf\u5b83\u5011\u7684\u6642\u5019\u662f\u5206\u958b\u7684\u3002<\/p>\n<p>\u63a5\u8457\u8981\u547c\u53eb\u9019Eloquent\\Builder\u7684find\u65b9\u6cd5\u3002<\/p>\n<pre>\r\n\/\/ \\Illuminate\\Database\\Eloquent\\Builder.php\r\n\t\/**\r\n\t * Find a model by its primary key.\r\n\t *\r\n\t * @param  mixed  $id\r\n\t * @param  array  $columns\r\n\t * @return \\Illuminate\\Database\\Eloquent\\Model|static|null\r\n\t *\/\r\n\tpublic function find($id, $columns = array('*'))\r\n\t{\r\n\t\tif (is_array($id))\r\n\t\t{\r\n\t\t\treturn $this->findMany($id, $columns);\r\n\t\t}\r\n\r\n\t\t$this->query->where($this->model->getQualifiedKeyName(), '=', $id);\r\n\r\n\t\treturn $this->first($columns);\r\n\t}\r\n\r\n<\/pre>\n<p>\u6211\u4e0d\u559c\u6b61\u7b2c\u4e00\u6bb5\u7684\u9632\u79a6\u6027\u7de8\u7a0b\u3002\u5b83\u82e5\u767c\u73fe\u50b3\u5165\u7684\u662f\u9663\u5217\u6703\u53bb\u547c\u53ebfindMany&#8230;\u3002\u9019\u6703\u5c0e\u81f4\u5176\u4ed6Laravel\u6846\u67b6\u958b\u767c\u8005\u6feb\u7528find\u65b9\u6cd5\u3001\u4e0d\u53bb\u5206\u6e05\u695afind\u8ddffindMany\u7684\u5dee\u5225\u3002\u9019\u662f\u5728\u8ff4\u907f\u6e9d\u901a\u4e0d\u826f\u7684\u554f\u984c\u3002<br \/>\n\u53c3\u95b1\uff1a<a href=\"http:\/\/danielroop.com\/blog\/2009\/10\/15\/why-defensive-programming-is-rubbish\/\">Why Defensive Programming is Rubbish<\/a><\/p>\n<p>\u63a5\u8457\u6703\u547c\u53ebDatabase\\Query\u5be6\u9ad4\uff08\u5b83\u5728\u524d\u9762\u7684\u65b9\u6cd5\u4e2d\u88ab\u7522\u751f\uff0c\u6211\u7701\u7565\u4e86\u9019\u90e8\u4efd\uff09\u7684where\u65b9\u6cd5\u3002<br \/>\n\u7528getQualifiedKeyName\u7522\u751f\u50cf\u662f&#8217;users.id&#8217;\u7684\u5b57\u4e32\u7d66where\u65b9\u6cd5\u3002<br \/>\n\u5176\u5be6\u5c31\u662f\u7528OOP\u7684\u65b9\u5f0f\u4ee5Database\\Query\u53bb\u5bebSQL\u3002<\/p>\n<p>\u6700\u5f8c\uff0c\u547c\u53ebfirst\u65b9\u6cd5\u3002<\/p>\n<pre>\r\n\/\/ Illuminate\\Database\\Eloquent\\Builder.php\r\n\t\/**\r\n\t * Execute the query and get the first result.\r\n\t *\r\n\t * @param  array  $columns\r\n\t * @return \\Illuminate\\Database\\Eloquent\\Model|static|null\r\n\t *\/\r\n\tpublic function first($columns = array('*'))\r\n\t{\r\n\t\treturn $this->take(1)->get($columns)->first();\r\n\t}\r\n\r\n<\/pre>\n<p>\u547c\u53eb\u4e86take\u65b9\u6cd5&#8230;\u4f46\u662fsource code\u6c92\u6709\u5b9a\u7fa9take\u65b9\u6cd5\u3002\u5b83\u5230\u5e95\u662f\u4ec0\u9ebc\uff1f<br \/>\n\u563f\u563f\uff0c\u5225\u5fd8\u4e86PHP\u6709__call\u9019\u500bmagic function\u3002<\/p>\n<pre>\r\n\/\/ Illuminate\\Database\\Eloquent\\Builder.php\r\n\t\/**\r\n\t * Dynamically handle calls into the query instance.\r\n\t *\r\n\t * @param  string  $method\r\n\t * @param  array   $parameters\r\n\t * @return mixed\r\n\t *\/\r\n\tpublic function __call($method, $parameters)\r\n\t{\r\n\t\tif (isset($this->macros[$method]))\r\n\t\t{\r\n\t\t\tarray_unshift($parameters, $this);\r\n\r\n\t\t\treturn call_user_func_array($this->macros[$method], $parameters);\r\n\t\t}\r\n\t\telseif (method_exists($this->model, $scope = 'scope'.ucfirst($method)))\r\n\t\t{\r\n\t\t\treturn $this->callScope($scope, $parameters);\r\n\t\t}\r\n\r\n\t\t$result = call_user_func_array(array($this->query, $method), $parameters);\r\n\r\n\t\treturn in_array($method, $this->passthru) ? $result : $this;\r\n\t}\r\n\r\n<\/pre>\n<p>\u771f\u6b63\u7684take\u51fa\u73fe\u5728\u5012\u6578\u7b2c\u4e8c\u884c\u3002\u4e5f\u5c31\u662fDatabase\\Query\u7684take\u3002<br \/>\n\u5b83\u53ea\u662f\u8981\u52a0\u5165SQL\u8a9e\u6cd5\u7684LIMIT\u800c\u5df2\u3002<\/p>\n<p>\u6ce8\u610f\u6700\u5f8c\u4e00\u884c\u3002\u53ea\u6709\u7576\u65b9\u6cd5\u5728passthru\u9663\u5217\u88e1\u9762\u624d\u6703\u56de\u50b3\u65b9\u6cd5\u7d50\u679c\uff0c\u4e0d\u7136\u90fd\u662f\u50b3\u56de\u81ea\u5df1\u3002<br \/>\ntake, get, first\u4e09\u500b\u65b9\u6cd5Database\\Query\u985e\u5225\u90fd\u6709\uff0c\u5c0e\u81f4\u9019\u6bb5\u770b\u4f3c\u4e00\u822c\u7684Method Chaining\uff0c\u5176\u5be6\u5b8c\u5168\u4e0d\u662f\u3002<br \/>\ntake\u662fQuery\\Builder\u7684\u3001get\u662fEloquent\\Builder\u7684\u3001first\u662fEloquent\\Collection\u7684\u3002<br \/>\n\u6240\u4ee5\u9019\u884c\uff1a<\/p>\n<pre>\r\n\t\treturn $this->take(1)->get($columns)->first();\r\n<\/pre>\n<p>\u5176\u5be6\u7b49\u65bc\uff1a<\/p>\n<pre>\r\n        $this->query->take(1);\r\n        $models = $this->get($columns);\r\n        return $models->first();        \r\n<\/pre>\n<p><red><br \/>\n\u5982\u60a8\u6240\u898b\uff0c\u9019\u5c31\u662fmagic method\u6703\u88ab\u4eba\u8a6c\u75c5\u7684\u5730\u65b9\u3002\u9019\u6bb5code\u8b8a\u5f97\u8d85\u7d1a\u96e3trace\uff0c\u4e0d\u77e5\u9053\u5230\u5e95\u5728\u547c\u53eb\u4ec0\u9ebc\u3002<br \/>\n<\/red><\/p>\n<p>\u9019\u4e09\u53e5\uff0c\u7b2c\u4e00\u53e5\u5c31\u662f\u7528$query\u8a2d\u5b9a\u6578\u91cf\uff08MySQL\u7684LIMIT\uff09\uff0c\u7b2c\u4e8c\u53e5\u662f\u5f97\u5230\u5305\u4f4f\u591a\u500bEloquent\u5be6\u9ad4\u7684Collection\uff0c\u7b2c\u4e09\u53e5\u5247\u662f\u62ff\u51faCollection\u7684\u7b2c\u4e00\u500b\u3002<\/p>\n<p>\u7b2c\u4e8c\u53e5\u7684get\u5c31\u662f\u4f60\u5e38\u7528\u5230\u7684\u90a3\u500bget\uff1a<\/p>\n<pre>\r\n$users = User::where('votes', '>', 100)->take(10)->get();\r\n<\/pre>\n<p>\u4f86\u770b\u770b\u5b83\u7684\u9577\u76f8\u5427\u3002<\/p>\n<pre>\r\n\/\/ Illuminate\\Database\\Eloquent\\Builder.php\r\n\t\/**\r\n\t * Execute the query as a \"select\" statement.\r\n\t *\r\n\t * @param  array  $columns\r\n\t * @return \\Illuminate\\Database\\Eloquent\\Collection|static[]\r\n\t *\/\r\n\tpublic function get($columns = array('*'))\r\n\t{\r\n\t\t$models = $this->getModels($columns);\r\n\r\n\t\t\/\/ If we actually found models we will also eager load any relationships that\r\n\t\t\/\/ have been specified as needing to be eager loaded, which will solve the\r\n\t\t\/\/ n+1 query issue for the developers to avoid running a lot of queries.\r\n\t\tif (count($models) > 0)\r\n\t\t{\r\n\t\t\t$models = $this->eagerLoadRelations($models);\r\n\t\t}\r\n\r\n\t\treturn $this->model->newCollection($models);\r\n\t}\r\n\r\n<\/pre>\n<p>\u91cd\u9ede\u4f86\u4e86\u3002\u7d42\u65bc\u80fd\u56de\u7b54\u4e00\u958b\u59cb\u7684\u554f\u984c<br \/>\n\u300c\u8cc7\u6599\u5eab\u6488\u51fa\u4f86\u7684\u61c9\u8a72\u662f\u4e00\u5927\u5806rows\uff0c\u8f49\u6210PHP\u7684\u9663\u5217\u9084\u7b97\u5bb9\u6613\u60f3\u50cf\uff0c\u4f46\u600e\u9ebc\u8f49\u6210Eloquent\u5be6\u9ad4\u5462\uff1f\u300d<br \/>\n\u4f86\u770b\u7b2c\u4e00\u53e5\u7684getModels\uff1a<\/p>\n<pre>\r\n\/\/ Illuminate\\Database\\Eloquent\\Builder.php\r\n\t\/**\r\n\t * Get the hydrated models without eager loading.\r\n\t *\r\n\t * @param  array  $columns\r\n\t * @return \\Illuminate\\Database\\Eloquent\\Model[]\r\n\t *\/\r\n\tpublic function getModels($columns = array('*'))\r\n\t{\r\n\t\t\/\/ First, we will simply get the raw results from the query builders which we\r\n\t\t\/\/ can use to populate an array with Eloquent models. We will pass columns\r\n\t\t\/\/ that should be selected as well, which are typically just everything.\r\n\t\t$results = $this->query->get($columns);\r\n\r\n\t\t$connection = $this->model->getConnectionName();\r\n\r\n\t\t$models = array();\r\n\r\n\t\t\/\/ Once we have the results, we can spin through them and instantiate a fresh\r\n\t\t\/\/ model instance for each records we retrieved from the database. We will\r\n\t\t\/\/ also set the proper connection name for the model after we create it.\r\n\t\tforeach ($results as $result)\r\n\t\t{\r\n\t\t\t$models[] = $model = $this->model->newFromBuilder($result);\r\n\r\n\t\t\t$model->setConnection($connection);\r\n\t\t}\r\n\r\n\t\treturn $models;\r\n\t}\r\n<\/pre>\n<p>\u89e3\u7b54\u770b\u8d77\u4f86\u4e0d\u9060\u4e86\uff01\u56de\u5230Eloquent\\Model\u770b\u9019\u500b\u65b9\u6cd5<\/p>\n<pre>\r\n\/\/ Illuminate\\Database\\Eloquent\\Model.php\r\n\t\/**\r\n\t * Create a new model instance that is existing.\r\n\t *\r\n\t * @param  array  $attributes\r\n\t * @return static\r\n\t *\/\r\n\tpublic function newFromBuilder($attributes = array())\r\n\t{\r\n\t\t$instance = $this->newInstance(array(), true);\r\n\r\n\t\t$instance->setRawAttributes((array) $attributes, true);\r\n\r\n\t\treturn $instance;\r\n\t}\r\n\r\n<\/pre>\n<p>\u539f\u4f86\u5728\u9019\u88e1\uff01\u7d42\u65bc\u627e\u5230\u7b54\u6848\u4e86\uff01\u8166\u888b\u90fd\u5feb\u71d2\u6389\u4e86\uff01<\/p>\n<h3>\u8907\u7fd2<\/h3>\n<p>\u597d\uff0c\u6700\u5f8c\u6211\u5011\u4f86\u8907\u7fd2\u4e00\u904d\u3002<br \/>\nEloquent\u7684find\u4f9d\u5e8f\u57f7\u884c\uff1a<br \/>\n\u4e00\u3001\u505a\u51faEloquent\\Builder\u7269\u4ef6\uff0c\u8acb\u5b83\u6839\u64daprimary key\u505a\u51faEloquent\\Model\u5be6\u9ad4<br \/>\n\u4e8c\u3001Eloquent\\Builder\u6703\u62ff\u51faQuery\\Builder\u5be6\u9ad4\u5e6b\u5fd9\uff0c\u544a\u8a34\u5b83where\u6558\u8ff0<br \/>\n\u4e09\u3001Eloquent\\Builder\u63a5\u8457\u544a\u8a34\u5b83limit\u6558\u8ff0\u3001\u62ff\u51farows\u505a\u6210\u7269\u4ef6\u3001\u628a\u7b2c\u4e00\u500b\u56de\u50b3<\/p>\n<p>\u4ee5\u4e0a\u5927\u81f4\u5c31\u662f\u4e00\u500bEloquent\u5b50\u985e\u5225\u5728find\u6642\u6703\u505a\u7684\u4e8b\u60c5\u3002\u662f\u4e0d\u662f\u6bd4\u60f3\u50cf\u4e2d\u8907\u96dc\u5730\u591a\u5462\uff1a\uff09<br \/>\n\u6211\u662f\u89ba\u5f97contributors\u6709\u9ede\u70ab\u6280\u5566\uff0c\u591a\u5beb\u5e7e\u884c\u6e05\u695a\u5f97\u591a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6488\u51fa\u4e00\u7b46\u8cc7\u6599\u6642\uff0cEloquent\u662f\u5982\u4f55\u5c07\u8cc7\u6599mapping\u6210\u7269\u4ef6\u7684\u5462\uff1f $user = User::find( &hellip; <a href=\"https:\/\/blog.turn.tw\/?page_id=1360\" class=\"more-link\">\u7e7c\u7e8c\u95b1\u8b80 <span class=\"screen-reader-text\">Eloquent CRUD\uff1afind<\/span> <span class=\"meta-nav\">&rarr;<\/span> <\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_mi_skip_tracking":false},"_links":{"self":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1360"}],"collection":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1360"}],"version-history":[{"count":12,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1360\/revisions"}],"predecessor-version":[{"id":1372,"href":"https:\/\/blog.turn.tw\/index.php?rest_route=\/wp\/v2\/pages\/1360\/revisions\/1372"}],"wp:attachment":[{"href":"https:\/\/blog.turn.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}