haritz

haritz

Member Since 1 Year Ago

Experience Points
800
Total
Experience

4,200 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
0
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
800 XP
Feb
16
5 days ago
Activity icon

Started a new Conversation Use Variable Inside Query With

Hello, I have the next code on my controller:

public function week($league)
    {

        $user = auth()->user();

        $puntos = 0;

        $positions = array('top_id', 'jungle_id', 'mid_id', 'adc_id', 'sup_id');

        $league_id = League::where('LINK', $league)->firstOrFail(['id']);

        $weeks = Weeks::where('active', 1)->first();
        if (request('week') != null) {
            $week = request('week');
        } else {
            $week = Weeks::where('id', $weeks->id - 1)->first();
            $week = $week->name;
        }

        $roster = UserLeagueRoster::where([['user_id', $user->id], ['league_id', $league_id->id], ['WEEK', $week]])->first();


        if ($roster != null) {
            foreach ($positions as $posi) {
                $player = Player::with(['points' => function ($query) {
                    $query->where('WEEK', 'Week 4');
                }])->where('id', $roster->$posi)
                    ->get();

                foreach ($player as $points) {
                    $puntos += $points->points->sum('POINTS');
                }
            }
        }
        /*DB::table('user_league_roster')
            ->where([['user_id', $user->id], ['league_id', $league_id->id], ['WEEK', $week]])
            ->update(['points' => $puntos]);*/

        $league = UserLeagueRoster::where([['league_id', $league_id->id], ['WEEK', $week]])->orderBy('points', 'DESC')->get();

        $weeks = Weeks::all();

        return view('league\week', compact('roster', 'week', 'puntos', 'league', 'weeks'));
    }

In this part:

$player = Player::with(['points' => function ($query) {
                    $query->where('WEEK',  'Week 4');
                }])->where('id', $roster->$posi)
                    ->get();

Where is says "Week 4" I would like to use the variable $week but it says Undefined variable. How can I access to it?

Thank you.

Feb
15
6 days ago
Activity icon

Started a new Conversation View Not Found Shared Hosting

Hello,

I just uploaded my project to one shared hosting and the home view works fine but when I try to go to antoher page I get the error that the view is not found.

The home.blade.php is located in resources/views and then I have resources/views/league folder where I have more views.

If I move one of resources/views/league view to resources/views, its works fine on my page, but if its inside resources/views/league, I get the error.

Thank you

Feb
14
1 week ago
Activity icon

Replied to Call To Undefined Method Carbon\Traits\Week::where() Error, Why?

My bad, wrong use of Model.

Thank you.

Activity icon

Started a new Conversation Call To Undefined Method Carbon\Traits\Week::where() Error, Why?

I am getting this error and honeslty I have no idea why this is happening.

Getting this error here:

 $week_active = Weeks::where('active', 1)->first();
        

        $match = Match::where('WEEK' , $week_active->name)->first();

        $date = Carbon::parse($match->date);
        if($date <= Carbon::now()){
            Week::where('active', 1)->first()->update(['active' => 0]); //Getting error here
            Week::where('id', '>', $week_active->id)->first()->update(['active' => 1]);
        }

I just want to update a row. $match->date is returning a datetime.

Thank you.

Feb
10
1 week ago
Activity icon

Replied to Groupby In View

Perfect, thanks

Activity icon

Replied to Groupby In View

Seems it does not work for me.

I get POSITION with a relationship.

UserPlayerLeague Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserPlayerLeague extends Model
{
    protected $table = 'user_player_league';

    public $timestamps = false;

    public function player()
    {
        return $this->belongsTo(Player::class, 'player_id');
    }

    public function points(){
        return $this->belongsTo(PlayerPoints::class, 'player_id');
    }

}

Player Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Player extends Model
{
    protected $table = 'players';

    public $timestamps = false;

    public function team()
    {
        return $this->belongsTo(Team::class, 'TEAM_ID');
    }

    public function points()
    {
        return $this->hasMany(PlayerPoints::class);
    }

}

I can access to player POSITON by $player->player->POSITION

Activity icon

Started a new Conversation Groupby In View

Hello,

