moktar
7/14/2019 - 9:44 PM

advanced laravel

<?php


User::all(['name']) // get only names
User::find([1,2,3],['name']) // get only names
User::where('email','s@gmail.com')->firstOrFail()  
User::whereEmail('m@gmail.com')->get()
User::whereMonth('created_at','02')->get()
User::whereYear('created_at','2019')->get()
User::whereDay('created_at','29')->get()
User::whereTime('created_at','11:23:01')->get()
####################################
// WRONG
// this way wull not execute the user id in second where wo will get all users 
User::where('user_id',$id)
    ->where('created_at',$date)
    ->orWhere('updated_at',$date)
    ->get();

// Correct 
// this way wull execute the user id in second where and will get all users 
User::where('user_id',$id)
    ->where(function($query){
      return $query->where('created_at',$date)
          ->orWhere('updated_at',$date)
    })->get();
####################################

// get only users created at last 30 days 
User::where('created_at','>',now()->subdays(30)->get());

// get only Articles created at last 30 days by a specific user 
Article::where('created_at','>',now()->subdays(30)->where('user_id',$request->user_id)->get());

####################################
// ex: search query  With If 
$query = Article::query();
if(request('user_id')){
  $query->where('user_id',request('user_id'));
}
if(request('title')){
  $query->where('title',request('title'));
}
$result = $query->get();

// ex: search query  With When
$result = Article::when(request('user_id'),function($query) use ($user_id){
  return $query->where('user_id',$user_id);
})->when(request('title'),function($query){
  return $query->where('title',request('title'));
})-get();
####################################

// DB: 
// select users and calulate difference btween created and updated 
$users = 
User::select(DB::raw('id,name,email,created_at,DATEDIFF(updated_at,created_at) as days_active'))->get();
$users = 
User::selectRaw('id,name,email,created_at,DATEDIFF(updated_at,created_at) as days_active')->get();

$users = 
User::select(DB::raw('id,name,email,created_at,DATEDIFF(updated_at,created_at) as days_active'))
->whereRaw('DATEDIFF(updated_at,created_at) > 300')->get();

$users = 
User::select(DB::raw('id,name,email,created_at,DATEDIFF(updated_at,created_at) as days_active'))
->orderByRaw('DATEDIFF(updated_at,created_at) desc')->get();

####################################

$articles = Article::all();
$titles = [];

// normal way 
foreach($articles as $article){
  if(strlen($article->title) >40){
    $articles[] = $article->title
  }
}

// via collections 
$articles->filter(function($article){
  return strlen($article) > 40;
})->map(function($article){
  return $article->title;
})


####################################
// add dynamicly option to dropdown 
$users = User::select(['name','id'])->get()->prepend(new User(['name'=>'--- please chose a user ---']));

// return random user from array
$users = User::select(['name','id'])->get()->shuffle();
// Or 
$users = User::pluck('name','id');
//and in blade 
@foreach($users as $id => $name)
<option value="{{$id}}">{{$name}}</option>
@endforeach

// chunk 
// chunk mean separete users to teams every team with 3 users 
$teams = User::select(['name','id'])->get()->shuffle()->chunk(3);
// in blade 
@foreach($teams as $team){
<b>Team {{$loop->iteration}}</b>
<ul>@foreach($team as $user)</ul>
<li>{{$user->name}}</li>
@endforeach</ul>
}

// random just one user 
$teams = User::select(['name','id'])->get()->random();

// contains
$users = User::select(['name','id','password'])->get();
if($users->contains('password','ajlflksjfkaljsflkajs')){
  dd('not ready');
}
// select from authors where name moktar and books title like "learn"
// with query builder 
$books = Book::join('authors','books.author_id','=','author.id')
        ->where('authors.name','moktar')
        ->where('title','like','%learn%')
        ->pluck('title');
// the same query with eloquent 
$author = Author::where('authors.name','moktar')->first();
$books = $author->books()->where('title','like','%learn%')
          ->pluck();
          
// select author with more than 5 books 
Author::has('books','>',5)->get();
// select only rated books 
Author::has('books.rating')->get();

Author::whereHas('books',function($query){
  $query->where('title','like','')
})