public function store()
 {
     $article = Articles::create(Input::only(['title', 'content', 'month', 'year', 'author', 'image', 'category']));
     $article->slug = str_replace(' ', '-', strtolower(Input::get('title')));
     $article->save();
     return Redirect::to('/admin/articles/' . $category->slug . '/' . $article->slug . '/');
 }
 /**
  * Run the database seeds.
  * 新增假資料
  * @return void
  */
 public function run()
 {
     Articles::truncate();
     $time = Carbon::now('Asia/Taipei')->subweeks(3);
     foreach (range(1, 20) as $value) {
         $time->addDay();
         Articles::create(['article_title' => '文章測試' . $value, 'article_content' => '這是一篇測試文章,第' . $value . '篇。', 'feature' => rand(0, 1), 'page_view' => rand(0, 200), 'created_at' => $time, 'updated_at' => $time]);
     }
 }
Example #3
0
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('CanvasTest', ['as' => 'CanvasTest', function () {
    return view('CanvasTest');
}]);
Route::group(['prefix' => 'ORM'], function () {
    Route::get('', ['as' => 'ORM.HOME', function () {
        echo "this is ORM HOME.";
    }]);
    Route::get('create', ['as' => 'ORM.create', function () {
        // 使用Facade的create方法
        \App\Models\Articles::create(['article_title' => '文章測試Facade', 'article_content' => '這是一篇測試文章,第Facade篇。', 'feature' => rand(0, 1), 'page_view' => rand(0, 200), 'created_at' => Carbon::now('Asia/Taipei'), 'updated_at' => Carbon::now('Asia/Taipei')]);
        echo "Facade!!<br />";
        // 使用new建構式
        $post = new \App\Models\Articles();
        $post->article_title = '文章測試new';
        $post->article_content = '這是一篇測試文章,第new篇。';
        $post->feature = rand(0, 1);
        $post->page_view = rand(0, 200);
        $post->created_at = Carbon::now('Asia/Taipei');
        $post->updated_at = Carbon::now('Asia/Taipei');
        $post->save();
        echo "new!!<br />";
    }]);
    Route::get('search', ['as' => 'ORM.search', function () {
        // 收尋資料
        $data = \App\Models\Articles::all();
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     Eloquent::unguard();
     Articles::create(['title' => 'Test', 'content' => 'This is just a sample article', 'slug' => 'this-is-just-a-sample-article', 'month' => Carbon::now()->month, 'year' => Carbon::now()->year, 'author' => 1, 'category_id' => 1]);
 }
 public function import_vnexpress($case = null)
 {
     Log::info("Do log import_vnexpress in " . date("Y-m-d H:i:s"));
     if (true) {
         //============  ============
         //
         //
         if (empty($this->array_site)) {
             echo "List null";
             return;
         }
         if (empty($case)) {
             echo "Nhap gia tri";
             return;
         }
         switch ($case) {
             case "home":
                 $array_site_a[0] = "http://vnexpress.net";
                 break;
             case "all":
                 $array_site_a = $this->array_site;
                 break;
             default:
                 $array_site_a[0] = $this->array_site[(int) $case];
                 break;
         }
         // foreach tất cả phần tử trong mảng $this->array_site
         foreach ($array_site_a as $key_site => $value_site) {
             // Khởi tạo biến dom của link $value_site
             $dom = str_get_html(file_get_contents($value_site));
             // Lấy ra tất cả các tag a có trong link $value_site
             $m = $dom->find("a");
             $count_success = 0;
             // foreach tất cả các tag a có trong link $value_site
             foreach ($m as $key => $value) {
                 if ($count_success > 20) {
                     echo "Over 10 result" . PHP_EOL;
                     continue;
                 }
                 // Nếu tồn tại href thì vào trong
                 if (!empty($value->attr["href"])) {
                     // Lấy các link có phần tử cuối là .html và độ dài từ 10 ký tự trở lên
                     if (preg_match("/.{10,}\\.html\$/", $value->attr["href"])) {
                         // Log ban đầu
                         echo "=== Link: " . $value->attr["href"] . PHP_EOL;
                         if (LOG_INFO_FLAG_IMPORT) {
                             Log::info("Link: " . $value->attr["href"]);
                         }
                         //============  ============
                         //
                         //Khởi tạo biến title
                         //Phần tử trong có thể bao gồm
                         //1: plaintex
                         //2: img
                         // Kiểm tra trong link có phải hình hay không
                         if ($value->find("img", 0)) {
                             // Đúng là hình
                             if (!empty($value->find("img", 0)->attr["alt"])) {
                                 //Có alt gán biến title
                                 $title = $value->find("img", 0)->attr["alt"];
                             } else {
                                 //Nếu không có alt thì continue
                                 if (LOG_INFO_FLAG_IMPORT) {
                                     Log::info("exit :0 __ " . $value->attr["href"]);
                                 }
                                 continue;
                             }
                         } else {
                             // Không phải hình
                             $title = $value->innertext();
                         }
                         //
                         //============  ============
                         // Các trường hợp bắt lỗi
                         // 1: Link đã có trong DB
                         // 2: Chuỗi $title < 10 kỹ tự
                         if (Articles::where("article_link", $value->attr["href"])->count() != 0 || strlen(trim($title)) < 10) {
                             if (LOG_INFO_FLAG_IMPORT) {
                                 Log::info("exit :1 __ " . $title);
                             }
                             continue;
                         }
                         // Nếu link là đường dẫn tương đối thì thêm domain vào
                         if (preg_match("/^\\//", $value->attr["href"])) {
                             $value->attr["href"] = "http://vnexpress.net" . $value->attr["href"];
                         }
                         //============ ============  ============  ============
                         //  Khởi tạo biến $dom2 với nội dung theo link $value->attr["href"]
                         // Bạn có thể dùng function get content của vihoangson
                         // Gist.github: https://gist.github.com/vihoangson/647d856380ac5ca353b0
                         // Desc: Function lấy nội dung html của trang web khác bằng cUrl
                         // Function curl_get($url)
                         //
                         $dom2 = str_get_html(file_get_contents($value->attr["href"]));
                         $title = $title;
                         $link = $value->attr["href"];
                         // Rửa tổng biến $content
                         $content = "";
                         // Tìm phần tử đầu tiên trong dom có giá trị là #left_calculator
                         if ($dom2->find("#left_calculator", 0)) {
                             $content = $dom2->find("#left_calculator", 0)->innertext();
                         }
                         //============ ============  ============ ============
                         // LẤY NỘI DUNG CHI TIẾT TRANG
                         //  $value->attr["href"]
                         //  Output: $title;$content;$link;
                         //============ ============  ============ ============
                         //
                         // Khởi tạo biến $dom2 với nội dung theo link $value->attr["href"]
                         $dom2 = str_get_html(file_get_contents($value->attr["href"]));
                         $title = $title;
                         $link = $value->attr["href"];
                         // Rửa tổng biến $content
                         $content = "";
                         //============  ============
                         // $name_dom_title
                         // Biến bao của tag title
                         // Lưu ý: có thể thay đổi khi layout trang đích thay đổi
                         //============ ============
                         $name_dom_title = ".title_news h1";
                         //============  ============
                         // $name_dom_content
                         // Biến bao của tag content
                         // Lưu ý: có thể thay đổi khi layout trang đích thay đổi
                         //============ ============
                         $name_dom_content = "#left_calculator";
                         // Tìm phần tử đầu tiên trong dom có giá trị là #left_calculator
                         if ($dom2->find($name_dom_content, 0)) {
                             $content = $dom2->find($name_dom_content, 0)->innertext();
                         }
                         //
                         //============ ============  ============ ============
                         // LẤY NỘI DUNG CHI TIẾT TRANG
                         //  $value->attr["href"]
                         //  Output: $title;$content;$link;
                         //============ ============  ============ ============
                         //============  ============
                         // Save vào DB
                         //
                         if ($content == "") {
                             $dom2->clear();
                             if (LOG_INFO_FLAG_IMPORT) {
                                 Log::info("exit : Content rỗng");
                             }
                             continue;
                         }
                         $data = ["article_title" => $title, "article_content" => $content, "article_link" => $link];
                         if (Articles::create($data)) {
                             echo "!!! Saved: " . $title . PHP_EOL;
                             if (LOG_INFO_FLAG_IMPORT) {
                                 Log::info("!!! Saved: " . $title);
                             }
                             $count_success++;
                         } else {
                             if (LOG_INFO_FLAG_IMPORT) {
                                 Log::info("!!! Could't Save: " . $title);
                             }
                         }
                         //
                         //============  ============
                     }
                 }
             }
             // End foreach $m
         }
         // End foreach $this->array_site
         // Dừng chương trình
         // Lấy bài viết top
         $this->vnexpress_set_important_news();
         $c = new ArticlesController();
         // Lấy phần mở đầu
         $c->get_extra_content();
         // lấy hình ảnh chính
         $c->update_main_img();
         die;
         //
         //  ============  ============
     }
     // End if
 }
Example #6
0
 /**
  *  模型事件: 当模型 增、删、改 的时候,除了执行本该执行的 增、删、改 ,还执行一些其他的事情
  *
  *  Eloquent 中 create 方法 与 save  方法的 却别  在于 属性 的  黑白 名单 guard ,fillable
  *
  */
 public function modelEvent()
 {
     $data = ['title' => 'Model Event', 'content' => 'the model event can be declaration in model boot method', 'user_id' => 3, 'click_num' => 99];
     /*try{
                 Articles::create($data);
     
             }catch(Exception $e){
                 debug($e->getMessage());
             }*/
     /**
      *  由于 user_id 是 guard 属性,所以,只能使用 save 方法保存
      *
      * 也就是说,如果一个 模型 里面存在 保护字段,并且 创建 模型的 时候用 create 方法的话,那么需要执行两次 sql 语句 ,一条insert ,一条update
      *
      *  但是 如果 创建 模型的  时候  用的 是 save 方法的话   就不用管 在模型中 是否存在 黑 白 名单,所有的都一起 insert 进去
      */
     $obj = Articles::create($data);
     debug($obj->toArray());
     $obj->user_id = 3;
     $obj->status = 1;
     $obj->save();
     debug($obj->toArray());
     $obj = new Articles();
     $obj->title = 'Model Event';
     $obj->content = 'save content';
     $obj->user_id = 8;
     $obj->status = 6;
     $obj->click_num = 19;
     $re = $obj->save();
     if ($re) {
         debug('save success');
     } else {
         debug('save fail');
     }
     return view('index');
 }