alexMafus

alexMafus

Member Since 1 Year Ago

Experience Points 980
Experience Level 1

4,020 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 Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

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

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

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

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    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 Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

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

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

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

24 Jun
11 months ago

alexMafus started a new conversation Eloquent Order Users By Latest Post Written.

Hi i want to show all my users in a index page but the ordering to be based on latest post written here s my relationship to my app.

User model:

public function article() {
        return $this->hasMany(Article::class);
}

Article model:

public function user() { 
        return $this->belongsTo(Author::class);
}

Here is my function i try to get the users but dont work

    public function scopeGetAllUsers($query, $num) {
        return $query
                ->join('articles', 'users.id', '=', 'articles.user_id')
                orderBy('articles.created_at', 'desc')
                ->where('users.active',true)
                ->with('users.article')
                ->simplePaginate($num);
    }

21 May
1 year ago

alexMafus left a reply on Slow Query Where Has With Big Data

what do you mean?

alexMafus left a reply on Slow Query Where Has With Big Data

Articles

    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->nullable();
            $table->integer('user_id_edit')->nullable();
            $table->integer('author_id')->nullable();
            $table->integer('poll_id')->nullable();
            $table->integer('category_id')->nullable();
            $table->string('title')->nullable();
            $table->string('slug')->nullable();
            $table->string('supertitle')->nullable();
            $table->text('text')->nullable();
            $table->string('source')->nullable();
            $table->string('redirect_url')->nullable();
            $table->string('meta_title')->nullable();
            $table->string('meta_description')->nullable();
            $table->string('meta_keywords')->nullable();
            $table->integer('active')->nullable();
            $table->string('inside_image')->nullable();
            $table->string('outside_image')->nullable();
            $table->string('youtube_id')->nullable();
            $table->string('kaltura_id')->nullable();
            $table->string('schedule_time')->nullable();
            $table->datetime('published_at')->nullable();
            $table->timestamps();
        });
    }

Tag

 public function up()
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->index();
            $table->timestamps();
        });
    }

Article Tag

 public function up()
    {
        Schema::create('article_tag', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('article_id')->unsigned()->index();
            $table->integer('tag_id')->unsigned()->index();
            $table->timestamps();
        });
    }

alexMafus left a reply on Slow Query Where Has With Big Data

Here is my query with where has 33.3s

select * from `articles` where exists (select * from `tags` inner join `article_tag` on `tags`.`id` = `article_tag`.`tag_id` where `articles`.`id` = `article_tag`.`article_id` and `name` = 'χρυσαυγίτης εθνοφύλακας') order by `created_at` desc limit 11 offset 0
``

alexMafus left a reply on Slow Query Where Has With Big Data

i have done all indexes but i think is slow because the where has scan all my articles table with 500.000 articles do you know any implementation with inner join so not to scan all my db.

alexMafus started a new conversation Slow Query Where Has With Big Data

Hi, i have a article table and a tag table with a many to many relationship

and i want to search articles by tag when i have dummy data is ok but when i use real data is very slow where has any idea to improve maybe inner join.

Here is m function

 public function scopeTagArticles($query, $text) {
        return $query->latest()
                     ->whereHas('tags', function($query) use ($text) {
                         $query->where('name', $text);
                      });
    }

17 May
1 year ago

alexMafus started a new conversation Order By Nery Slow

Hi i have this fuction in my model but is very slow(i have done all indexes) any idea to improve

  public function popular($query, $num) {
        return  $query
                ->orderBy('id', 'desc')
                ->where('active',1)
                ->with('category')
               ->where('author_id',0) 
               ->whereNotNull('only_video')
                ->whereDate('created_at', '>', \Carbon\Carbon::now()->subMonth())
                ->where('published_at', '<=', Carbon::now())
                ->limit($numOfArticles);
    }

26 Apr
1 year ago

alexMafus left a reply on Cant Understand Eloquent Distinct

so how can i have a functionality to not show all the players with same first name and last name but only one for example in JS i can filter the players how in eloquent ca i do that? @Snapey i just dont want to show duplicate players by first and last name (i try all day but i am new in laravel and cant do it).