I would like to create a html Select Option for each different position in a foreach.

This is what I return to my view:

public function myTeam($league)
    {
        $user = auth()->user();
        $league_id = League::where('LINK', $league)->firstOrFail(['id']);

        $players = UserPlayerLeague::where([['user_id', $user->id], ['league_id', $league_id->id]])->get();


        return view('league\myteam1', compact('players'));
    }

And this is my view:

@extends('layouts.app')

@section('content')
<style>
    #div1 {
        width: 100px;
        height: 75px;
    }
</style>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12 col-sm-12 col-lg-2">
            <div class="row">
                <div class="col-md-12">
                    @include('league.layouts.menu')
                </div>
                <div class="col-md-12 col-sm-12 col-lg-12">
                    <div class="card mb-2">
                        <div class="card-header">My team</div>
                        <div class="card-body">
                            <div class="row justify-content-center">
                                @if(! $players->isEmpty())
                                @foreach($players as $player)
                                <div class="col-md-6 col-6 col-lg-12 col-xl-6">
                                    <div class="m-1"><img class="img-flui" src="{{ URL::to('/') }}/img/players/{{$player->player->NAME}}.jpg" alt="{{$player->player->NAME}}" width="100" height="75"></div>
                                </div>
                                @endforeach
                                @else
                                <div class="col-md-6 col-6 col-lg-12 col-xl-6">
                                    <a href="{{route('market', Request()->league)}}" class="btn btn-success">Buy players</a>
                                </div>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>


        <div class="col-md-12 col-sm-12 col-lg-10">
            @if($errors->any())
            <div class="alert alert-success">
                <h4 class="text-center">{{ $errors->first() }}</h4>
            </div>
            @endif
            <div class="card mb-2">
                <div class="card-header">My roster<a href="#" class="btn btn-success float-right">Save</a></div>
                <div class="card-body">
                    @foreach($players as $player)
                    <div class="form-group">
                        <label for="exampleFormControlSelect1">Example select</label>
                        <select class="form-control" id="exampleFormControlSelect1">
                            @if($player->player->POSITION == "Bot Laner")
                            <option value="{{$player->player->id}}">{{$player->player->NAME}}</option>
                            @endif
                        </select>
                    </div>
                    @endforeach
                </div>
            </div>
            <div class="card">
                <div class="card-header">My team</div>
                <div class="card-body">
                    <div class="row m-2">
                        <div class="table-responsive">
                            <table class="table" id="example">
                                <thead>
                                    <tr>
                                        <th scope="col"></th>
                                        <th scope="col">Name</th>
                                        <th scope="col">Points</th>
                                        <th scope="col">Team</th>
                                        <th scope="col">Position</th>
                                        <th scope="col">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($players as $player)
                                    <tr>
                                        <th class="align-middle"><img src="{{ URL::to('/') }}/img/players/{{$player->player->NAME}}.jpg" alt="{{$player->player->NAME}}" width="100" height="75"></th>
                                        <td class="align-middle">{{$player->player->NAME}}</td>
                                        <td class="align-middle">{{$player->player->points->sum('POINTS')}}</td>
                                        <td class="align-middle"><img src="{{ URL::to('/') }}/img/{{$player->player->team->NAME}}.png" alt="{{$player->player->NAME}}" width="75" height="50"></td>
                                        <td class="align-middle">{{$player->player->POSITION}}</td>
                                        <td class="align-middle">
                                            <form class="" role="form" method="POST" action="{{URL::route('deletePlayer', [ Request()->league , $player->player->id])}}">
                                                {{ csrf_field() }}
                                                {{ method_field('DELETE') }}
                                                <button type="submit" class="btn btn-danger">Sell</button>
                                            </form>
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
@endsection

I would need to group them only in one foreach.

Thank you in advance.

Activity icon

Started a new Conversation Sum Values From Relationship

Hello,

This time I am trying to do this.

I have a table with:

id - player_id - points - week - day

For example:
1 - 1 - 15 - WEEK 1 - DAY 1
2 - 1 - 5 - WEEK 1 - DAY 1
3 - 1 - 20 - WEEK 1 - DAY 2
2 - 1 - 5 - WEEK 1 - DAY 2

