Laraval-Admin小技巧

  1. Grid模型,自定义显示字段,此字段需要两个拼接两个数据库字段如下:
...
// 不存在的`full_name`字段,自定义的显示字段
$grid->column('full_name','全名')->display(function () {
    return $this->first_name . ' ' . $this->last_name;
});

  1. Show中,两个模型PostComment之间是一对多的关系,关闭右上角的Button,以及关闭每行的按钮(编辑、查看、删除),其实在闭包中,使用使用的方式与Grid是一样的
	protected function detail($id)
 	{

        $show = new Show(Htxq::findOrFail($id));
        $show->id('话题回复编号');

        $show->user_id('回复人')->as(function ($userID){
            return User::findOrFail($userID)->nickname;
        });
        $show->content('回复内容');

        $show->comments('评论',function ($comments){
            $comments->setResource('/admin/comments');
            $comments->model()->select('app_comments.*')
                ->join('app_users','app_users.id','=','app_comments.author_id');
            $comments->id('评论编号');
            $comments->author_id('评论人')->display(function ($userID){
                return User::findOrFail($userID)->nickname;
            });
            $comments->post_content('评论内容');

            $comments->is_publish('审核状态')->editable('select', [0=>'未审核',1=>'已通过',2=>'已拒绝']);
            //关闭右上角的创建按钮
            $comments->disableCreateButton();
			
            $comments->actions(function ($actions) {
            	//禁用删除
                //$actions->disableDelete();
                //禁用编辑
                //$actions->disableEdit();
                //禁用查看
                $actions->disableView();
            });

            return $comments;

        });



        return $show;
	}
  1. Form保存前或保存中执行其他操作
	//保存前回调
	$form->saving(function (Form $form) {
	     $user_id = $form->model()->user_id;
	     //use user_id to do something
	     return $form;
	});
  1. 保存Form输出成功信息

// 抛出成功信息
$form->saving(function ($form) {

    $success = new MessageBag([
        'title'   => "I'm title...",
        'message' => "I'm message....",
    ]);

    return back()->with(compact('success'));
});

参考:

[1] https://github.com/z-song/laravel-admin/issues/2075

你可能感兴趣的:(Laravel-Admin)