加入/未加入用户的按钮行为不同

问题描述 投票:0回答:1

我的应用允许用户单击绿色的“加入”按钮来加入组。单击后,他们应该会看到一个红色的“取消加入”按钮。

这是我的按钮:

@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

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

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

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

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

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();
    }
}
php laravel
1个回答
0
投票

有多个组,那么如何确定要与表单一起加入的组?表格可能像这样吗?

@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!!');
}

类似地调整其他功能,例如取消连接和销毁。在用户模型中,

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

在GroupController的show()函数中,

public function show(){
    $groups = Group::all();
    return view('groups.show',compact('groups'))'
}
© www.soinside.com 2019 - 2024. All rights reserved.