我想添加和查看某些球队的球员,并根据laravel中的球队id查看他们

我想添加和查看某些球队的球员,并根据laravel中的球队id查看他们,laravel,Laravel,我不知道如何推进这一进程。我想通过URL发送团队id,它会这样提交 我想取数字,即1,并在id中输入球员姓名,以便检索特定球队的球员 我也做过这样的关系,比如球队有很多球员,球员属于球队 我的玩家控制器如下 <?php namespace App\Http\Controllers; use App\Models\Player; use App\Models\Team; use Illuminate\Http\Request; use App\Http\Requests\Players\

我不知道如何推进这一进程。我想通过URL发送团队id,它会这样提交 我想取数字,即1,并在id中输入球员姓名,以便检索特定球队的球员

我也做过这样的关系,比如球队有很多球员,球员属于球队

我的玩家控制器如下

<?php

namespace App\Http\Controllers;

use App\Models\Player;
use App\Models\Team;
use Illuminate\Http\Request;
use App\Http\Requests\Players\CreatePlayersRequest;
use App\Http\Requests\Players\UpdatePlayersRequest;

class PlayersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Team $team)
    {   
        return view('players.index')->with('team', $team)->with('players', Player::all());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {   
        return view('players.create')->with('teams', Team::all());
    }

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

        Player::create([
            'playerName' => $request->playerName,
            'team' => $request->team
            
        ]);
        

        session()->flash('Success', 'Player Added Successfully.');
        return redirect(route('player.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Player $player)
    {
        return view('players.create')->with('player', $player)->with('teams', $team);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdatePlayersRequest $request,Player $player)
    {
         $player->update([
        'playerName' => $request->playerName
    ]); 
        session()->flash('success', 'Player Name Updated Successfully.');
        return redirect(route('player.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Player $player)
    {
        $player->delete();
        session()->flash('success', 'Player Deleted Successfully.');
        return redirect(route('player.index'));
    }
}
我真正想做的是使用PHP

<a href="viewcase.php?id= '.$row['id'].' " class = "btn btn-primary"> View Cases </a>
像这样得到它


因此,我可以使用该id将球员输入到该id中,以便查看每个球队的唯一球员。

如果您希望将球队与球员一起检索,则可以执行此操作


你的问题让我们搞不懂你的意思,请重新写你的问题。@jesuerswinsuarez他想做
http://eurocup.test:8000/player/1
而不是
http://eurocup.test:8000/player?1
。。。只需编辑底部的
表单
。@AnkitS您可以使用
将数据传递到
视图
,然后将数组放入其中,不要使用(…)->使用(…)->使用(…)->使用(…)
,如果您只想发送一个请求的信息(
flash
),请使用(…)
重定向(…)->使用(…)
,不要执行
session()->flash(…)
(内部执行
$this->session->flash(…)
,但要用Laravel的方式来做……我明白了,这很简单,但这要等到问题有所改善。你也可以添加我来进行更多的合作,我可以随时帮助你解决你的问题。在skype添加我:你可以通过使用的简单
将数据传递到视图,使用数组
和(['team'=>$team',players'..)
,不要使用(…)->使用(…)->使用(…)->使用(…)
。谢谢@matiaslauriti,我会改进我的答案。@matiaslauriti在skype上添加我:jesus.erwin.SuareZ143试图获取非对象的属性“players”,它显示以下错误我想这是因为在我的团队表中没有球员列,但在我的球员表中有团队id列。因此找不到$team->player
Route::resource('player', 'App\Http\Controllers\PlayersController');
<a href="viewcase.php?id= '.$row['id'].' " class = "btn btn-primary"> View Cases </a>
$id = $_GET['id'];
class PlayersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Team $team)
    {   
         

        $team = Team::where('id', request()->input('team_id'))->with('players')->first();


        return view('players.index')->with(['team' => $team, 'players' => $team->players]);
    }

}