加入/未加入用户的按钮行为有所不同 [英] Button behavior to be different for joined / unjoined users

查看:50
本文介绍了加入/未加入用户的按钮行为有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序允许用户单击绿色的加入"按钮来加入组.单击后,他们应该会看到红色的取消加入"按钮.

My app allows users to join groups if they click a green 'Join' button. Once clicked, they should then see a RED 'unjoin' button.

这是我的按钮:

@if(Auth::user() == '')

<form method="post" action="{{ route('groups.join') }}">
@csrf
<button class="btn btn-success" type="submit">Join Group</button>
<input type="hidden" name="group_id" value="{{ $group->id }}" />
</form>

@else

<form action="{{ route('groups.destroy', $group->id)}}" method="post">
@csrf
@method('DELETE')
<button class="btn btn-sm btn-danger" type="submit">Unjoin</button>
</form>

@endif

已注销的用户看到绿色,而已登录的用户看到红色,这是正确的,但已登录的用户即使已加入该组也始终看到红色.嗯?

Logged out users see GREEN and logged in see RED, which is correct, but logged in users ALWAYS see red, even if they have joined the group. Hmmm?

我认为问题是第一个if语句,因为它仅检查登录的用户,而实际上更不可能检查具有以下身份的用户 加入了小组.所以,我尝试了这个:

I think the issue is the first if statement because it's only checking for logged in users, when actually it's more imposrant to check for users who have joined the group. So, I tried this:

@if(Auth::user() == 'joinedUsers')

但是它仍然仅显示一个红色按钮,仅当用户已加入一个组时,如何将查询更改为显示红色按钮?

but it still only shows a RED button, how do I change query to show RED button only if a group has been joined by user?

GroupController.php

GroupController.php

<?php

namespace App\Http\Controllers;

use App\Group;
use Illuminate\Http\Request;

// All Groups pages require login except 'show'
class GroupsController extends Controller
{

public function __construct()
{
$this->middleware('auth', ['except' => 'show']);
}

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
$groups = Group::where('created_by_user_id', auth()->id())->get();

return view('groups/index', compact('groups'));
}

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function joined()
{
//@todo change query to show groups joined
// $groups = Group::where('created_by_user_id', auth()->id())->get();
// $groups = Group::with('joinedUsers')

$groups = auth()->user()->groupsJoined()->get();

return view('groups/joined', compact('groups'));
}

/**
 * Store the group that a user has joined in storage.
 *
 * @param\Illuminate\Http\Request$request
 * @return \Illuminate\Http\Response
 */
public function join(Request $request)
{
$request->validate([
'group_id' => 'required',
]);

$group = Group::find($request->get('group_id'));
$group->joinedUsers()->attach(auth()->id());

return redirect('groups/joined')->with('success', 'You joined the group!!');
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
return view('groups.create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param\Illuminate\Http\Request$request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)

{
$request->validate([
'group_title' => 'required',
'group_description' => 'required',
'group_date' => 'required',
'group_time' => 'required',
]);

$group = new Group([
'group_title' => $request->get('group_title'),
'group_description' => $request->get('group_description'),
'group_date' => $request->get('group_date'),
'group_time' => $request->get('group_time'),
]);
$group->save();
return redirect('/groups')->with('success', 'Group saved!!');
}

/**
 * Display the specified resource.
 *
 * @paramint$id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
// $group = Group::find($id);
$group = Group::with('createdByUser')->where('id', $id)->first();
return view('groups.show', compact('group'));
}

/**
 * Show the form for editing the specified resource.
 *
 * @paramint$id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
$group = Group::find($id);
return view('groups.edit', compact('group'));
}

/**
 * Update the specified resource in storage.
 *
 * @param\Illuminate\Http\Request$request
 * @paramint$id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
$request->validate([
'group_title' => 'required',
'group_description' => 'required',
'group_date' => 'required',
'group_time' => 'required',
]);

$group = Group::find($id);
$group->group_title =$request->get('group_title');
$group->group_description = $request->get('group_description');
$group->group_date = $request->get('group_date');
$group->group_time = $request->get('group_time');
$group->save();
return redirect('/groups')->with('success', 'Group updated!');
}

/**
 * Remove the specified resource from storage.
 *
 * @paramint$id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
$group = Group::find($id);
$group->delete();
return redirect('/groups')->with('success', 'Group deleted!');
}
}

User.php

     */
    public function groupsJoined()
    {
        return $this->belongsToMany(Group::class, 'group_joined_user', 'user_id', 'group_id')
            ->withTimestamps();
    }
}

推荐答案

有多个组,那么如何确定要与表单连接的组?也许表格可能是这样的?

There are multiple groups, so how do you determine which group to join with your form? Maybe the form could be like this?

@foreach($groups as $group) {
    @if(auth()->user()->hasJoinedGroup($group))
        <form method="post" action="{{ route('groups.join', $group->id) }}">
          @csrf
          <button class="btn btn-success" type="submit">Join Group</button>
        </form>
    @else
        <form action="{{ route('groups.destroy', $group->id)}}" method="post">
            @csrf
            @method('DELETE')
            <button class="btn btn-danger" type="submit">Leave group</button>
        </form>
    @endif
    </form>
@endforeach

在控制器中:

The join function: 
/**
 * Store the group that a user has joined in storage.
 *
 * @param\Illuminate\Http\Request$request
 * @return \Illuminate\Http\Response
 */
public function join(Request $request, $groupId)
{

$group = Group::find($groupId);
$group->joinedUsers()->attach(auth()->id());

return redirect('groups/joined')->with('success', 'You joined the group!!');
}

类似地调整其他功能,例如取消连接&破坏.在用户模型中,

Similarly adjust other functions like unjoin & destroy. In User Model,

public function hasJoinedGroup($group){
    foreach($user->joinedGroups()->all() as $joinedGroup){
        if($joinedGroup->id == $group->id){
            return true;
        }
    }
    return false;
}

在GroupController的show()函数中,

In show() function in GroupController,

public function show(){
    $groups = Group::all();
    return view('groups.show',compact('groups'))'
}

这篇关于加入/未加入用户的按钮行为有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