alexMafus left a reply on Cant Understand Eloquent Distinct

@Snapey i have created a cms and i have an index page where i show all the players and i have some search fields(search by name,city etc) now i have a lot dublicate records so i want a button in search so when i click to show me only the unique users by first name and last name so if i have 5 John Doe with same name and surname i want to show only the first one

Here is my search function

Player Model


public function scopeSearchPlayers($query, $search_id, $search_name, $search_number, $search_status, $search_gender, $search_school, $search_email, $search_from, $search_to, $search_nomos, $search_dimos, $pagination) {
        return $query->latest()->when($search_id, function ($query) use ($search_id) { 
            return $query->where('registrationId',  '=',  $search_id);     
        
        })->when($search_name, function ($query) use ($search_name) {
                return $query->where('first_name', 'LIKE', '%'.$search_name.'%');

        })->when($search_number, function ($query) use ($search_number) {
            return $query->where('phone_parent_one', $search_number)
                         ->orWhere('phone_parent_two', $search_number);  

        })->when($search_school, function ($query) use ($search_school) {
            return $query->where('school', $search_school);                     

        })->when($search_email, function ($query) use ($search_email) {
            return $query->where('emailOne', $search_email)
                         ->orWhere('emailTwo', $search_email);                    
            
        })->when($search_status, function ($query) use ($search_status) {
            return $query->where('statusOne', $search_status);  

        })->when($search_gender, function ($query) use ($search_gender) {
            return $query->where('gender', $search_gender);         
        
        })->when($search_nomos, function ($query) use ($search_nomos) {
            return $query->where('perioxi', $search_nomos);   
        
        })->when($search_dimos, function ($query) use ($search_dimos) {
            return $query->where('poli', $search_dimos);       

        })->when($search_from, function ($query) use ($search_from) {
            return $query->where('created_at',  '>=',  $search_from);

        })->when($search_to, function ($query) use ($search_to) {
            return $query->where('created_at',  '<=',  $search_to);   

        })->when($pagination, function ($query) use ($pagination) {
            return $query->paginate($pagination);   
        });
    }

Player Controller

 public function index(Request $request) { 
        $search_id = $request->get('id');
        $search_name = $request->get('name');
        $search_number = $request->get('number');
        $search_email = $request->get('email');
        $search_status =  $request->get('status');
        $search_gender =  $request->get('gender');
        $search_school = $request->get('school'); 
        $search_from = $request->get('date_from'); 
        $search_to = $request->get('date_to');  
        $search_nomos = $request->get('nomos');
        $search_dimos = $request->get('dimos');
        $pagination =  $request->get('pagination');  
        
        if ($search_id || $search_name || $search_number || $search_status || $search_gender || $search_school ||  $search_email || $search_from || $search_to ||  $search_nomos || $search_dimos ||  $pagination) {
          $players = Player::searchPlayers($search_id, $search_name,  $search_number, $search_status, $search_gender, $search_school, $search_email, $search_from, $search_to, $search_nomos, $search_dimos, $pagination);
        }
        else {
            $players = Player::latest()->paginate(20);
        }

        $userAuth = Auth::user();
        
        return view('players.index', compact(['players', 'search_id',  'search_name', 'search_number', 'search_status', 'search_gender', 'search_school', 'search_email', 'search_from', 'search_to', 'userAuth', 'pagination', 'nomoi', 'dimoi', 'search_nomos', 'search_dimos']));

    }

And the search blade

