Downloads
Stars
Version
A package for managing polls in Laravel.
Open your composer.json
file and add the following to the require
key:
"data33/laravel-poll": "^1.0.1"
After adding the key, run composer update from the command line to install the package
composer update data33/laravel-poll
Before you can start using the package we need to set some configurations.
To do so you must first publish the config file, you can do this with the following artisan
command.
php artisan vendor:publish --provider="Data33\LaravelPoll\PollServiceProvider"
After running the above command, you should see the following changes:
config/polls.php
database/migrations
folderresources/views/vendor/data33/laravel-poll
If you wish to extend this package, you can specify your own classes to be used as models in the config/polls.php
file. Make sure your models extend the existing ones.
You can either create polls manually by using the models directly, or you can use the createPoll helper method.
use Data33\LaravelPoll\Models\Poll;
$title = 'A new poll';
$text = 'A longer text describing the poll';
$type = Poll::TYPE_SINGLE;
// $type = Poll::TYPE_MULTIPLE;
$options = [
'The first option',
'The second option',
'The third option',
];
$endDate = new Carbon\Carbon('2018-12-24 00:00:00');
$poll = Poll::createPoll($title, $text, $type, $options, $endDate);
If you need to add more options to a poll after it has already been created you may use the addOption method.
$poll->addOption('A fourth option')
->addOption('A fifth option');
Let's assume your App\User model uses the Voter trait.
auth()->user()
->voteFor($poll, $pollOption);
if (auth()->user()->hasVotedInPoll($poll)) {
echo 'User voted in poll';
}
if (auth()->user()->hasVotedForOption($poll, $option)) {
echo 'User voted for specific option';
}