0

Helo there, please help me

I have a model that look like this

Schema::create('teams', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('player_id')->unsigned();
    });
    Schema::table('teams', function (Blueprint $table) {
        $table->foreign('player_id')->references('id')->on('players')->onDelete('cascade');
    });

and

Schema::create('team_members', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('team_id');
        $table->integer('creator_id');
    });`

Schema::table('team_members', function (Blueprint $table) { //another migration
        $table->integer('team_id')->unsigned()->change();
        $table->integer('creator_id')->unsigned()->change();

        $table->foreign('team_id')->references('id')->on('teams')->onDelete('cascade');
        $table->foreign('creator_id')->references('id')->on('players')->onDelete('cascade');
    });

I want to delete the team model, so I did this:

$team = Team::findOrFail($id);

    if(Session::get('player_id') == $team->creator_id)
    {
        $team_member = TeamMember::where('team_id', $id)->get();
        foreach($team_member as $tm)
        {
            $player = Player::find($tm->player_id);
            Notification::send($player, new \App\Notifications\DatabaseNotification(12, Session::get('username'), $id) );

        }

        @unlink($team->avatar);
        $team->delete();

        Session::flash('delete_team_success', 'Delete team success!');

        return redirect('/teams/me');
    }

the delete is success, the team and the team_member has gone from the table. But after I did query like thisTeamMember::where('player_id', Session::get('player_id'))->get(); It's still returning a value.Click here to see the result but when i did like this TeamMember::get() the result is empty Click here to see the result

Why this is happening? I need to get the null result when I did TeamMember::where('player_id', Session::get('player_id'))->get();

shalhan
  • 17
  • 4
  • You already try to use `first()` instead of `get()` if you only need one result, then you can verify the count? More info https://stackoverflow.com/a/20585483/6190900 – Joseph Jun 19 '17 at 05:31
  • but i need to use get(). and is there something wrong with my code? why that happened? – shalhan Jun 19 '17 at 05:41
  • Base on the documentation `get` method returns an `Illuminate\Support\Collection` so it will always returns an instance of collection, base on your screenshot you return an empty collection not a `null` – Joseph Jun 19 '17 at 05:54
  • Why you need to return a null value? If for validation to check if the data is in your database, you may use first() – Joseph Jun 19 '17 at 05:55

0 Answers0