Пример #1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     Eloquent::unguard();
     DB::table('tags')->truncate();
     //DB::table('pastes')->truncate();
     $faker = Faker\Factory::create();
     $paste_count = 10;
     $tags = array('php', 'javascript', 'ruby', 'js', 'cpp', 'c++', 'c#', 'go', 'html', 'css');
     for ($i = 0; $i < $paste_count; $i++) {
         $tags_per_paste = rand(1, 3);
         // Generate the paste
         $examplePaste = new Paste();
         $examplePaste->paste = $faker->paragraph;
         $examplePaste->title = $faker->realText(46);
         $examplePaste->expire = $faker->dateTime($max = 'now');
         $examplePaste->token = Str::random(40);
         $examplePaste->private = rand(0, 1);
         $examplePaste->delete_token = Str::random(40);
         $examplePaste->save();
         // Attach some tags to the new paste
         for ($i = 0; $i < $tags_per_paste; ++$i) {
             $exampleTag = new Tag();
             $exampleTag->tag = $tags[rand(0, sizeof($tags) - 1)];
             $exampleTag->paste_id = $examplePaste->id;
             $examplePaste->tags()->save($exampleTag);
         }
         print "Seeded paste with ID of " . $examplePaste->id . "\n";
     }
 }
Пример #2
0
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     $rules = array('private' => 'numeric|required', 'title' => 'max:46|required', 'paste' => 'required', 'expire' => 'required|numeric', 'private' => 'required|numeric', 'tags' => 'max:6|alpha');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         $messages = $validator->messages();
         return View::make('paste.form')->withErrors($messages);
     }
     $new_paste = new Paste();
     $new_paste->title = Input::get('title');
     $new_paste->token = Str::random(40);
     $new_paste->delete_token = Str::random(40);
     $new_paste->paste = Input::get('paste');
     $new_paste->private = Input::get('private');
     date_default_timezone_set('UTC');
     $expire_time = date('Y-m-d H:i:s', strtotime(sprintf('now + %s minutes', Input::get('expire'))));
     $new_paste->expire = $expire_time;
     if (!$new_paste->save()) {
         Debugbar::error('Saving failed!');
     }
     // Check if tags are set
     if (Input::has('hidden-tags')) {
         $tags = explode(' ', Input::get('hidden-tags'));
         foreach ($tags as $key => $tag) {
             $tag_model = new Tag();
             $tag_model->tag = $tag;
             $tag_model->paste_id = $new_paste->id;
             $new_paste->tags()->save($tag_model);
         }
     }
     if ($new_paste->id) {
         return Redirect::route('paste.show', $new_paste->token)->withCookie(Cookie::make('edittoken', $new_paste->token, 30));
     }
     return view::make('paste.form', array('page_title' => 'Create a paste'));
 }