コード例 #1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     $posts = Post::all();
     $category = new Category();
     $category->name = "Main category";
     $category->generateSlug();
     $category->save();
     foreach ($posts as $post) {
         $post->categories()->attach($category);
         $post->save();
     }
 }
コード例 #2
0
ファイル: PostSeeder.php プロジェクト: thevruno/my-own-blog
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     for ($i = 0; $i < 20; $i++) {
         $paragraph = rand(1, 7);
         $data = json_decode(file_get_contents("http://www.randomtext.me/api/lorem/p-" . $paragraph . "/" . rand(3, 20) . "" . rand(20, 45)));
         $description = function () use($data) {
             $desc = explode("</p>", $data->text_out);
             return $desc[0] . "</p>";
         };
         $title = function () use($data) {
             $text = explode(" ", str_replace(["<p>", "</p>"], "", $data->text_out));
             $wordCount = rand(1, 8);
             $start = rand(1, count($text) - $wordCount);
             $wordCount += $start;
             $title = "";
             while ($start < $wordCount) {
                 $title .= $text[$start] . " ";
                 $start++;
             }
             while (true) {
                 if ($post = Post::whereTitle($title)->first()) {
                     $title .= ".";
                 } else {
                     return $title;
                 }
             }
             return $title;
         };
         $post = new Post();
         $post->description = $description();
         $post->content = $data->text_out;
         $post->title = $title();
         $post->generateSlug();
         $post->user_id = 1;
         $post->save();
     }
 }
コード例 #3
0
 /**
  * Get post from blog by slug
  *
  * @param $slug
  * @return mixed
  */
 public function getPost($slug)
 {
     $post = Post::whereSlug($slug)->first();
     if (!$post) {
         throw (new ModelNotFoundException())->setModel(get_class($post));
     } else {
         //TODO:: Move it from there and add some logic :)
         if ($viewed = \Session::get("viewed")) {
             if (!isset($viewed[$slug])) {
                 $viewed[$slug] = true;
                 \Session::set("viewed", $viewed);
                 $post->views++;
                 $post->save();
             }
         } else {
             $viewed[$slug] = true;
             \Session::set("viewed", $viewed);
             $post->views++;
             $post->save();
         }
         return view("pages.post")->withPost($post);
     }
 }
コード例 #4
0
 public function listByTag(Post $blog, $tag)
 {
     $posts = $blog->byTag($tag);
     return view('blog', compact('posts'));
 }
コード例 #5
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     $permission = Post::find($id);
     return ["success" => $permission ? $permission->delete() : false];
 }