Player Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Player extends Model
{
    protected $table = 'players';

    public $timestamps = false;

    public function team()
    {
        return $this->belongsTo(Team::class, 'TEAM_ID');
    }

    public function points()
    {
        return $this->belongsTo(PlayerPoints::class, 'ID', 'player_id');
    }

}

PlayerPoints Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PlayerPoints extends Model
{
    protected $table = 'players_points';

    public $timestamps = false;

    public function player()
    {
        return $this->hasMany(Player::class, 'player_id');
    }

}

I can get the player points but only gets the first row:

{{$player->points->POINTS}}

What I would like to do is to get the total amount of POINTS that each player has.

Thank you in advance.

Feb
05
2 weeks ago
Activity icon

Awarded Best Reply on Check If There Are More Than 2 Rows With Relationship

This is how I did it:

 public function buyPlayer($league, $player)
    {
        $user = auth()->user();

        $league_id = League::where('LINK', $league)->first(['id']);
        $check_owns_player = UserPlayerLeague::where([['player_id', $player], ['user_id', $user->id], ['league_id', $league_id->id]])->first();

        $team = Player::find($player);

        $check_max_limit = DB::table('user_player_league')
        ->selectRaw('COUNT(*) AS result')
        ->join('players', 'user_player_league.player_id', '=', 'players.id')
        ->join('teams', 'players.TEAM_ID', '=', 'teams.id')
        ->where('user_id' , $user->id)
        ->where('league_id' , $league_id->id)
        ->where('teams.NAME' , $team->team->NAME)
        ->get();

        foreach($check_max_limit as $limit){
            $limit = $limit->result;
        }

        if ($limit >= 2) {
            return Redirect::back()->withErrors(['You reached the limit of players per team']);
        }

        if ($check_owns_player != null) {
            return Redirect::back()->withErrors(['You already own this player']);
        }


        $buy = new UserPlayerLeague();

        $buy->user_id = $user->id;
        $buy->league_id = $league_id->id;
        $buy->player_id = $player;

        $buy->save();

        return Redirect::back();
    }

I am not sure if it is the best way to do it but it works for me

Activity icon

Replied to Check If There Are More Than 2 Rows With Relationship

This is how I did it:

 public function buyPlayer($league, $player)
    {
        $user = auth()->user();

        $league_id = League::where('LINK', $league)->first(['id']);
        $check_owns_player = UserPlayerLeague::where([['player_id', $player], ['user_id', $user->id], ['league_id', $league_id->id]])->first();

        $team = Player::find($player);

        $check_max_limit = DB::table('user_player_league')
        ->selectRaw('COUNT(*) AS result')
        ->join('players', 'user_player_league.player_id', '=', 'players.id')
        ->join('teams', 'players.TEAM_ID', '=', 'teams.id')
        ->where('user_id' , $user->id)
        ->where('league_id' , $league_id->id)
        ->where('teams.NAME' , $team->team->NAME)
        ->get();

        foreach($check_max_limit as $limit){
            $limit = $limit->result;
        }

        if ($limit >= 2) {
            return Redirect::back()->withErrors(['You reached the limit of players per team']);
        }

        if ($check_owns_player != null) {
            return Redirect::back()->withErrors(['You already own this player']);
        }


        $buy = new UserPlayerLeague();

        $buy->user_id = $user->id;
        $buy->league_id = $league_id->id;
        $buy->player_id = $player;

        $buy->save();

        return Redirect::back();
    }

I am not sure if it is the best way to do it but it works for me

Feb
04
2 weeks ago
Activity icon

Replied to Check If There Are More Than 2 Rows With Relationship

I am a little bit confused right now, seems what you gave me does not work. What I think I need is this:

SELECT COUNT(teams.id) fROM user_player_league INNER JOIN players ON user_player_league.player_id = players.id INNER JOIN teams ON players.id = teams.id WHERE user_id = 1 AND league_id = 1

At the moment i am not getting team anywhere

Activity icon

Replied to Check If There Are More Than 2 Rows With Relationship

I have USER_ID, PLAYER_ID and LEAGUE_ID.

