dqndc26628 2015-06-16 02:04
浏览 62
已采纳

从URL中检索Slug for function(Laravel 5)

I'm making an App where a group is created by a user. This user can go to the group page (automated unique slug is created for the groups).

My goal is for the member who created the group, to be able to make a invite code for that specific group.

Currently the code generator is hardcoded to create one for the group with id=8.

Here is the code:

public function createInviteCode()
    {
        $group = Group::find(8); // Group ID you want to manage
        $code = Invite::generateCode();
        $invite = $group->invites()->create([
            'code' => $code,
            'expires_at' => Carbon::now()->addDays(7) // invite expires in 7 days
        ]);

        return redirect()->back();
    } 

The code is generated on through a submit button on my showgroup.blade.php:

{!! Form::open(['route' => 'invite.create']) !!}
{!! Form::submit('Generate invite code', ['class' =>  'btn btn-primary']) !!}
{!! Form::close() !!}

What I am trying to do, is retrieve the slug that I'm surfing to, to show this group:

function to retrieve the slug:

public function search()
{
    $group = Group::whereSlug($slug)->first();

    return view('pages.groups.showgroup')->withSlug($group);
}

Also these are my Routes.php:

$router->post('invites', ['as' => 'invite.create', 'uses' => 'GroupController@createInviteCode']);
$router->get('group/{slug}', ['as' => 'group.search', 'uses' => 'GroupController@search']);

So a small recap: Currently I'm simply creating a invite code for the group with ID=8. What I want to do is change my function so it creates a invite code for the group that I'm surfing to (aka through the slug)

How do I code this exactly?

  • 写回答

1条回答 默认 最新

  • dongyao2001 2015-06-16 12:07
    关注

    Fixed it:

    Route:

    $router->post('invites/{slug}', ['as' => 'invite.create', 'uses' => 'GroupController@createInviteCode']);
    

    My form:

    {!! Form::open(['route' => ['invite.create', $group->slug]]) !!}
    {!! Form::submit('Generate invite code', ['class' =>  'btn btn-primary']) !!}
    {!! Form::close() !!}
    

    The function:

    public function createInviteCode($slug)
    {
        $group = Group::where('slug', $slug)->firstOrFail();
    
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 机器学习简单问题解决
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写