删除按钮隐藏在帖子的作者上

时间:2017-07-20 10:26:45

标签: javascript php jquery laravel

我在删除按钮中添加了以下if语句:

@if(Auth::user() == $status->user)
                    <button type="submit" class="btn btn-danger">

                        <a href="{{ route('user.status.delete',['status_id'=>$status->id]) }}">Delete</a>


                        @endif

它应该隐藏其他帖子的按钮,只显示在您的帖子中,但它也隐藏在您自己的帖子中。

以下全班

<div class="panel panel-default">
    <div class="panel-heading">{{$user->name}} - {{$status->created_at->diffForHumans()}} </div>

    <div class="panel-body">
        <div class="row">
            <div class="col-md-1">
                <img src="{{$user->getAvatar()}}" class="img-responsive">
            </div>

        <div class="col-md-11">
    <p>{{$status->status_text}}</p>
            @if($status->type == 1)
                <img src="{{asset('status_images/'.$status->image_url)}}" class="img-responsive" style="width: 100%;">
                @endif
        </div>
        <div class="col-md-12">
            <hr>
            <ul class="list-unstyled list-inline">

                <li>
                    <button class="btn btn-xs btn-info" type="button" data-toggle="collapse" data-target="#view-comments-{{$status->id}}" aria-expanded="false" aria-controls="view-comments-{{$status->id}}">
                        <i class="fa fa-comments-o"></i>View & Comment</button>
                </li>

                       <li>
                            @if(\App\Eloquent\StatusLikes::where(['status_id'=>$status->id,'user_id'=>Auth::user()->id])->first())
                           @else

                    {!! Form::open() !!}
                    {!! Form::hidden('like_status',$status->id)!!}
                    <button type="submit" class="btn btn-info btn-xs">
                        <i class="fa fa-thumbs-up"></i>Like status
                    </button>



                    {!! Form::close ()!!}


                       </li>

                        @if(Auth::user() == $status->user)
                    <button type="submit" class="btn btn-danger">

                        <a href="{{ route('user.status.delete',['status_id'=>$status->id]) }}">Delete</a>


                        @endif

                    </button>




                           @endif


                <li>
                    {{$comment_count}} comments


                </li>

                <li>

                    {{$like_count}} likes

                </li>
            </ul>

        </div>
        </div>
        <div class="panel-footer clearfix">



          {!! Form::open() !!}
            {!! Form::hidden('post_comment',$status->id) !!}

            <div class="form-group">
                <div class="input-group">
                    <input type="text" class="form-control" name="comment-text" id="comment-text" placeholder="Post a comment...">
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="submit"><i class="fa fa-send"></i>Add</button>
                    </span>
                </div>
            </div>

            {!! Form::close() !!}


            <div class="collapse" id="view-comments-{{$status->id}}">

                @if($comments->first())
                    @foreach($comments as $comment)


                         <div class="row">

                             <div class="col-md-1">

                                 <img src="{{\App\Eloquent\User::find($comment->user_id)->getAvatar()}}" class="img-responsive">
                             </div>


                        <div class="col-md-11">
                            <ul class="list-inline list-unstyled">

                                <li>

                                    <a href="">{{\App\Eloquent\User::find($comment->user_id)->name}}</a>

                                </li>

                                <li>
                                    posted {{$comment->created_at->diffForHumans()}}

                                </li>
                            </ul>


                            <p> {{$comment->comment_text}}</p>

                        </div>

                         </div>
                     <hr>
                    @endforeach
                    @else
                    <p>This status contains no comments.</p>
                    @endif
        </div>
        </div>
</div>
</div>

CONTROLLER

<?php

namespace App\Http\Controllers;

use App\Eloquent\Status;
use App\Friends;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Laracasts\Flash\Flash;
//use Request;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\DB;


class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        if (Input::has('like_status')) {
            $status = Input::get('like_status');

            $selectedStatus = Status::find($status);