This is my method:

public function buyPlayer($league, $player)
    {
        $user = auth()->user();
        
        $league_id = League::where('LINK', $league)->first(['id']);
        $check_owns_player = UserPlayerLeague::where([['player_id', $player], ['user_id', $user->id], ['league_id', $league_id->id]])->first();


        if($check_owns_player != null){
            return Redirect::back()->withErrors(['You already own this player']);
        }


        $buy = new UserPlayerLeague();

        $buy->user_id = $user->id;
        $buy->league_id = $league_id->id;
        $buy->player_id = $player;

        $buy->save();

        return Redirect::back();
    }
Activity icon

Replied to Check If There Are More Than 2 Rows With Relationship

Yes sir.

Example (user, player, league):

Haritz - Thomas - League 1 Bugysha - Thomas - League 1 Haritz - Thomas - League 2 ...

Activity icon

Replied to Check If There Are More Than 2 Rows With Relationship

What I mean is that an User can have many Players in many Leagues and a Player can have many Users in many Leagues.

Thats why I made the table USER_PLAYER_LEAGUE so I can use the 3 IDs of each table.

Activity icon

Replied to Check If There Are More Than 2 Rows With Relationship

What I have is a table called: USER_PLAYER_LEAGUE where I save USER_ID, LEAGUE_ID and PLAYER_ID.

PLAYER table has a TEAM_ID column which allows me to check to which team belongs obviously. So what I need to do is to check if the user, in the same league, has already 2 players from the same team. If so, not allow to add new player.

Activity icon

Replied to Check If There Are More Than 2 Rows With Relationship

Yes third one is not allowed if its in the same league(league_id) and for the same user(user_id).

Activity icon

Started a new Conversation Check If There Are More Than 2 Rows With Relationship

Hello,

I have a table where I save players_id, league_id and user_id.

What I want to do is not allow to save new player if there are 2 players with the same tam_id in the same league and for the same user.

Player Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Player extends Model
{
    protected $table = 'players';

    public $timestamps = false;

    public function team()
    {
        return $this->belongsTo(Team::class, 'TEAM_ID');
    }

}

UserPlayerLeague Model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserPlayerLeague extends Model
{
    protected $table = 'user_player_league';

    public $timestamps = false;

    public function player()
    {
        return $this->belongsTo(Player::class, 'player_id');
    }
}

Thank you in advance.

Feb
02
2 weeks ago
Activity icon

Replied to Display 2 HasMany In Blade

Mmmmm f**ck me I guess. I guess was blind.

Thank you.

Activity icon

Started a new Conversation Display 2 HasMany In Blade

Hello,

I have a Matches table where I insert TEAM_ID_1 and TEAM_ID_2.

In the Team model:

public function matches()
    {
        return $this->hasMany(Match::class);
    }

And on Match mode:

public function team1()
    {
        return $this->belongsTo(Team::class, 'TEAM_ID_1');
    }
    public function team2()
    {
        return $this->belongsTo(Team::class. 'TEAM_ID_2');
    }

First of all, I don't if this can be done.

On blade:

@foreach($matches as $week => $group)
                        <div class="col-md-6 text-center">
                            <h1>{{$week}}</h1>
                            @foreach($group as $key => $match)
                            <div class="d-flex justify-content-center">
                                <div class="p-2 bd-highlight"><img src="{{ URL::to('/') }}/img/{{$match->team1->NAME}}.png" alt="" width="40" height="40">{{$match->team1->SHORT_NAME}}</div>
                                <div class="p-2 bd-highlight align-self-center">@if($match->WINNER === 1)1 - 0 @else 0 - 1 @endif</div>
                                <div class="p-2 bd-highlight">{{$match->TEAM_ID_2}}<img src="{{ URL::to('/') }}/img/{{$match->team1->NAME}}.png" alt="" width="40" height="40"></div>
                            </div>
                            @endforeach
                        </div>
                        @endforeach

On controller:

$matches = Match::get()->take(20)->groupBy('WEEK');

$leagues = League::whereDoesntHave('users', function($query) {
            $query->where('admin',auth()->id());
        })->where('private', 0)->withCount('users')->paginate(15);
        return view('home', compact('leagues', 'matches'));

