{"id":253,"date":"2020-12-24T09:03:57","date_gmt":"2020-12-24T09:03:57","guid":{"rendered":"http:\/\/robinluo.top\/?p=253"},"modified":"2020-12-24T09:03:57","modified_gmt":"2020-12-24T09:03:57","slug":"eloquent-%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/robinluo.top\/?p=253","title":{"rendered":"eloquent \u4f7f\u7528"},"content":{"rendered":"\n<p>\u7b80\u4ecbeloquent \u4e3alaravel\u4e2d\u7684orm\u6a21\u5757<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u4ee3\u7801\u4e3aorm\u5bf9\u8c61\u751f\u6210\u547d\u4ee4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:model Flight \/\/\u521b\u5efaFlight orm \u5bf9\u8c61<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>php artisan make:model Flight --migration | -m \/\/\u751f\u6210migration\u6570\u636e\u5e93\u8fc1\u79fb\u6587\u4ef6<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># Generate a model and a FlightFactory class...\nphp artisan make:model Flight --factory\nphp artisan make:model Flight -f\n\n# Generate a model and a FlightSeeder class...\nphp artisan make:model Flight --seed\nphp artisan make:model Flight -s\n\n# Generate a model and a FlightController class...\nphp artisan make:model Flight --controller\nphp artisan make:model Flight -c\n\n# Generate a model and a migration, factory, seeder, and controller...\nphp artisan make:model Flight -mfsc<\/code><\/pre>\n\n\n\n<p>\u751f\u6210\u7684\u57fa\u672c\u5bf9\u8c61\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Flight extends Model\n{\n    \/\/optional\n    protected $table = 'my_flights';\/\/\u6307\u5b9a\u8868\u540d\n    protected $primaryKey = 'flight_id'; \/\/\u6307\u5b9a\u4e3b\u952e\n    public $incrementing = false; \/\/\u4e3b\u952e\u751f\u6210\u6a21\u5f0f\n    protected $keyType = 'string';\n \/\/\u4e3b\u952e\u6570\u636e\u7c7b\u578b\n    public $timestamps = false;\n\/\/\u662f\u5426\u6709create \u548c update \u65f6\u95f4\n    protected $dateFormat = 'U';\n \/\/\u65e5\u671f\u683c\u5f0f\n    const CREATED_AT = 'creation_date';\n \/\/\u521b\u5efa\u65f6\u95f4\u5b57\u6bb5\u540d\n    const UPDATED_AT = 'updated_date';\/\/\u66f4\u65b0\u65f6\u95f4\u5b57\u6bb5\u540d\n    protected $connection = 'sqlite';\n \/\/\u6307\u5b9a\u8be5orm\u6570\u636e\u5e93\u8fde\u63a5\n    protected $fillable = &#91;] \/\/\u6570\u7ec4 \u53ef\u6279\u91cf\u521b\u5efa\u7684\u5b57\u6bb5\n    protected $guard = &#91;] \/\/\u6570\u7ec4 \u4e0d\u53ef\u6279\u91cf\u521b\u5efa\u7684\u5b57\u6bb5\n\n    protected $attributes = &#91;\n   \/\/\u5b57\u6bb5\u9ed8\u8ba4\u503c\n        'delayed' => false,\n    ];\n\n\n}<\/code><\/pre>\n\n\n\n<p>\u67e5\u8be2\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<p>Flight::all<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Models\\Flight;\n\nforeach (Flight::all() as $flight) {\n    echo $flight->name;\n}<\/code><\/pre>\n\n\n\n<p>\u67e5\u8be2\u6784\u9020\u5668<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$flights = Flight::where('active', 1)\n               ->orderBy('name')\n               ->take(10)\n               ->get();<\/code><\/pre>\n\n\n\n<p>\u805a\u5408\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$count = Flight::where('active', 1)->count();\n\n$max = Flight::where('active', 1)->max('price');<\/code><\/pre>\n\n\n\n<p>\u5bf9\u8c61collection reject\u65b9\u6cd5\u5254\u9664\u53d6\u6d88\u7684\u822a\u73ed \uff08\u95ed\u5305\u8fd4\u56detrue\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$flights = Flight::where('destination', 'Paris')->get();\n\n$flights = $flights->reject(function ($flight) {\n    return $flight->cancelled;\n});<\/code><\/pre>\n\n\n\n<p>orm\u6e38\u6807<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Models\\User;\n\n$users = User::cursor()->filter(function ($user) {\n    return $user->id > 500;\n});<\/code><\/pre>\n\n\n\n<p>\u5b50\u67e5\u8be2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Models\\Destination;\nuse App\\Models\\Flight;\n\nreturn Destination::addSelect(&#91;'last_flight' => Flight::select('name')\n    ->whereColumn('destination_id', 'destinations.id')\n    ->orderByDesc('arrived_at')\n    ->limit(1)\n])->get();<\/code><\/pre>\n\n\n\n<p>\u67e5\u4e00\u6761\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Models\\Flight;\n\n\/\/ Retrieve a model by its primary key...\n$flight = Flight::find(1);\n\n\/\/ Retrieve the first model matching the query constraints...\n$flight = Flight::where('active', 1)->first();\n\n\/\/ Alternative to retrieving the first model matching the query constraints...\n$flight = Flight::firstWhere('active', 1);<\/code><\/pre>\n\n\n\n<p>\u67e5\u5230\u4e00\u6761\u6216\u8005\uff08\u95ed\u5305\uff09<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>model = Flight::where('legs', '>', 3)->firstOr(function () {\n    \/\/ ...\n});<\/code><\/pre>\n\n\n\n<p>\u67e5\u8be2\u62a5notFound  fail\u65b9\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$flight = Flight::findOrFail(1);\n\n$flight = Flight::where('legs', '>', 3)->firstOrFail();<\/code><\/pre>\n\n\n\n<p>\u9759\u6001\u521b\u5efa<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Models\\Flight;\n\n\/\/ Retrieve flight by name or create it if it doesn't exist...\n$flight = Flight::firstOrCreate(&#91;\n    'name' => 'London to Paris'\n]);\n\n\/\/ Retrieve flight by name or create it with the name, delayed, and arrival_time attributes...\n$flight = Flight::firstOrCreate(\n    &#91;'name' => 'London to Paris'],\n    &#91;'delayed' => 1, 'arrival_time' => '11:30']\n);\n\n\/\/ Retrieve flight by name or instantiate a new Flight instance...\n$flight = Flight::firstOrNew(&#91;\n    'name' => 'London to Paris'\n]);\n\n\/\/ Retrieve flight by name or instantiate with the name, delayed, and arrival_time attributes...\n$flight = Flight::firstOrNew(\n    &#91;'name' => 'Tokyo to Sydney'],\n    &#91;'delayed' => 1, 'arrival_time' => '11:30']\n);<\/code><\/pre>\n\n\n\n<p>\u5bf9\u8c61\u521b\u5efa<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Http\\Controllers\\Controller;\nuse App\\Models\\Flight;\nuse Illuminate\\Http\\Request;\n\nclass FlightController extends Controller\n{\n    \/**\n     * Store a new flight in the database.\n     *\n     * @param  \\Illuminate\\Http\\Request  $request\n     * @return \\Illuminate\\Http\\Response\n     *\/\n    public function store(Request $request)\n    {\n        \/\/ Validate the request...\n\n        $flight = new Flight;\n\n        $flight->name = $request->name;\n\n        $flight->save();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u5bf9\u8c61\u66f4\u65b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Models\\Flight;\n\n$flight = Flight::find(1);\n\n$flight->name = 'Paris to London';\n\n$flight->save();<\/code><\/pre>\n\n\n\n<p>\u6279\u91cf\u66f4\u65b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Flight::where('active', 1)\n      ->where('destination', 'San Diego')\n      ->update(&#91;'delayed' => 1]);<\/code><\/pre>\n\n\n\n<p>\u5bf9\u8c61\u586b\u5145\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$flight->fill(&#91;'name' => 'Amsterdam to Frankfurt']);<\/code><\/pre>\n\n\n\n<p>\u6309\u6761\u4ef6\u66f4\u65b0\u6216\u521b\u5efa<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$flight = Flight::updateOrCreate(\n    &#91;'departure' => 'Oakland', 'destination' => 'San Diego'],\/\/\u6761\u4ef6\n    &#91;'price' => 99, 'discounted' => 1] \/\/\u6570\u636e\n);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Flight::upsert(&#91;\n    &#91;'departure' => 'Oakland', 'destination' => 'San Diego', 'price' => 99],\n    &#91;'departure' => 'Chicago', 'destination' => 'New York', 'price' => 150]\n], &#91;'departure', 'destination'], &#91;'price']);<\/code><\/pre>\n\n\n\n<p>\u5220\u9664<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Models\\Flight;\n\n$flight = Flight::find(1);\n\n$flight->delete();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Flight::destroy(1);\n\nFlight::destroy(1, 2, 3);\n\nFlight::destroy(&#91;1, 2, 3]);\n\nFlight::destroy(collect(&#91;1, 2, 3]));<\/code><\/pre>\n\n\n\n<p>\u67e5\u8be2\u5220\u9664<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$deletedRows = Flight::where('active', 0)->delete();<\/code><\/pre>\n\n\n\n<p>\u903b\u8f91\u5220\u9664<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>namespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse Illuminate\\Database\\Eloquent\\SoftDeletes;\n\nclass Flight extends Model\n{\n    use SoftDeletes; \/\/\u591a\u4e00\u4e2adeleted_at\u5b57\u6bb5\n}<\/code><\/pre>\n\n\n\n<p>\u8fc1\u79fb\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use Illuminate\\Database\\Schema\\Blueprint;\nuse Illuminate\\Facades\\Schema;\n\nSchema::table('flights', function (Blueprint $table) {\n    $table->softDeletes();\n});\n\nSchema::table('flights', function (Blueprint $table) {\n    $table->dropSoftDeletes();\n});<\/code><\/pre>\n\n\n\n<p>\u68c0\u67e5\u662f\u662f\u5426\u88ab\u8f6f\u5220\u9664<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if ($flight->trashed()) {\n    \/\/\n}<\/code><\/pre>\n\n\n\n<p>\u8fd8\u539f\u903b\u8f91\u5220\u9664\u7684\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$flight->restore();\nFlight::withTrashed()\n        ->where('airline_id', 1)\n        ->restore();\n$flight->history()->restore();<\/code><\/pre>\n\n\n\n<p>\u5f3a\u5236\u7269\u7406\u5220\u9664<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$flight->forceDelete();<\/code><\/pre>\n\n\n\n<p>\u548c\u8f6f\u5220\u9664\u7684\u6570\u636e\u4e00\u8d77\u67e5\u8be2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$flights = Flight::withTrashed()\n                ->where('account_id', 1)\n                ->get();<\/code><\/pre>\n\n\n\n<p>\u53ea\u67e5\u8be2\u8f6f\u5220\u9664\u7684\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$flights = Flight::onlyTrashed()\n                ->where('airline_id', 1)\n                ->get();<\/code><\/pre>\n\n\n\n<p>\u5bf9\u8c61\u590d\u5236replicate<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>use App\\Models\\Address;\n\n$shipping = Address::create(&#91;\n    'type' => 'shipping',\n    'line_1' => '123 Example Street',\n    'city' => 'Victorville',\n    'state' => 'CA',\n    'postcode' => '90001',\n]);\n\n$billing = $shipping->replicate()->fill(&#91;\n    'type' => 'billing'\n]);\n\n$billing->save();<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7b80\u4ecbeloquent \u4e3alaravel\u4e2d\u7684orm\u6a21\u5757 \u4ee5\u4e0b\u4ee3\u7801\u4e3aorm\u5bf9\u8c61\u751f\u6210\u547d\u4ee4 \u751f\u6210\u7684\u57fa\u672c\u5bf9\u8c61\uff1a \u67e5\u8be2\u65b9\u6cd5\uff1a Flight::all \u67e5\u8be2\u6784\u9020\u5668 \u805a\u5408\u51fd\u6570 \u5bf9\u8c61collection reject\u65b9\u6cd5\u5254\u9664\u53d6\u6d88\u7684\u822a\u73ed \uff08\u95ed\u5305\u8fd4\u56detrue\uff09 orm\u6e38\u6807 \u5b50\u67e5\u8be2 \u67e5\u4e00\u6761\u6570\u636e \u67e5\u5230\u4e00\u6761\u6216\u8005\uff08\u95ed\u5305\uff09 \u67e5\u8be2\u62a5notFound fail\u65b9\u6cd5 \u9759\u6001\u521b\u5efa \u5bf9\u8c61\u521b\u5efa \u5bf9\u8c61\u66f4\u65b0 \u6279\u91cf\u66f4\u65b0 \u5bf9\u8c61\u586b\u5145\u6570\u636e \u6309\u6761\u4ef6\u66f4\u65b0\u6216\u521b\u5efa \u5220\u9664 \u67e5\u8be2\u5220\u9664 \u903b\u8f91\u5220\u9664 \u8fc1\u79fb\u6587\u4ef6 \u68c0\u67e5\u662f\u662f\u5426\u88ab\u8f6f\u5220\u9664 \u8fd8\u539f\u903b\u8f91\u5220\u9664\u7684\u6570\u636e \u5f3a\u5236\u7269\u7406\u5220\u9664 \u548c\u8f6f\u5220\u9664\u7684\u6570\u636e\u4e00\u8d77\u67e5\u8be2 \u53ea\u67e5\u8be2\u8f6f\u5220\u9664\u7684\u6570\u636e \u5bf9\u8c61\u590d\u5236replicate<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[31,29,10,30],"_links":{"self":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/253"}],"collection":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=253"}],"version-history":[{"count":14,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions"}],"predecessor-version":[{"id":269,"href":"https:\/\/robinluo.top\/index.php?rest_route=\/wp\/v2\/posts\/253\/revisions\/269"}],"wp:attachment":[{"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/robinluo.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}