Upgrade to Pro — share decks privately, control downloads, hide ads and more …

使用Eloquent ORM

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

使用Eloquent ORM

Avatar for Shengyou Fan

Shengyou Fan

December 07, 2014
Tweet

More Decks by Shengyou Fan

Other Decks in Programming

Transcript

  1. Model ׳ྫ • Laravel త૬᮫׳ྫෳश - Ұݸ resource ሣጯ౸ DB

    ཫతҰݸ table - Ұݸ model ሣጯ౸ table ཫతҰݸ row - table ໊᜝༻ෳᏐʀ model ໊᜝༻ᄸᏐ - ኺࢿྉදऔग़ိత౦੢ੋ Collectionɼ Մᙛ ਞྻ(array) ࢖༻
  2. tinker খ㢨ख • PHP ຊ਎༗㚎ݐޓಈࢦྩ (REPL) ໛ࣜɼ Մ༻ php -a

    ਐೖɼՄࡏଖதଌࢼఔࣜᛰ • tinker ଇੋ Laravel ఏڙత artisan ࢦྩɼআ ྃਐೖ PHP త REPL ໛ࣜ֎ɼՃࡌ Laravel ॴ༗෺݅؀ڥɼՄᩋզ၇ࡏଖதଌࢼ Laravel తఔࣜᛰ ˒ tinker 㘸໌ɿhttp://laravel-recipes.com/recipes/280/interacting-with-your-application
  3. artisan tinker • ਐೖ PHP ޓಈ໛ࣜɼฒࡌೖ Laravel ؀ڥ - ՄҎ௚઀ࡏࢦྩ໛ࣜԼૢ࡞

    Laravel ֤ݩ ݅ɼଌࢼఔࣜሜ๏ - ཁ݁ଋ੥༻ exit; ࢦྩ • ൣྫɿ $  php  artisan  tinker [1]  >  exit; ˒ ༝ԙ tinker ૬ґత extension pcntl ࡏ Windows ্ᔒ༗መ࡞ɼҼࠑແ ๏શޭೳࢧԉ tinker ޭೳɿhttp://php.net/manual/en/pcntl.installation.php
  4. dd() ଌࢼ޻۩ • Laravel 㚎ݐҰݸ debug ีॿവࣜɿdd() - ଖመबੋ೺ᏓᏐ㟚ਐ var_dump

    ޙ exit - ՄҎࡏ೚Կ஍ํ࢖༻ɼଌࢼఔࣜᛰྲྀ ఔɺᬓाɺྃղ෺݅㚎༰ • ൣྫɿ $post  =  Post::find(1); dd($post); ˒ dd() 㘸໌ɿhttp://laravel.com/docs/4.2/helpers#miscellaneous
  5. ৽⃧ࢿྉ • ࢖༻ new ݐߏࣜ - ௚઀࢖༻ new ݐߏࣜ㗞ੜ Model

    መᱪɼ ࠶ଘ䈕 • ൣྫɿ $post  =  new  Post; $post-­‐>title  =  ‘My  Title’; $post-­‐>save();
  6. Mass Assignment • Laravel త ORM ՄҎ௚઀༻ Mass Assignment తํࣜ௚઀৽⃧ࢿྉɼޠ๏؆

    ܿɺշ଎ํศɼୠ༗Մೳ။༗҆શੑ໰୊ • ࡏ generator 㗞ੜత Model ཫɼfillable త ሱੑबੋࡏઃఆ䬟ࠣཝҐՄҎ࢖༻ Mass Assignment ၏ҝ๷ڔखஈ ˒ ׭໢จ݅ɿhttp://laravel.tw/docs/4.2/eloquent#mass-assignment
  7. ઃఆ fillableɺguarded • ࡏ Model 㚎ઃఆ fillable ሱੑɼࢦఆ䬟ࠣཝ ҐՄҎಁա Mass

    Assignment ိሜೖ/ߋ৽ ࢿྉ • guarded ଇੋ૬൓తሱੑɼઃఆޙՄҎอ ޢಛఆཝҐෆ࢖༻ Mass Assignment • ൣྫɿ protected  $fillable  =  [‘title’,  ‘content’,  ‘category_id’]; protected  $guarded  =  [‘id’,  ‘password’];
  8. ႟আࢿྉ • destroy($id); - ႟আҰචࢿྉɼ࢖༻ primary key • destroy([1, 2,

    3]); - ႟আଟචࢿྉɼ࢖༻ array ၚ值 • destroy(1, 2, 3); - ႟আଟචࢿྉɼ࢖༻ଟჩᏐၚ值
  9. ߋ৽ࢿྉ • save(); • ઌऔग़ Model መᱪޙɼ࠶਑ሣሱੑߋ ৽ɼશ෦׬੒ޙ࠶ଘ䈕 • ൣྫɿ

    $post  =  Post::find(1); $post-­‐>title  =  ‘My  New  Title’; $post-­‐>save();
  10. 查ਘࢿྉ • all() // औग़શ෦ࢿྉ • find($id); // ࢖༻ primary

    key • where(‘ཝҐ’, ‘ᑍ݅’, ‘值’); • orderBy(‘ཝҐ’, ‘ഉংํࣜ’); • get(); // ճၚ Collection • first(); // ճၚ Model
  11. Collection ஞචऔग़ • ኺ Model 查ᨑత݁ՌɼEloquent ။ճၚҰ ݸ Collectionɼଖߦҝब૾ਞྻҰᒬɼՄ Ҏࢧԉ༻

    foreach औग़ࢿྉ • ൣྫɿ $posts  =  Post::all();  //  $posts  ੋ  Collection foreach($posts  as  $post) {        echo  $post-­‐>title;  //  $post  ੋ  Model }
  12. ಛҟޭೳ • paginate($per_page); - ґઃఆతචᏐࡏ Query 㚎Ճ Limit ޠ ๏ɼࡏ

    view ্Մࣗಈࢧԉ෼ท • random($num); - ኺ Collection ᬋػऔग़ n චࢿྉ
  13. Model ࢿྉ᮫࿆ • ࡏ Model 㚎ఆٛଖᢛଖଞ Model ؒత᮫࿆ ྨܕ Eloquent

    ब။ࣗಈሡṜݸ᮫࿆Ꮣ੒෺ ݅ؒతሱੑɼࣕແधࣗߦԼ SQL ޠ๏ • ൣྫɿ public  function  {㠥Ұݸ  Model  త໊᜝(ᄸ/ෳᏐ)}() {        return  $this-­‐>{᮫࿆ྨܕ}(‘{Model  ໊᜝}’); }
  14. ᮫࿆ྨܕ • belongsTo(‘Model’); - ᓤሱԙ㠥Ұݸ Model • hasMany(‘Model’); - ༴༗ଟݸ

    Model ˒ ᮫࿆ྨܕৄࡉ㘸໌ɿhttp://laravel.tw/docs/4.2/eloquent#relationships
  15. Model 㚎औ༻᮫࿆ • ೺ࢿྉؒత᮫࿆༻෺݅తํࣜڈࢥߟɼଈ Մಁա Model తሱੑ值ڈऔಘ᮫࿆ޙతࢿ ྉ㚎༰ • ൣྫɿ

    $post  =  Post::find(1); echo  $post-­‐>category-­‐>name; foreach($post-­‐>comments  as  $comment) {        echo  $comment-­‐>content; }
  16. ֊ஈᒾ查ද • ࡏ routes.php 㚎ଌࢼ Eloquent ORM ༻๏ త get

    request نଇᏐݸ • ࡾݸ Model 㚎౎༗ሣጯత᮫࿆ᬓाઃఆ