Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

niklas's avatar

Rating and views

hello I´m wery new in laravel, but i´m trying to understand it. Im trying to figurate how to add ratings on a page.

I have this in my /app/models/qustions.php

protected function getUpVotesAttribute($value)
    {
        return (int) $value;
    }

    protected function getDownVotesAttribute($value)
    {
        return (int) $value;
    }
    
    protected function getVotedAttribute()
    {
        if (Auth::guest() || !$this->user) {
            return null;
        }

        return questions_votes::where('users_id', Auth::user()->id)
                          ->where('questions_id', $this->id)
                          ->pluck('type');
    }

But i dont now how to get in on the page.

And the last thing i´m trying to do is add count views, how many people visit a page.

And for that i find this:

public function addclick()  {

    $this->views = $this->views + 1;
    $this->save();

    }

    public function show($id) {

     $question = Questions::find($id)->first();
     $question->addclick();

     $data['question'] =  $question;

     return View::make( 'questions.show', $data );

    }

But like the other i dont now how to move forward... I hope some one can help me.

0 likes
2 replies
bobbybouwmann's avatar

Laravel uses accessors and mutators. Since you pass your information to the view you will be able to access it. In your case you can do this in your view:

$question->upVotes;
$question->downVotes;

Well you get the idea.

Make sure to read the documentation before you continue

Source: http://laravel.com/docs/5.1/eloquent-mutators#accessors-and-mutators

Also, it's very common to use the compact function in your controller to pass data to your views

public function show($id) 
{
    $question = Questions::find($id)->first();
    $question->addclick();

    // This will do the same thing as $data['question'], but then more readable
    return View::make( 'questions.show', compact('question')); 
}
niklas's avatar

In the root i have questions.php and index.php I list all the questions in index.php with links to the questions.php Then i got /app/models/questions.php And last /app/views/questions.php

to show all my questions i use:

<?php 
$questions = DB::table('questions')->take(10)->get();
    foreach ($questions as $question) {
                                    
echo '<li class="item item-link" id="';echo $question->id;echo '">';
echo '<p class="question"><span class="question-text">';
echo '<a href="questions.php?q=';echo $question->id;echo '">';
echo $question->message;
echo '</a></span></p><div class="date">';
echo $question->views;
echo '</div><div class="date">0</div><div class="date">';
echo $question->date;
echo '</div></li>';
}
?>

And then in questions.php in the same page as index i have to show the single questions :

$question = Question::where('id', $_GET['q'])->first();

The thing i don´t understand is, how do i show the things in questions.php in root from the app/views/questions.php app/models/questions.php

Please or to participate in this conversation.