codwelt/laravel-blog

此包的最新版本(v2.7.1)没有可用的许可证信息。

laravel博客

v2.7.1 2020-01-19 23:05 UTC

This package is auto-updated.

Last update: 2024-09-20 10:02:16 UTC


README

在项目根目录的命令行中执行以下命令

$ composer require codwelt/laravel-blog

或直接在 composer.json 文件中添加

{
    "require": {
        "codwelt/laravel-blog": "~2.0"
    }
    
}

您可以在 composer.json 文件中添加 script,并设置 post-update-cmd 事件,其中可以添加更新包时执行的 post 命令。

     "post-update-cmd":[
                "@php artisan codwelt_blog:update"
     ]

在项目内部执行此命令,这将执行博客的安装,包括迁移和发布配置文件

$ php artisan codwelt_blog:install

配置

文章创建者

为了创建文章,需要将其关联到模型,该模型是文章的作者。因此,需要指定项目中创建文章的模型。为此,在 config/blogCodwelt.php 路径下的 blogCodwelt.php 文件中,将包含如下数组

  'creatorPost' => [
        'model' => App\User::class,
        'columnOfRelation' => 'id'
    ],

model 键中,默认是用户模型的路径,但如果在项目中创建文章的模型不是用户模型,则替换用户模型并写入模型类的路径,请记住,它应以 ::class 结尾。名为 columnOfRelation 的键需要指示将文章与模型关联的列;默认是用户表的主键 id,因此如果模型表的主键不同,则需要更改它。

最后,在创建文章的模型中,必须使用名为 CreatorOfPosts 的 trait,其路径为 CodWelt\Blog\Operations\Core\Traits\CreatorOfPosts。此外,trait 具有一个名为 getName 的抽象方法,必须在模型中实现,并返回将显示为文章创建者名称的名称,在我们的例子中,这表示模型相同的可填充 name

   <?php
   
   namespace App;
   
   use Codwelt\Blog\Operations\Core\Traits\CreatorOfPosts;
   use Illuminate\Foundation\Auth\User as Authenticatable;
   
   class User extends Authenticatable
   {
       use CreatorOfPosts;
   
  
    
        
       /**
        * The attributes that are mass assignable.
        *
        * @var array
       */
       protected $fillable = ['id', 'name', 'email', 'password', 'cargo', 'imgperfil', 'telefono', 'emailsecundario', 'activo'];
            
       /**
        * Debe retornar el nombre de quien crea el post
        * @return string
        */
       public function getNameModel()
       {
           return $this->name;
       }
   }

文章评论者

文章评论可以由匿名用户或注册用户(使用 Laravel 中的模型)进行。如果希望评论显示评论者的姓名,则必须在配置文件中指定模型和关联评论与用户的列

  'commentatorPost' => [
        'model' => App\User::class,
        'columnOfRelation' => 'id'
    ],

model 键中,默认是用户模型的路径,但如果在项目中创建文章的模型不是用户模型,则替换用户模型并写入模型类的路径,请记住,它应以 ::class 结尾。名为 columnOfRelation 的键需要指示将文章与模型关联的列;默认是用户表的主键 id,因此如果模型表的主键不同,则需要更改它。

最后,在创建文章的模型中,必须使用名为 CommentatorOfPosts 的 trait,其路径为 CodWelt\Blog\Operations\Core\Traits\CommentatorOfPosts。此外,trait 具有一个名为 getNameModel 的抽象方法,必须在模型中实现,并返回将显示为文章创建者名称的名称,在我们的例子中,这表示模型相同的可填充 name。此外,还需要实现 getEmailModel 方法,它必须返回用户的电子邮件地址,以及 getUrlImageProfile 方法,它必须返回用户个人资料的图片 URL。

   <?php
   
   namespace App;
   
   use Codwelt\Blog\Operations\Core\Traits\CommentatorOfPosts;
   use Illuminate\Foundation\Auth\User as Authenticatable;
   
   class User extends Authenticatable
   {
       use CommentatorOfPosts;
   
  
    
        
       /**
        * The attributes that are mass assignable.
        *
        * @var array
       */
       protected $fillable = ['id', 'name', 'email', 'password', 'cargo', 'imgperfil', 'telefono', 'emailsecundario', 'activo'];
            
       /**
        * Debe retornar el nombre de quien crea el post
        * @return string
        */
       public function getNameModel()
       {
           return $this->name;
       }

        /**
        * Correo electronico que se mostrara en los comentarios
        * @return string
        */
       public function getEmailModel()
       {
           return $this->email;
       }

    /**
     *  Debe retornar la url de la foto de perfil del usuario, si se retorna nulo se asigna una foto por defecto
     * @return mixed
     */
    public function getUrlImageProfile(){
        $this->url_profile;
    }

   }