If on view I do:

{{$match->team1->SHORT_NAME}}

It returns well the short name of the team but if instead I do

{{$match->team2->SHORT_NAME}}

Does not work, I get:

Class 'App\TeamTEAM_ID_2' not found

Thank you in advance.

Feb
01
2 weeks ago
Activity icon

Started a new Conversation How To Display GtoupBy Into Blade

Hello, I am trying to display data into my blade divided by players position.

$players = Player::orderBy('team_id')->get()->groupBy('POSITION');
        return view('league\market', compact('players'));

I have this query that returns the palyers grouped by POSITION and what I would like to do is display each player according to their POSITION.

TOP: All players that are top laners MID: All players that are mid laners

And so on.

Thanks in advance.

Jan
31
3 weeks ago
Activity icon

Awarded Best Reply on Show Many To Many Data In Blade

Fixed, had one problem on the databse column.

Activity icon

Replied to Show Many To Many Data In Blade

Fixed, had one problem on the databse column.

Activity icon

Replied to Show Many To Many Data In Blade

Just in case

User Model:

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;


class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function leagues()
    {
        return $this->belongsToMany(League::class, 'leagues_players', 'user_id', 'league_id');
    }
}

League model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class League extends Model
{
    protected $table = 'leagues';

    protected $fillable = [
        'NAME', 'LINK', 'LIMIT'
    ];

    public function getRouteKeyName()
    {
        return 'LINK';
    }

    public $timestamps = false;

    public function users()
    {
        return $this->belongsToMany(User::class, 'leagues_players', 'league_id', 'user_id');
    }
}

I still don't understand why I can't get the users.

Jan
30
3 weeks ago
Activity icon

Replied to Show Many To Many Data In Blade

I changed models to what you wrote me.

At the moment I have 2 users and 2 leagues.

In the table "leagues_players" I have 3 rows and what I get in blde is "[]"

Activity icon

Started a new Conversation Show Many To Many Data In Blade

I am trying to show data into blade but the array is empty. I just want to show how many users a League has.

League Model:

public function users()
    {
        return $this->belongsToMany(User::class, 'leagues_players', 'league_id');
    }

User Model:

 public function leagues()
    {
        return $this->belongsToMany(League::class, 'leagues_players', 'user_id');
    }

LeagueController:

public function show($id)
    {
        $league = League::where('LINK', $id)->firstOrFail();

        return view('league\view', compact('league'));
    }

In blade:

{{$league->users}}

Returns empty.

My leagues_players table:

ID
user_id
league_id
Activity icon

Replied to How To Show Data From Many To Many?

How I could access to user ID so I dont show Leagues that the current user created?

I know I can do:

$user = auth()->user();
$user->id

To get User ID

Activity icon

Replied to How To Show Data From Many To Many?

Thank you very much, it worked perfectly.

Activity icon

Started a new Conversation How To Show Data From Many To Many?

Hello, I have 2 tables which are Users and Leagues.

On League Model I have this:

 public function users()
    {
        return $this->belongsToMany(User::class, 'leagues_players','user_id', 'league_id');
    }

On User Model I have this:

public function leagues()
    {
        return $this->belongsToMany(League::class, 'leagues_players','league_id', 'user_id');
    }

And I create a table called leagues_players with user_id and league_id.

What I want to do is show a table with all the leagues and show how many people is on each league.

I used this code:

$leagues = DB::table('leagues_players')
        ->select('league_id', 'leagues.name', 'leagues.link', DB::raw('count(*) as players'))
        ->join('leagues', 'leagues_players.league_id', '=', 'leagues.id')
        ->groupBy('league_id')
        ->get();

And it works fine if the League has any user. If not, it shows nothing.

On blade:

@foreach ($leagues as $league)
<tbody>
<tr>
<th scope="row">{{$league->name}}</th>
<td class="text-center">{{$league->players}}/6</td>
<td class="text-center"><a href="/join/{{$league->link}}" class="btn btn-success text-white">Join</a></td>
</tr>
</tbody>
@endforeach