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 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器