<script src="{{ asset('/js/perioxes.js') }}"></script>
<div  class="search">
  <form  class="search__form" method="GET" action="/players">
     <div class="search__wrapper"> 
        <span class="search__item__icon ion-search"></span>
        <input class="search__item__input" value="{{$search_id}}" type="search" name="id" placeholder="ID" />  
     </div>

     <div class="search__wrapper">
        <span class="search__item__icon ion-search"></span>
        <input class="search__item__input" value="{{$search_name}}" type="search" name="name" placeholder="Όνομα" />
     </div>

     <div class="search__wrapper">
        <span class="search__item__icon ion-search"></span>
        <input class="search__item__input" value="{{$search_number}}" type="search" name="number" placeholder="Τηλέφωνο" />
     </div>

      <div class="search__wrapper">
        <span class="search__item__icon ion-search"></span>
        <input class="search__item__input" value="{{$search_email}}" type="search" name="email" placeholder="Email" />
     </div>

     <div class="search__wrapper styled-select color rounded">
      <span class="search__item__icon--category ion-ios-list-outline"></span>
      <select name="gender" id="gender">
        <option value=""  {{ $search_gender == "" ? 'selected' : '' }}>Φύλλο</option>
        <option value="male" {{ $search_gender == "male" ? 'selected' :  '' }}>Αγόρια</option>
        <option value="female" {{ $search_gender == "female" ? 'selected' :  '' }}>Κορίτσια</option>
      </select>
     </div>

      <div class="search__wrapper styled-select color rounded">
      <span class="search__item__icon--category ion-ios-list-outline"></span>
      <select name="school" id="school">
        <option value=""  {{ $search_school == "" ? 'selected' : '' }}>Τάξη</option>
        <option value="1" {{ $search_school == "1" ? 'selected' :  '' }}>1η Δημοτικού</option>
        <option value="2" {{ $search_school == "2" ? 'selected' :  '' }}>2α Δημοτικού</option>
        <option value="3" {{ $search_school == "3" ? 'selected' :  '' }}>3η Δημοτικού</option>
        <option value="4" {{ $search_school == "4" ? 'selected' :  '' }}>4η Δημοτικού</option>
        <option value="5" {{ $search_school == "5" ? 'selected' :  '' }}>5η Δημοτικού</option>
        <option value="6" {{ $search_school == "6" ? 'selected' :  '' }}>6η Δημοτικού</option>
        <option value="7" {{ $search_school == "7" ? 'selected' :  '' }}>1η Γυμνασίου</option>
        <option value="8" {{ $search_school == "8" ? 'selected' :  '' }}>2α Γυμνασίου</option>
        <option value="9" {{ $search_school == "9" ? 'selected' :  '' }}>3η Γυμνασίου</option>
      </select>
     </div>


    <div class="search__wrapper styled-select color rounded">
      <span class="search__item__icon--category ion-ios-list-outline"></span>
      <select name="status" id="status">
        <option value=""  {{ $search_status == "" ? 'selected' : '' }}>Εγγεγραμμένοι</option>
        <option value="1" {{ $search_status == "1" ? 'selected' :  '' }}>Ταυτοποιημένοι</option>
      </select>
     </div>

       <div class="search__wrapper styled-select color rounded">
          <span class="search__item__icon--category ion-ios-albums"></span>
             <select id="address_nomos_p1" name="nomos"></select>
       </div>

       <div class="search__wrapper styled-select color rounded">
          <span class="search__item__icon--category ion-ios-albums"></span>
             <select id="address_dhmos_p1" name="dimos"></select>
       </div>

     <div class="search__wrapper">
        <span class="search__item__icon ion-android-calendar"></span>
        <input value="{{$search_from}}" class="datepicker search__item__input"  type="text"  name="date_from" placeholder="Date from " />
     </div>

     <div class="search__wrapper">
        <span class="search__item__icon ion-android-calendar"></span>
        <input value="{{$search_to}}" class="datepicker search__item__input" type="text"  name="date_to" placeholder="Date to" />
     </div>

     <div class="search__wrapper styled-select color rounded">
      <span class="search__item__icon--category ion-ios-list-outline"></span>
      <select name="pagination" id="pagination">
        <option value="20"  {{ $pagination == "" ? 'selected' : '' }}>20 Players</option>
        <option value="50" {{ $pagination == "50" ? 'selected' :  '' }}>50 Players</option>
        <option value="100" {{ $pagination == "100" ? 'selected' :  '' }}>100 Players</option>
        <option value="200" {{ $pagination == "200" ? 'selected' :  '' }}>200 Players</option>
      </select>
   </div> 
     <button class="search__submit" type="submit">Search</button>
  </form>
</div> 

So i want to add another field for unique users.

