Last active
April 10, 2025 08:32
-
-
Save hassansin/2fb9d72b51860d454316 to your computer and use it in GitHub Desktop.
Laravel 5 Eloquent CheatSheet #laravel #eloquent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Model:: | |
/*Select*/ | |
select('col1','col2') | |
->select(array('col1','col2')) | |
->select(DB::raw('businesses.*, COUNT(reviews.id) as no_of_ratings, IFNULL(sum(reviews.score),0) as rating')) | |
->addSelect('col3','col4') | |
->distinct() // distinct select | |
->from('table') | |
/*Query*/ | |
->where('column','value') | |
->where('column','LIKE','%'.$value.'%') | |
->orWhere('column','!=', 'value') | |
->whereIn('column',[1,2,3]) | |
->whereRaw('age > ? and votes = 100', array(25)) | |
->whereNotIn('id', function($query){ | |
$query->select('city_id') | |
->from('addresses') | |
->groupBy('addresses.city_id'); | |
}) | |
->whereRaw(DB::raw("id in (select city_id from addresses GROUP BY addresses.city_id)")) | |
->whereExists(function($query) | |
{ | |
$query->select(DB::raw(1)) | |
->from('business_language') | |
->whereRaw('business_language.language_id = languages.id') | |
->groupBy('business_language.language_id') | |
->havingRaw("COUNT(*) > 0"); | |
}) | |
/*Joins*/ | |
->join('business_category','business_category.business_id','=','businesses.id') | |
->leftJoin('reviews','reviews.business_id', '=', 'businesses.id') | |
->join('business_category',function($join) use($cats) { | |
$join->on('business_category.business_id', '=', 'businesses.id') | |
->on('business_category.id', '=', $cats, 'and', true); | |
}) | |
->join(DB::raw('(SELECT *, ROUND(AVG(rating),2) avg FROM reviews WHERE rating!=0 GROUP BY item_id ) T' ), function($join){ | |
$join->on('genre_relation.movie_id', '=', 'T.id') | |
}) | |
/*Eager Loading */ | |
->with('table1','table2') | |
->with(array('table1','table2','table1.nestedtable3')) | |
->with(array('posts' => function($query) use($name){ | |
$query->where('title', 'like', '%'.$name.'%') | |
->orderBy('created_at', 'desc'); | |
})) | |
/*Grouping*/ | |
->groupBy('state_id','locality') | |
->havingRaw('count > 1 ') | |
->having('items.name','LIKE',"%$keyword%") | |
->orHavingRaw('brand LIKE ?',array("%$keyword%")) | |
/*Cache*/ | |
->remember($minutes) | |
->rememberForever() | |
/*Offset & Limit*/ | |
->take(10) | |
->limit(10) | |
->skip(10) | |
->offset(10) | |
/*Order*/ | |
->orderBy('id','DESC') | |
->latest() // on 'created_at' column | |
->latest('column') | |
->oldest() // on 'created_at' column | |
->oldest('column') | |
/*Getters*/ | |
->find($id) | |
->find($id, array('col1','col2')) | |
->findOrFail($id) | |
->first(array('col1','col2')) | |
->firstOrFail() | |
->all() | |
->get() | |
->get(array('col1','col2')) | |
->getFresh() // no caching | |
->getCached() // get cached result | |
->chunk(1000, function($rows){ | |
$rows->each(function($row){ | |
}); | |
}) | |
->lists('column') // numeric index | |
->lists('column','id') // 'id' column as index | |
->lists('column')->implode('column', ',') // comma separated values | |
->paginate(10) | |
->paginate(10, array('col1','col2')) | |
->simplePaginate(10) | |
->count() | |
->count(DB::raw('distinct businesses.id')); |
Thanks! Bookmarked!
Thanks, but that one is more complete: http://cheats.jesse-obrien.ca/
orWhereNotNull doesn't existe in laravel 5.5 !!
@MisterDuval your link is not valid, can you post a new one?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very helpful, thanks!!