在控制器中获取特定信息?

我的控制器有问题并在其中获取数据。我的问题是,当我像这样让我的团队时:


$team = Team::where('join_number', '=', $data['team'])->get();

我似乎无法通过去访问它


$team->join_request_needed = 'True'

例如?如果我只是死转储


$team->join_request_needed

我收到“此集合实例上不存在属性 [join_request_needed]”。我如何访问这些数据?如果我死了转储$team,我会得到这个;所以它得到数据?


Illuminate\Database\Eloquent\Collection {#282 ▼

  #items: array:1 [▼

    0 => App\Team {#291 ▼

      #guarded: []

      #connection: "sqlite"

      #table: "teams"

      #primaryKey: "id"

      #keyType: "int"

      +incrementing: true

      #with: []

      #withCount: []

      #perPage: 15

      +exists: true

      +wasRecentlyCreated: false

      #attributes: array:8 [▶]

      #original: array:8 [▼

        "id" => "1"

        "user_id" => "1"

        "name" => "team1"

        "description" => null

        "join_request_needed" => "0"

        "join_number" => "1574832472024"

        "created_at" => "2019-11-27 05:27:52"

        "updated_at" => "2019-11-27 22:46:55"

      ]

      #changes: []

      #casts: []

      #dates: []

      #dateFormat: null

      #appends: []

      #dispatchesEvents: []

      #observables: []

      #relations: []

      #touches: []

      +timestamps: true

      #hidden: []

      #visible: []

      #fillable: []

    }

  ]

}

这是我的整个功能(到目前为止只是粗略,因为我坚持获取数据):


public function check() {

    $data = request()->validate([

        'email' => ['required', 'email'],

        'team' => ['required', 'alpha_num', 'exists:teams,join_number']

    ]);


一只名叫tom的猫
浏览 94回答 2
2回答

繁星coding

在您的情况下,Team 是 Teams 的集合,因此您可以使用 querybuilder 方法first()$team = Team::where('join_number', '=', $data['team'])->first();由于查询的工作方式,在查询生成器上调用 get 将始终返回一个集合。如果你想要单个对象first()或者find()是选项。

繁花不似锦

Team由于您正在获取with的集合,因此join_number = $data['team'] 您需要先循环$team. 但是,如果您只想拥有一个具有 ajoin_number = $data['team']您应该做的 $teamfirst()而不是get()
打开App,查看更多内容
随时随地看视频慕课网APP