25 Apr
1 year ago

alexMafus started a new conversation Cant Understand Eloquent Distinct

Hi i have a function that filter my users and now i want to show me unique users by first name but when i use this code show me only the first name how to filter unique users by first name and to show all of user data here is my code

 })->when($search_status, function ($query) use ($search_status) {
            return $query->select('first_name')->distinct();

alexMafus left a reply on Question About Search In Eloquent

@jlrdw i want something like this i think

})->when($search_unique, function ($query) use ($search_unique) {
            return $query->distinct('first_name', 'last_name');

alexMafus started a new conversation Question About Search In Eloquent

Hi i want to add in my search implementation the ability to select a checkbox and then to show only the unique players by name and surname so for example if i have a user with name John Doe twice to show me only one here is my code.

Player Model

 public function scopeSearchPlayers($query, $search_id, $search_name, $search_status, $pagination) {
       
        return $query->latest()->when($search_id, function ($query) use ($search_id) { 
            return $query->where('registrationId',  '=',  $search_id);     
        
        })->when($search_name, function ($query) use ($search_name) {
                return $query->where('first_name', 'LIKE', '%'.$search_name.'%');
       
        })->when($search_status, function ($query) use ($search_status) {
            return $query->where('statusOne', $search_status);  
        })->when($pagination, function ($query) use ($pagination) {
            return $query->paginate($pagination);   
        });
    }

Player Controller

      public function index(Request $request) { 
        $search_id = $request->get('id');
        $search_name = $request->get('name');
        $search_status =  $request->get('status');
        $pagination =  $request->get('pagination');  
        
        if ($search_id || $search_name ||  $search_status ||  $pagination) {
          $players = Player::searchPlayers($search_id, $search_name,  $search_status,  $pagination);
        }
        else {
            $players = Player::latest()->paginate(20);
        }
 
        return view('players.index', compact(['players', 'search_id',  'search_name', 'search_status', 'pagination']));
    }

alexMafus left a reply on How To Show Only Unique Users By Name In Eloquent?

search to is not in the scope so it dosent work

alexMafus left a reply on How To Show Only Unique Users By Name In Eloquent?

i want in this function to limit my users results so to get only the unique by name and n dublicate

})->when($unique_name, function ($query) {
            return $query->groupBy('first_name');

so you have any idea what to insert to callback?

alexMafus started a new conversation Remove Dublicate Value From Search Laravel

hi i want in my search to cms to remove the dublicate names when a user elect remove dublicate so i try to add this functionality to my previous code but dont work here is the function for search

public function scopeSearchPlayers($query, $search_id, $search_name, $search_number, $search_status, $search_gender, $search_school, $search_email, $search_from, $search_to, $search_nomos, $search_dimos, $unique_name, $pagination) {
       
        return $query->when($search_id, function ($query) use ($search_id) { 
            return $query->where('registrationId',  '=',  $search_id);     
        
        })->when($search_name, function ($query) use ($search_name) {
                return $query->where('first_name', 'LIKE', '%'.$search_name.'%');

        })->when($search_number, function ($query) use ($search_number) {
            return $query->where('phone_parent_one', $search_number)
                         ->orWhere('phone_parent_two', $search_number);  

        })->when($search_school, function ($query) use ($search_school) {
            return $query->where('school', $search_school);                     

        })->when($search_email, function ($query) use ($search_email) {
            return $query->where('emailOne', $search_email)
                         ->orWhere('emailTwo', $search_email);                    
            
        })->when($search_status, function ($query) use ($search_status) {
            return $query->where('statusOne', $search_status);  

        })->when($search_gender, function ($query) use ($search_gender) {
            return $query->where('gender', $search_gender);         
        
        })->when($search_nomos, function ($query) use ($search_nomos) {
            return $query->where('perioxi', $search_nomos);   
        
        })->when($search_dimos, function ($query) use ($search_dimos) {
            return $query->where('poli', $search_dimos);       

        })->when($search_from, function ($query) use ($search_from) {
            return $query->where('created_at',  '>=',  $search_from);

        })->when($search_to, function ($query) use ($search_to) {
            return $query->where('created_at',  '<=',  $search_to);  

        })->when($unique_name, function ($query) {
             return $query->distinct()->select('first_name','last_name')->groupBy('first_name');

        })->when($pagination, function ($query) use ($pagination) {
            return $query->paginate($pagination);   
        });
    }

and to get only users with unique is the unique name filed but dont work.

alexMafus left a reply on How To Show Only Unique Users By Name In Eloquent?

Hi @bashy i have this function that search my users and today i add the unique_name but dont work


    public function scopeSearchPlayers($query, $search_id, $search_name, $search_number, $search_status, $search_gender, $search_school, $search_email, $search_from, $search_to, $search_nomos, $search_dimos, $unique_name, $pagination) {
       
        return $query->latest()->when($search_id, function ($query) use ($search_id) { 
            return $query->where('registrationId',  '=',  $search_id);     
        
        })->when($search_name, function ($query) use ($search_name) {
                return $query->where('first_name', 'LIKE', '%'.$search_name.'%');

        })->when($search_number, function ($query) use ($search_number) {
            return $query->where('phone_parent_one', $search_number)
                         ->orWhere('phone_parent_two', $search_number);  

        })->when($search_school, function ($query) use ($search_school) {
            return $query->where('school', $search_school);                     

        })->when($search_email, function ($query) use ($search_email) {
            return $query->where('emailOne', $search_email)
                         ->orWhere('emailTwo', $search_email);                    
            
        })->when($search_status, function ($query) use ($search_status) {
            return $query->where('statusOne', $search_status);  

        })->when($search_gender, function ($query) use ($search_gender) {
            return $query->where('gender', $search_gender);         
        
        })->when($search_nomos, function ($query) use ($search_nomos) {
            return $query->where('perioxi', $search_nomos);   
        
        })->when($search_dimos, function ($query) use ($search_dimos) {
            return $query->where('poli', $search_dimos);       

        })->when($search_from, function ($query) use ($search_from) {
            return $query->where('created_at',  '>=',  $search_from);

        })->when($search_to, function ($query) use ($search_to) {
            return $query->where('created_at',  '<=',  $search_to);  

        })->when($unique_name, function ($query) {
            return $query->groupBy('first_name');

        })->when($pagination, function ($query) use ($pagination) {
            return $query->paginate($pagination);   
        });
    }

alexMafus started a new conversation How To Show Only Unique Unsers By Name Laravel?

Is any way to get the unique rows filter by name or email from users db?

alexMafus left a reply on Search In Eloquent

search name search by name and unique name is just a Boolean 0 1 that i want to remove the duplicate players when i select this field

alexMafus started a new conversation Search In Eloquent

Hi i have a search implementation and i try to filter my users by unique name and surname so to not show me duplicate players but dont work.

Here is my Player Model.

public function scopeSearchPlayers($query, $search_id, $search_name, $unique_name, $pagination) {

        return $query->latest()->when($search_id, function ($query) use ($search_id) { 
            return $query->where('registrationId',  '=',  $search_id);     
        
        })->when($search_name, function ($query) use ($search_name) {
                return $query->where('first_name', 'LIKE', '%'.$search_name.'%');

  
        })->when($unique_name, function ($query) use ($unique_name) {
            return $query->distinct('first_name')->pluck('first_name'); 

        })->when($pagination, function ($query) use ($pagination) {
            return $query->paginate($pagination);   
        });
    }
}

24 Apr
1 year ago

alexMafus started a new conversation Route Issue

Hi i have this two routes but dont work because the first one overwrite the second any suggestion?

    Route::get('/videos/lifestyle/{id}', '[email protected]');
    Route::get('/videos/{section}/{category}', '[email protected]');

12 Apr
1 year ago

alexMafus left a reply on Query Performance

@Snapey i have index the section but is slow so can i tell something like this in laravel because i try but dont work

class Article extends Model {

    public function category() {
        return $this->belongsTo(Category::class);
    }
 

public function scopeGetLatestArticles($query, $section1, $section2,  $numOfArticles) {
     
        
        return  $query
                ->latest()
                ->where('active',1)
                ->with('category')
                ->whereNull('author_id') 
                ->whereDate('created_at', '>', \Carbon\Carbon::now()->subMonth())
                ->where('published_at', '<=', Carbon::now())
                ->category()->where('section'.$section1)
                ->category()->orWhere('section'.$section2)
                ->limit($numOfArticles);
    }
}

So i want to use directly categories() can i?

->category()->where('section'.$section1)
->category()->orWhere('section'.$section2)
09 Apr
1 year ago

alexMafus started a new conversation Query Performance

Hi i have a function in my Article class that get all my posts base on section(news, lifestyle etc) and works perfect and load time(0,0024) for 500.000 articles but when i simply add an order for example by id desc or by created_at desc the query increase the load time to 2.5s and i dont get the reason here is my function without latest() or orderBy() that cause the problem.

public function scopeGetArticlesBySection($query, $section, $numOfArticles) { 
        return  $query
               ->where('active',1)
                ->with('category')
                ->whereNull('author_id')
                ->whereDate('created_at', '>', \Carbon\Carbon::now()->subMonth())
                ->where('published_at', '<=', Carbon::now())
                ->whereHas('category', function($query) use ($section) {
                    $query->where('section', $section);
                })->limit($numOfArticles);
 }

Here is my db schema for articles


Schema::create('articles', function (Blueprint $table) {
            $table->increments('id')->unsigned()->index();
            $table->integer('user_id')->index()->nullable();
            $table->integer('user_id_edit')->nullable();
            $table->integer('author_id')->index()->nullable();
            $table->integer('category_id')->index()->nullable();
            $table->string('title')->nullable();
            $table->string('slug')->nullable();
            $table->string('supertitle')->nullable();
            $table->text('text')->nullable();
            $table->string('source')->nullable();
            $table->string('redirect_url')->nullable();
            $table->integer('active')->index()->nullable();
            $table->string('schedule_time')->nullable();
            $table->datetime('published_at')->index()->nullable();
            $table->timestamp('created_at')->index();
            $table->timestamp('updated_at')->index();
        });

Here is my query in mysql


select * from `articles` where `active` = '1' and `author_id` is null and date(`created_at`) > '2018-03-09 06:01:02' and `published_at` <= '2018-04-09 06:01:02' and exists (select * from `categories` where `articles`.`category_id` = `categories`.`id` and `section` = 'news') limit 7

I used and left join but is more slower here is query

select * from `articles`  LEFT JOIN `categories`  ON  'news' or 'lifestyle'  = `categories`.`section` where `articles`.`active` and `articles`.`author_id` is null and date(`articles`.`created_at`) > '2018-03-09 17:21:11' and `articles`.`published_at` <= '2018-04-09 17:21:11'  = 1 order by `articles`.`created_at` desc limit 5

alexMafus left a reply on Query Performance Question

I used and left join but is more slower here is the query

select * from `articles`  LEFT JOIN `categories`  ON  'news' or 'lifestyle'  = `categories`.`section` where `articles`.`active` and `articles`.`author_id` is null and date(`articles`.`created_at`) > '2018-03-09 17:21:11' and `articles`.`published_at` <= '2018-04-09 17:21:11'  = 1 order by `articles`.`created_at` desc limit 5

alexMafus left a reply on Query Performance Question

@Cronix i have made all my fields with index but the problem is when i make a query with ordering and the where has together do you think any other solution than the wherehas i have used?

08 Apr
1 year ago

alexMafus started a new conversation Query Performance Question

Hi i have a function in my Article class that get all my posts base on section(news, lifestyle etc) and works perfect and load time(0,0024) for 500.000 articles but when i simply add an order for example by id desc or by created_at desc the query increase the load time to 2.5s and i dont get the reason here is my function without latest() or orderBy() that cause the problem.

public function scopeGetArticlesBySection($query, $section, $numOfArticles) { 
        return  $query
               ->where('active',1)
                ->with('category')
                ->whereNull('author_id')
                ->whereDate('created_at', '>', \Carbon\Carbon::now()->subMonth())
                ->where('published_at', '<=', Carbon::now())
                ->whereHas('category', function($query) use ($section) {
                    $query->where('section', $section);
                })->limit($numOfArticles);
 }

alexMafus started a new conversation Query Issue Very Slow(please Help In Peoduction)

Hi i build a portal and today i try migrate my data(120.000 articles) to my new site and i check(laravel-debugbar)that my query are very slow for example in my home page here are the queries i make and the load time is 10s.

Article Model

  public function scopeGetLatestArticles($query, $section1=null, $section2=null,  $numOfArticles) {
        return  $query->latest()
                ->where('active',1)
                ->with('category')
                ->whereNull('author_id')
                ->where('published_at', '<=', Carbon::now())
                ->whereHas('category', function($query) use ($section1, $section2) {
                    $query->where('section', $section1)
                          ->orWhere('section', $section2);
                })->limit($numOfArticles);
    }

    public function scopeGetArticlesByCategory($query, $slug, $numOfArticles) {
        return $query->latest()
                ->where('active',1)
                ->with('category')
                ->whereNull('author_id')
                ->where('published_at', '<=', Carbon::now())
                ->whereHas('category', function($query) use ($slug) {
                    $query->where('slug', $slug);
                })->limit($numOfArticles);
     }

    public function scopeGetArticlesBySection($query, $section, $numOfArticles) { 
        return  $query->latest()
                ->where('active',1)
                ->with('category')
                ->whereNull('author_id')
                ->where('published_at', '<=', Carbon::now())
                ->whereHas('category', function($query) use ($section) {
                    $query->where('section', $section);
                })->limit($numOfArticles);
    }

    public function scopeGetArticlesByVideo($query, $numOfArticles) {
        return  $query->latest()
                ->where('active',1)
                ->with('category')
                ->whereNull('author_id')
                ->where('published_at', '<=', Carbon::now())
                ->where('youtube_id', '!=', null)
                ->orWhere('kaltura_id', '!=', null)
                ->limit($numOfArticles);
    }

    public function scopeGetArticlesByAuthor($query, $numOfArticles) {
        return  $query->latest()
                ->where('active',1)
                ->with('category')
                ->whereNotNull('author_id')
                ->where('published_at', '<=', Carbon::now())
                ->limit($numOfArticles);    
    }

    public function scopeArticle($query, $id, $category) {
       return $query->where('active',1)
              ->whereNull('author_id')
              ->where('published_at', '<=', Carbon::now())
              ->where('id', $id)
              ->firstOrFail();
    }

Article Controller

 /* Top Articles */
      $topArticles = Article::getLatestArticles('news', 'lifestyle', 5)->get();
      $firstTopArticle = $topArticles->splice(0,1)->first();
      $secondTopArticle = $topArticles->splice(0,1)->first();
      

      /* News Articles */
      $newsArticles = Article::getArticlesBySection('news', 7)->get();
      $newsFisrtArticle = $newsArticles->splice(0,1)->first();
      $newsSecondArticle = $newsArticles->splice(0,1)->first();
      $newsThirdArticle = $newsArticles->splice(0,1)->first();

      /* Lifestyle Articles */
      $lifestyleArticles = Article::getArticlesBySection('lifestyle', 7)->get();
      $lifestyleFisrtArticle = $lifestyleArticles->splice(0,1)->first();

      /* Authors */
      $latestAuthors = Author::getAuthors()->get();

      /* Video Articles */
      $videoArticles = Article::getArticlesByVideo(4)->get();
      $videoFirstArticle = $videoArticles->splice(0,1)->first();
31 Mar
1 year ago

alexMafus started a new conversation Ckeditor And Social Media

Hi guys do you know any good free plugin for embed social media posts(instagram, fb etc) i used Media Embed but seems to need money and i have limited requests.

28 Mar
1 year ago

alexMafus left a reply on Csrf Token Question

@Cronix i remove the csrf from my app so now the session time is 120 ? and now i have any possibility to show me the page expired message?

27 Mar
1 year ago

alexMafus left a reply on Csrf Token Question

@carlcassar I know that but i have a single page application with one route and is a registration form so the csrf token creates my trouble because have a lot of users and throws me a page expired error so except the security issue effects my project?

alexMafus started a new conversation Csrf Token Question

HI, because i have a problem with csrf token to production problem i added in comments in the kernel file.

protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
           // \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
 ],

