vendredi 28 décembre 2018

I want favorite button for different stories to work properly

I am facing a problem in PHP Laravel project. I have a favorite button for stories. Different users will have an option to make favorite the story they liked. "Stories" and "Favorite_Story" have separate tables. I have fetched all stories from the stories table, and fav_story from "fav_story" table, based on user_id.

This is my controller code

public function fanfiction(){
    $user_id = session('userid');
    $data['stories'] = DB::Table('stories')->orderBy('story_id', 'des')->get();
    $data['fav_story'] = DB::Table('favorite_story')->where('user_id', $user_id)->get();
    return view('fanfiction', $data);
}

This is my view code

@foreach($stories as $row)
                <?php $story_id = $row->story_id; ?>
                <article class="post excerpt">
                    <a href="" id="featured-thumbnail">
                        <div class="featured-thumbnail">
                            @if($row->img == '')
                            <img class="img-responsive" width="30%" src="" alt="Story Image" />
                            @else
                            <img class="img-responsive"  width="30%" src="" alt="Story Image" />
                            @endif
                        </div>                      
                    </a>
                    <div class="post-content" style="text-align:justify;">
                        ;
                        <h3></h3>
                    </div>

                    <div class="readMore">
                        @if(Session('username'))
                            @foreach($fav_story as $row1)
                                @if($row1->story_id == $story_id)
                                <a href="" style="background:#59AAE1;" > Unfavorite</a>
                                @elseif($story_id)
                                    <a href="" style="background:#1dbf73;" > Favorite</a>
                                @endif
                            @endforeach
                        @endif
                    </div>
                </article>
                @endforeach

The stories which are marked as a favorite story will show an unfavorite button with that specific story, which will be made favorite on again click on that button. The problem is, it shows both favorite and unfavorite button with that specific story and not button for favorite is shown which needs to have this button to make it as favorite. Please help me asap. Thank you

Aucun commentaire:

Enregistrer un commentaire