doubi6669 2017-07-20 10:26
浏览 47
已采纳

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

I added the following if statement to the delete button :

@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

It is supposed hide the button for other posts and only appear for your posts but it hides on your own posts too .

The whole class below

<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条回答 默认 最新

  • dsmvovm27249 2017-07-20 14:46
    关注

    Try below given code, let me know if it's not work for you:

    @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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办