This change maybe cause any problem ???

26 Mar
1 year ago

alexMafus left a reply on The Page Has Expired Due To Inactivity. Please Refresh And Try Again.

@Snapey i have my form to root route / so i should use something like this ?

protected $except = [ '/' ];

alexMafus left a reply on The Page Has Expired Due To Inactivity. Please Refresh And Try Again.

@Snapey how can i remove this page expired token so to make my form work again because is in production.

alexMafus left a reply on The Page Has Expired Due To Inactivity. Please Refresh And Try Again.

but my form is public so can anyone make a registration like a contact form @Snapey do you think is permission problem because earlier i delete a post from my db?

Please help is in production.

alexMafus started a new conversation The Page Has Expired Due To Inactivity. Please Refresh And Try Again.

Hi i have a form in production(linux machine) and sometimes when i submit the form throws me this error The page has expired due to inactivity. Please refresh and try again.

I dont make any ajax requests and i have the csrf field please help

21 Mar
1 year ago

alexMafus started a new conversation Email Problem(help Production)

Hi i have created a simple registration form but sometimes throws me this error Connection to tcp://smtp.office365.com:587 Timed Out. i use office 365 and smtp for sending mails.

Here is my controller

public function store(Request $request) {
        $player = new Player;
        $player->first_name = $request->get('first_name');
        $player->last_name = $request->get('last_name');
        $player->emailOne = $request->get('emailOne');
        $image = $request->file('photo');
        $filename  = uniqid() . '.' . $image->getClientOriginalExtension();
        $location = public_path('images/players/' . $filename);
        Image::make($image)->save($location); 
        $player->image = $filename;
        $player->save();
        Mail::to($player->emailOne)->send(new verifyMailOne($player));
        if($player->emailTwo) {
           Mail::to($player->emailTwo)->send(new verifyMailTwo($player));
        } 
    }

