unrulynatives/attitudes

1.4.0 2017-02-05 13:15 UTC

This package is not auto-updated.

Last update: 2024-09-23 12:58:09 UTC


README

这是一个适用于 Laravel >= 5.3 的完整点赞和踩票解决方案。

  • 允许点赞和踩票,可以是二进制或多个步骤。
  • 记录投票用户的备忘录(评论)在同一表中。
  • 为从 rtconner/laravel-likeable 包迁移数据提供了开箱即用的迁移工具。
  • 允许将对象分配到收藏夹分类(每个投票对象只能有一个)。

欢迎贡献者。有任何功能建议?请提交问题。

当前版本

Latest Stable Version Total Downloads

所有功能的列表

该包提供几种在同一数据库表中工作的解决方案。选择一个或全部使用以获得所需的功能。存储点赞和踩票的三个值由您决定:二进制、负值或 null

  • 重要性 - 可以用于观察和优先处理具有 010 范围内值的(包括负值)内容。

  • 态度 - 可以存储 -101 值,表示不喜欢和喜欢(点赞和踩票)。如上所述,值的范围可以由开发者决定,例如 -2 表示恨,-3 表示对对象极度不满。

  • 激活 - 例如,该字段可以存储 -101 值。对于内容过滤功能很有用。

  • favoritetype_id - 仅作为未来开发的脚手架。一个项目可以存储在某个子分类下的收藏夹中。如果 null,则表示根目录。此功能将需要一个额外的模型 Favoritetype

  • user_notes - 允许用户对模型进行笔记(尚未完成)。建议最大字符数为 150,以便表格不会过重。

待办事项

  • 投票计数器函数
  • 各种投票计数器情况的示例视图
  • 添加用户笔记的示例
  • 从 rtconner 迁移时拆分集合 - 现在它提交到 PHP 超时限制

贡献

虽然该包现在运行得很好,但我需要一些时间来使代码看起来更专业。欢迎贡献者!

安装

  1. 将此行添加到您的 'composer.json`

