0

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

$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']
    ]);

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

    if ($team->join_request_needed = 'True') {
        if (Member::where('email', '=', $data['email'])->count() > 0) {
            $member = Member::where('email', '=', $data['email']);
            if ($member->approved = 'True') {
                // member has been accepted
                return view('portal.members.answer', [
                    'team' => $team
                ]); // questions
            } else {
                // member has not been accepted
                dd('member has not been accepted');
                return view(''); // waiting screen
            }
        } else {
            // create a join request for the member
            $team->member()->create([
                'email' => $data['email']
            ]);
            return view(''); // waiting screen
        }
    } else if(Member::where('email', '=', $data['email'])->count() > 0) {
        // no join request needed, member already exists
        return view('portal.members.answer', [
            'team' => $team
        ]); // questions
    } else {
        // no join request needed, need to create member
        dd($team);
        $team->member()->create([
            'email' => $data['email']
        ]);
        return view('portal.members.answer', [
            'team' => $team
        ]); // questions
    }
}
4

2 回答 2

1

在您的情况下,Team 是 Teams 的集合,因此您可以使用 querybuilder 方法first()

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

由于查询的工作方式,在查询生成器上调用 get 将始终返回一个集合。如果你想要单个对象first()或者find()是选项。

于 2019-11-28T00:16:28.773 回答
0

Team由于您正在获取with的集合,因此join_number = $data['team'] 您需要先循环$team. 但是,如果您只想拥有一个具有 ajoin_number = $data['team']您应该做的 $teamfirst()而不是get()

于 2019-11-28T02:28:18.747 回答