社区评论Part 2
打赏作者

chenxin

对于您网站的评论编辑问题!我想问当我进入我自己的评论的编辑界面!我修改路由中的id,按道理会跳转到别人的评论的修改界面!而你的网站拒绝访问!我想说这个功能怎么实现的!

JellyBool 回复 chenxin

按道理会跳转到别人的评论的修改界面

按道理,就是禁止访问才对啊。你都不是评论的作者,逻辑上肯定不能让你去随便修改别人的评论啊。。。

chenxin 回复 JellyBool

对啊!我就是想问你怎么禁止的!我下面一个回复是我用的方法!您用的什么方法?

chenxin
 $comment=Comment::findOrFail($id);
 if(\Auth::user()->id==$comment->user->id)
 {
       return view('forum.editcomment',compact('comment'));
 }else{
        return 'you have no permission';
 }

我在控制器方法里面加了一个if判断!我想说怎样在roles生成的五个表中实现这样的一个判断!我想不明白的是这个权限表怎样表示出一个用户可以编辑自己的评论而不能编辑别人的评论这样的功能!通过在修改路由的id来edit的时候进行限制!

JellyBool 回复 chenxin

最简单就是这样啊。。。或者你使用 laravel 的 policy。

chenxin 回复 JellyBool

policy就是称之为策略类啊!有意思。。

JellyBool 回复 chenxin

恩,对的。在权限管理那个视频有讲到

lzy13121013

站长,请问一下,这个评论如何能实现顶贴功能呢?最新comment过的文章,在首页最前面显示。

JellyBool 回复 lzy13121013

根据 updated_at 来就可以了吧

Post::latest('updated_at')->get()
MarksGui888

Jelly,网站的社会化分享用的什么插件?

MarksGui888 回复 JellyBool

恩,谢谢啦!

jerryhu

Jelly,最近刚看了你这个关于评论的视频,但是还有很多细节的地方不是太清楚,很想知道你网站文章部分的评论是怎么做的,如果可以能否录个视频给大家分享一下呢

hate

Whoops, looks like something went wrong.

1/1
MassAssignmentException in Model.php line 424:
_token

hate

这单给我报一个 __token 啥意思

JellyBool 回复 hate

不要获取 _token 的值,检测是否有字段没有 fillable

hate 回复 JellyBool

class Comment extends Model
{
protected $fillabele = [‘body’,‘user_id’,‘discussion_id’];
public function user()
{
return $this->belongsTo(User::class);
}

public function discussion()
{
    return $this->belongsTo(Dicussion::class);  
}

}

class CommentsController extends Controller
{

public function store(Requests\PostCommentRequest $request)
{
    dd($request->all());
    Comment::create(array_merge($request->all(),['user_id'  => \Auth::user()->id]));

    return redirect()->action('PostsController@show',['id' => '2']);
}

}
在这里create 页面报__token

JellyBool 回复 hate

试试

Comment::create(array_merge($request->except('_token'),['user_id'  => \Auth::user()->id]));
hate 回复 JellyBool

Whoops, looks like something went wrong.

1/1
MassAssignmentException in Model.php line 424:
discussion_id

    Comment::create(array_merge($request->except('_token'),['user_id'  => \Auth::user()->id,'discussion_id' => $request->get('discussion_id')]));
JellyBool 回复 hate

单词写错了吧

protected $fillabele = ['body','user_id','discussion_id'];

$fillable 吧

hate 回复 JellyBool

好像是这么一回事。我直接用 protected $guarded = array();

kvcr945a

有没有计划出个多级评论的视频呢

dppppp

老师,这个评论怎么支持markdown语法呢?如下写的方式报错
image.png
image.png

dppppp

在构造方法里面也不行

JellyBool 回复 dppppp
(new Markdown)->markdown($value);

不要传 Markdown $markdown

dppppp
class Markdown
{
    protected  $parser;
    public function __construct(Parser $parser)
    {
        $this->parser = $parser;
    }

    public function markdown($text)
    {
        return $this->parser->makeHtml($text);
    }

}

markdown类是这么写的,但是还是报错

ErrorException in Markdown.php line 14:
Type error: Too few arguments to function App\Markdown\Markdown::__construct(), 0 passed in /Applications/MAMP/htdocs/la51/app/Comment.php on line 23 and exactly 1 expected (View: /Applications/MAMP/htdocs/la51/resources/views/forum/show.blade.php)
JellyBool 回复 dppppp

额。。。因为你依赖注入的时候没有穿 Parser啊

(new Markdown(new Parser))->markdown($value);
Nixus

老师,这节课结尾处,您提到的登录后,回到登录前的页面如何实现?

public function signup(UserLoginRequest $request, Closure $next)
{
    if (\Auth::attempt([
        'email'        => $request->email,
        'password'     => $request->password,
        'is_confirmed' => 1,
    ])) {
         return $next($request); // 这个不行报错了
         return redirect()->intended();// 这个也不行,还是跳转到了首页
    }
    \Session::flash('user_login_failed', '密码错误或邮箱没有验证');
    return redirect('/user/login')->withInput();
}

老师,评论框的行数,建议设多点,粘贴代码的时候,很不方便

JellyBool 回复 Nixus

好的,我设置一下可以自动伸缩吧

JellyBool 回复 Nixus

你试着体验一下 codecasts 的登录,在登录的时候会有一个 redirect_url 的参数,login 这个方法获取这个参数就好

Nixus 回复 JellyBool

OK!感谢!

Nixus 回复 JellyBool

感觉自己好笨啊,找了好久,又问朋友,才知道要用 Input::get()来接收?redirect=http://url.com

JellyBool 回复 Nixus

这个就。。。写多了就好

blaze0207 回复 JellyBool

請問我用 session 的方法去實現上述的功能可以嗎?
程式碼如下:

show.blade.php

{!! Session::put('discount_id', $discussion->id) !!}
<a href="/user/login" class="btn btn-block btn-success">登入發表評論</a>

UsersController.php

if (Auth::attempt([
     'email' => $request->email,
     'password' => $request->password,
     'is_confirmed' => 1
  ])) {
      $path = Session::get('discount_id') != '' ? '/discussions/' . Session::get('discount_id') : '/';
      Session::forget('discount_id');
      return redirect($path);
}

還是有更好的寫法呢?
想大大們指教~~!

JellyBool 回复 blaze0207

感觉也没啥更好的了

Flourishing 回复 JellyBool

应该有更好的方法吧 记得以前实现过 忘记了

paprikaLang

/var/folders/6d/wtj9rtxs6t112b5ndl31s4n00000gn/T/ro.nextwave.Snappy/ro.nextwave.Snappy/32DDB32C-7D41-48B6-8E21-892BDC0988EB.png

JellyBool 回复 paprikaLang

?这个是什么?

paprikaLang 回复 JellyBool

没事,搞定了! (__)

VatinT

试一下评论效果

VatinT

123444