spAo's avatar
Level 1

Laravel poll package

Hello all i need to create poll system on my web i found some package its called public function inani/larapoll - > https://github.com/akiyamaSM/larapoll

i installed package but i don't know how it works

controller

public function socialog()
    {

        $poll = new Poll([
            'question' => 'What is the best PHP framework?'
        ]);

        // attach options and how many options you can vote to
        // more than 1 options will be considered as checkboxes
        $poll->addOptions(['Laravel', 'Zend', 'Symfony', 'Cake'])
            ->maxSelection() // you can ignore it as well
            ->generate();
        $poll->isRadio(); // true
        $poll->isCheckable(); // false
        $poll->optionsNumber(); // 4

        return view('pages/socialog/socialog')
            ->with('socialog');
    }

on blade

{{ PollWriter::draw(Poll) }}

buts its errors me telling that addOptions needed so i created models Poll and PollOption

Poll

public function addOptions()
    {
        return $this->hasMany(PollOption::class, 'poll_id', 'id');
    }
}

PollOption

public function addOptions()
    {
        return $this->belongsTo(Poll::class, 'poll_id', 'id');
    }

now its errors ' Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::maxSelection() '

if its full package why i must create models ? i think i'm doing something wrong ( can anyone please help me : )

0 likes
8 replies
CorvS's avatar

You don't have to create models yourself, the Poll model already ships with the package and is all you need. If the addOptions() method is missing you probably didn't import the right Poll model? It has to be Inani\Larapoll\Poll.

You didn't pass your poll to your view and {{ PollWriter::draw(Poll) }} doesn't make sense either. Pass the poll to the view

...
return view('pages/socialog/socialog', ['poll' => $poll]);

and then draw it

{{ PollWriter::draw($poll) }}

as shown in the README of the package.

1 like
spAo's avatar
Level 1

thanks for answer

don't know why its don't import poll models i did everything what instruction told me to do

composer require inani/larapoll

then included service provider config/app.php

'providers' => [
    ...
    Inani\Larapoll\LarapollServiceProvider::class,
    ...
];


'aliases' => [
        ...
        'PollWriter' => Inani\Larapoll\PollWriterFacade::class,
        ...
];

then published Inani\Larapoll\LarapollServiceProvider and migrated it its add me larapoll_config and 4 migrations

i missed something ( don't understand why its not adding models ?

CorvS's avatar
CorvS
Best Answer
Level 27

Are you sure the package was installed? Did you check if the package is inside your vendor folder? Try running composer install to see if that changes anything. Have you checked your controller if

use Inani\Larapoll\Poll;

is at the top and that your IDE doesn't have any complaints?

1 like
spAo's avatar
Level 1

now its working in html on {{ PollWriter::draw($poll) }} its showing ' Thanks for voting '

i'm new and i'm having really dummy questions : P from where can i add a poll? in description how i understand there must be interface to add poll?

spAo's avatar
Level 1

ok roger that big thanks for help <3

1 like
spAo's avatar
Level 1

i' having trouble again

i created poll and add its current poll to html {{ PollWriter::draw(Inani\Larapoll\Poll::find(39)) }} and its showing poll and everything is perfect but when i vote its not showing me result after voting its telling me ' Thanks for voting ' but wheres result?

Please or to participate in this conversation.