            $selectedStatus->likes()->create([
                'user_id' => Auth::user()->id,
                'status_id' => $status
            ]);
            return redirect(route('home'));
        }

        if (Input::has('post_comment')) {
            $status = Input::get('post_comment');
            $commentBox = Input::get('comment-text');
            $selectedStatus = Status::find($status);

            $selectedStatus->comments()->create([

                'comment_text' => $commentBox,
                'user_id' => Auth::user()->id,
                'status_id' => $status
            ]);
            Flash::message('Your comment has been posted');
            return redirect(route('home'));

        }
        if (Input::has('status-text')) {
            $text = e(Input::get('status-text'));

            $rules = [
                'status-text' => 'required|string',


            ];

            $vaildator = Validator::make($request->all(), $rules);
            if (Input::hasFile('status_image_upload')) {

                $file = Input::file('status_image_upload');
                $mime = $file->getMimeType();

                $image = $request->file('status_image_upload');

                $imageName = str_random(8) . '_' . $image->getClientOriginalName();
                //$imageFull = str_random(8).'_'.$image->getClientOriginalExtension();

                $userStatus = new Status();
                $userStatus->status_text = $text;
                $userStatus->image_url = $imageName;
                $userStatus->type = 1;
                $userStatus->user_id = Auth::user()->id;


                if ($mime == "video/x-flv" || $mime == "video/mp4" || $mime == "application/x-mpegURL" || $mime == "video/MP2T" || $mime == "video/3gpp" || $mime == "video/quicktime" || $mime == "video/x-msvideo" || $mime == "video/x-ms-wmv") {//process upload}

                    $vaildator = Validator::make($request->all(), $rules);
                    if (!$vaildator->fails()) {
                        $image->move('status_videos', $imageName);
                        $userStatus->type = 2;
                        $userStatus->save();
                        Flash::success('Your status has been posted');
                        return redirect(route('home'));
                    } else {
                        return back()->with('error', 'Validation failed:' . $vaildator->errors);
                    }
                } else {
                    $rules['status_image_upload'] = 'image';
                    $vaildator = Validator::make($request->all(), $rules);
                    if (!$vaildator->fails()) {
                        $image->move('status_images', $imageName);
                        $userStatus->type = 1;
                        $userStatus->save();
                        Flash::success('Your status has been posted');
                        return redirect(route('home'));
                    } else {
                        return back()->with('error', 'Validation failed:' . $vaildator->errors);
                    }
                }


            } else {
            }
            if (!$vaildator->fails()) {
                $userStatus = new Status();
                $userStatus->status_text = $text;
                $userStatus->user_id = Auth::user()->id;
                $userStatus->save();
                Flash::success('Your status has been posted');
                return redirect(route('home'));
            } else {
                return back()->with('error', 'Validation failed:' . $vaildator->errors);
            }


        }


        //Get top 15 post
        $user_id = Auth::id();
        $all_friends = Friends::where(['friend_id' => $user_id, 'status' => 1])
            ->orWhere(['user_id' => $user_id, 'status' => 1])
            ->get();

        $friends = [];
        foreach ($all_friends as $val) {
            array_push($friends, $val->user_id, $val->friend_id);
        }

        $unique_friends = array_unique($friends);

        if (!empty($unique_friends)) {
            $top_15_posts = Status::whereIn('user_id', $unique_friends)
                ->orderBy('id', 'DESC')
                ->take(15)
                ->get();
        } else {
            $top_15_posts = Status::where('user_id', $user_id)
                ->orderBy('id', 'DESC')
                ->take(15)
                ->get();
        }

        //Get available friend request
        $user_id = Auth::id();
        $available_req_count = Friends::where(['friend_id' => $user_id, 'status' => 0])
            ->orderBy('id', 'desc')
            ->get()
            ->count();

        return view('home', [
            'top_15_posts' => $top_15_posts,
            'available_req_count' => $available_req_count

        ]);


    }
    public function getDeleteStatus($status_id)
    {
        $status =Status::where('id',$status_id)->first();
        if(Auth::user() != $status->user){
            return redirect()->back();
        }
        $status->delete();
        return redirect()->route('home')->with(['message' => 'Successfully deleted!']);
    }

}

2 个答案:

答案 0 :(得分:0)

使用此代码。它会起作用。

SELECT `user_id`, 
  GROUP_CONCAT(CASE WHEN `field_id` = 'field_2' THEN `field_value` ELSE NULL END) AS field_2,
  GROUP_CONCAT(CASE WHEN `field_id` = 'field_3' THEN `field_value` ELSE NULL END) AS field_3
FROM `my_table`  
GROUP BY `user_id`
having sum(field_id = 'field_1' and field_value = 'Summer') > 0;

我认为您只想向帖子的所有者显示删除按钮。在这种情况下,如果@if(Auth::id() == $status->user->id) // Show Delete button @endif 模型位于user变量中,此代码将有效。

答案 1 :(得分:0)

尝试下面给出的代码,如果它不适合你,请告诉我:

@if(Auth::id() == $status->user_id)
<button type="submit" class="btn btn-danger">
    <a href="{{ route('user.status.delete',['status_id'=>$status->id]) }}">Delete</a>
</button>
@endif