Here is my mail

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Player;

class verifyMailOne extends Mailable
{
    use Queueable, SerializesModels;
  
    public $player;
    
    public function __construct(Player $player) {
        $this->player = $player;
    }

    public function build() {
        return $this->view('verificationTemplateOne');
    }
}

Here is my configuration

    driver' => 'smtp',
    'host' => 'smtp.office365.com',
    'port' => 587,
    'from' => array('address' => '[email protected]', 'name' => 'john doe'),
    'encryption' => 'tls',
    'username' => 'foo_bar',
    'password' => 'pass',
    'sendmail' => '/usr/sbin/sendmail -bs',

    'markdown' => [
        'theme' => 'default',

        'paths' => [
            resource_path('views/vendor/mail'),
        ],
    ],
   
    'stream' => [
        'ssl' => [
            'allow_self_signed' => true,
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ],

];

DO you think this timeout happens because i save first the image and then send the mail or is any other problem.

11 Mar
1 year ago

alexMafus started a new conversation Get Specific Items From Collection(Production!!!)

Hi i have created a blog and i want in my index to display my articles but in the header section i have different ui for the two articles and the same ui for the others so here is my function to my Articles model that i get the articles:

    public function scopeGetArticlesBySection($query, $section, $numOfArticles) {
        return  $query->latest()
                ->where('active',1)
                ->with('user','category')
                ->where('author_id', null)
                ->where('published_at', '<=', Carbon::now())
                ->whereHas('category', function($query) use ($section) {
                    $query->where('section', $section);
                })->limit($numOfArticles);
    }

Here is my controller :

$newsArticles = Article::getArticlesBySection('news', 7)->get();
return view('home.index', compact(['newsArticles']));

So i want to get the first and second articles from my collection and then in the foreach the other ones.

07 Mar
1 year ago

alexMafus left a reply on Directly Loop A Relationship

You should make somethid like this:

$Users = User::with('someCategory')->get();

foreach($Users as $Transaction) {
    // Now I have the transaction
}