Laravel 之 Tinker 使用

[复制链接]
frankphper 发表于 2019-3-6 18:42:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
Laravel 之 Tinker 使用
  1. >php artisan tinker
  2. Psy Shell v0.7.2 (PHP 5.6.36 — cli) by Justin Hileman
  3. >>> $article = new App\Article;
  4. => App\Article {#645}
  5. >>> $article->title='My first title';
  6. => "My first title"
  7. >>> $article->content='content';
  8. => "content"
  9. >>> $article->published_at=Carbon\Carbon::now();
  10. => Carbon\Carbon {#647
  11.      +"date": "2019-03-06 10:10:58.320919",
  12.      +"timezone_type": 3,
  13.      +"timezone": "UTC",
  14.    }
  15. >>> $article;
  16. => App\Article {#645
  17.      title: "My first title",
  18.      content: "content",
  19.      published_at: Carbon\Carbon {#647
  20.        +"date": "2019-03-06 10:10:58.320919",
  21.        +"timezone_type": 3,
  22.        +"timezone": "UTC",
  23.      },
  24.    }
  25. >>> $article->save();
  26. => true
  27. >>> $article->toArray();
  28. => [
  29.      "title" => "My first title",
  30.      "content" => "content",
  31.      "published_at" => Carbon\Carbon {#647
  32.        +"date": "2019-03-06 10:10:58.320919",
  33.        +"timezone_type": 3,
  34.        +"timezone": "UTC",
  35.      },
  36.      "updated_at" => "2019-03-06 10:12:38",
  37.      "created_at" => "2019-03-06 10:12:38",
  38.      "id" => 1,
  39.    ]
  40. >>> $first = App\Article::find(1);
  41. => App\Article {#662
  42.      id: 1,
  43.      title: "My first title",
  44.      content: "content",
  45.      published_at: "2019-03-06 10:10:58",
  46.      created_at: "2019-03-06 10:12:38",
  47.      updated_at: "2019-03-06 10:12:38",
  48.      intro: "",
  49.    }
  50. >>> $first->published_at=Carbon/Carbon:now();
  51. PHP Parse error: Syntax error, unexpected ':' on line 1
  52. >>> $first->published_at=Carbon/Carbon::now();
  53. PHP Fatal error:  Class 'Carbon' not found in eval()'d code on line 1
  54. >>> $first->published_at=Carbon\Carbon::now();
  55. => Carbon\Carbon {#666
  56.      +"date": "2019-03-06 10:18:11.614766",
  57.      +"timezone_type": 3,
  58.      +"timezone": "UTC",
  59.    }
  60. >>> $first->save();
  61. => true
  62. >>> $first = App\Article::find(1);
  63. => App\Article {#674
  64.      id: 1,
  65.      title: "My first title",
  66.      content: "content",
  67.      published_at: "2019-03-06 10:18:11",
  68.      created_at: "2019-03-06 10:12:38",
  69.      updated_at: "2019-03-06 10:18:24",
  70.      intro: "",
  71.    }
  72. >>> ^C
  73. ➜ /Users/weirubo/www/php/laravel51 >php artisan tinker
  74. Psy Shell v0.7.2 (PHP 5.6.36 — cli) by Justin Hileman
  75. >>> $first = App\Article::find(1);
  76. => App\Article {#658
  77.      id: 1,
  78.      title: "My first title",
  79.      content: "content",
  80.      published_at: "2019-03-06 10:18:11",
  81.      created_at: "2019-03-06 10:12:38",
  82.      updated_at: "2019-03-06 10:18:24",
  83.      intro: "",
  84.    }
  85. >>> $first->published_at=Carbon\Carbon::now();
  86. => Carbon\Carbon {#639
  87.      +"date": "2019-03-06 18:19:48.379670",
  88.      +"timezone_type": 3,
  89.      +"timezone": "PRC",
  90.    }
  91. >>> $first->save();
  92. => true
  93. >>> $first = App\Article::find(1);
  94. => App\Article {#662
  95.      id: 1,
  96.      title: "My first title",
  97.      content: "content",
  98.      published_at: "2019-03-06 18:19:48",
  99.      created_at: "2019-03-06 10:12:38",
  100.      updated_at: "2019-03-06 18:19:57",
  101.      intro: "",
  102.    }
  103. >>> $first->title='Update';
  104. => "Update"
  105. >>> $first->save();
  106. => true
  107. >>> $second = App\Article::where('content', '=', 'content')->get();
  108. => Illuminate\Database\Eloquent\Collection {#665
  109.      all: [
  110.        App\Article {#666
  111.          id: 1,
  112.          title: "Update",
  113.          content: "content",
  114.          published_at: "2019-03-06 18:19:48",
  115.          created_at: "2019-03-06 10:12:38",
  116.          updated_at: "2019-03-06 18:22:05",
  117.          intro: "",
  118.        },
  119.      ],
  120.    }
  121. >>> $second->title;
  122. PHP error:  Undefined property: Illuminate\Database\Eloquent\Collection::$title on line 1
  123. >>> $second->title;
  124. PHP error:  Undefined property: Illuminate\Database\Eloquent\Collection::$title on line 1
  125. >>>
  126. >>>
  127. >>> $second = App\Article::where('content', 'content')->first();
  128. => App\Article {#669
  129.      id: 1,
  130.      title: "Update",
  131.      content: "content",
  132.      published_at: "2019-03-06 18:19:48",
  133.      created_at: "2019-03-06 10:12:38",
  134.      updated_at: "2019-03-06 18:22:05",
  135.      intro: "",
  136.    }
  137. >>> $second->title;
  138. => "Update"
  139. >>> $second = App\Article::create(['title'=>'Second title', 'content'=>'Second Content', 'published_at'=>Carbon\Carbon::now()]);
  140. Illuminate\Database\Eloquent\MassAssignmentException with message 'title'
  141. >>> $second = App\Article::create(['title'=>'Second title', 'content'=>'Second Content', 'published_at'=>Carbon\Carbon::now()]);
  142. Illuminate\Database\Eloquent\MassAssignmentException with message 'title'
  143. >>> ^C
  144. ➜ /Users/weirubo/www/php/laravel51 >php artisan tinker
  145. Psy Shell v0.7.2 (PHP 5.6.36 — cli) by Justin Hileman
  146. >>> $second = App\Article::create(['title'=>'Second title', 'content'=>'Second Content', 'published_at'=>Carbon\Carbon::now()]);
  147. => App\Article {#659
  148.      title: "Second title",
  149.      content: "Second Content",
  150.      published_at: Carbon\Carbon {#657
  151.        +"date": "2019-03-06 18:32:14.542947",
  152.        +"timezone_type": 3,
  153.        +"timezone": "PRC",
  154.      },
  155.      updated_at: "2019-03-06 18:32:14",
  156.      created_at: "2019-03-06 18:32:14",
  157.      id: 2,
  158.    }
  159. >>> $article->update(['title'=>'Change Title']);
  160. PHP error:  Undefined variable: article on line 1
  161. >>> $second->update(['title'=>'Change Title']);
  162. => true
  163. >>>
复制代码


*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则