"unrulynatives/attitudes": "^1.0"

  1. config/app.php 中注册服务提供者。

    Unrulynatives\Attitudes\AttitudesServiceProvider::class,

  2. 在您的 User.php 模型中注册此特性。

    use Unrulynatives\Attitudes\AttitudesUserExtensions;


    class User extends Authenticatable
    {

    use AttitudesUserExtensions;

该特性将使这两个函数可用。 (作为替代,您可以直接在模型文件中实现它们。)


    
    public function importances() {
        return $this->hasMany(\App\Models\Userattitude::class, 'creator_id');
    }
    
    public function attitudes() {
        return $this->hasMany(\App\Models\Userattitude::class, 'creator_id');
    }

  1. 在模型中注册特性

对所有要启用投票系统的模型,注册此特性

use Unrulynatives\Attitudes\UserAttitudes;

class Yourmodelname extends Model  {

    use UserAttitudes;

该特性将使以下三个函数可用。 (作为替代,您可以直接在模型文件中实现它们。)

    
    public function importances() {
        return $this->hasMany(\App\Models\Userattitude::class, 'creator_id');
    }
    
    public function attitudes() {
        return $this->hasMany(\App\Models\Userattitude::class, 'creator_id');
    }


    public function user_approach($user)
    {
        return $this->morphMany(\App\Models\Userattitude::class, 'item')->where('creator_id', ($user ? $user->id : NULL))->first();
    }


        

  1. 发布资产 发布此包中的迁移、视图、控制器和其他资产到您的 Laravel 应用

php artisan vendor:publish --provider="Unrulynatives\Attitudes\AttitudesServiceProvider" --force

  1. 运行迁移 现在运行迁移,使用命令 php artisan migrate。验证表 userattitudes 是否已创建。

注意:确保在副本上进行调整,因为此包的新版本可能会覆盖您的更改(注意发布命令中的 --force 选项。)

  1. (可选) 设置演示功能

尽管演示页面的数据库将由控制器函数填充,您也可以在这里填充数据库。

在您的 \database\seeds\DatabaseSeeder.php 文件的 run() 函数中声明此填充文件:\database\seeds\UnAQuotationsTableSeeder.php 并运行 php artisan db:seed

8. (optional) Use your own routes 

Properly working routes necessary for this package to work are locatad within ths package:

Route::any('{itemkind}/{id}/set_user_attitude', ['as' => 'attitudes.set_user_attitude', 'uses' => 'AttitudesController@set_user_attitude']); Route::any('{itemkind}/{id}/set_user_importance', ['as' => 'attitudes.set_user_importance', 'uses' => 'AttitudesController@set_user_importance']);


You can put them in your location of choice, but make sure that the new location is parsed first. In case of problems, reverse the order of service providers of this package and `App\Providers\RouteServiceProvider::class,`


9. Attach the js and css files to your template. 

Mind the file paths if you decide to place them somewhere else than they are published to.

<script type="text/javascript" src="{{URL::to('js/minitool_attitudes.js')}}"></script>



10. Include the below view files in your `foreach` loop. Note that the looped variable should be changed accordingly. Here I use `$o->`.

<?php $itemkind = 'quotes'; ?> // features is a name of your model
@include('userattitudes._userattitudes_attitude_toggle_abstracted', ['itemkind' => $itemkind,'o' => $o, 'attitude' => (($cua = $o->user_approach(Auth::user())) ? $cua->attitude : NULL)])

@include('userattitudes._userattitudes_importance_toggle_abstracted', ['itemkind' => $itemkind,'o' => $o, 'importance' => (($cua = $o->user_approach(Auth::user())) ? $cua->importance : NULL)])
Note: `itemkind` is plural lovercase name of your model. Take a look at the controller function: the `itemkind` name is changed into class name in order to process your request.

11. Define the morph class
The models which are attituded should have the morph class defined. The names are stored in the DB table `userattitudes` in column `item_type`.
I myself define the morph class definitions manually, just to be sure that Laravel functions won't use some unusual defaults. 
Do it in `app\Providers\AppServiceProvider.php`. Use the instructions in https://laravel.net.cn/docs/5.3/upgrade.


For this package to work with predefined example, open the file and inside the 
put the following code:

use Illuminate\Database\Eloquent\Relations\Relation;

// ...

public function boot()
{

    Relation::morphMap([
        'user' => User::class,
        'quotation' => \App\Models\UnAQuotation::class,
    ]);
}

12. Define a csrf token
Be sure that the head of your page contains the below declaration, otherwise you will meet with unexpected behavior of the script:
`<meta name="csrf-token" content="{{ csrf_token() }}" />`


### That's it! Now  user choices should be stored in the database table `userattitudes`.



## Working demo. 

If you installed this package correctly, point your browser to `attitudes-demo`.
- You should be logged in
- you sould have the `app\Models\Feature` model defined and at least one record in your database present. You can just simply use your own model for your test run - it's up to you.

An online demo is available at http://dev.unrulynatives.com/attitudes-demo


## Data migration from other packages

Added feature to migrate data from rtconner's package. See url `attitudes-migrate-likeable`



## Usage & Examples

### Count total votes of an object

`$this->countallvotes` - prints total number of votes cast by all users
`$this->countdownvotes`- prints total number of DOWN-votes cast by all users
`$this->countupvotes`- prints total number of UP-votes cast by all users


### collections
- `Quotation::whereUpvotedBy(Auth::id())->get();` // collection of Quotations upvoted by the user
- `Quotation::whereDownvotedBy(Auth::id())->get();` // collection of Quotations downvoted by the user
- `Quotation::whereVotedBy(Auth::id())->get();` // collection of Quotations voted in any way by the user (mind that the function counts records existing in DB, so it includes `null` and `0`)




### backend

IMPORTANT NOTE: In this package the assumed convention for model names is singular with capitalized first letter.
Mind the variable `itemkind`. It MUST be plural form of your model name.
If you have another system, you must rework the backend solutions of this package.

### Frontend

After publishing the views, in your app's `resources/views/` folder you will find a folder `userattitudes`. Inside you will find several files. In files included by the below templates you can define classes for your voting buttons and model affected by them. 
To have everything work well, start with including

`@include('userattitudes.set_attitudes_2')` 
(works with Bootstrap 4 and font Awesome (for thumbs up & down icons))





Other frontend solutions
`@include('userattitudes.set_attitudes_1')` will work nice with 
(works well with the UnrulyNatives Starter Kit package - need extra icon fonts installed)




## Help and documentation

point your browser to `attitudes-docs` to see instructions. If you cannot see the docs, then you have a problem to solve. Good luck!
After you use the `publish` command, the file will be located at `resources/views/userattitudes/features/index.blade.php`