Laravel
Запросы на чтение
$user= User::findOrFail($id);$user= User::firstOrCreate(['email'=>$email]);$user= User::find(1);$users= User::find([1,2,3]);
$media= Media::find($id);$categories= Category::lists('category','id');return view('medias.edit-media')->with('media',$media)->with('categories',$categories);
IN
Game::whereIn('games.id',$roomList)->get();
Column
Game::whereIn('id',$roomList)->pluck('id')->toArray();
Select fields
Table::select('name','surname')->where('id',1)->get();
AsArray
Game::select('games.id as id')->whereIn('games.id',$roomList)->get()->toArray();
NotNull
Model::whereNotNull('sent_at'); DB::table('table_name')->whereNotNull('sent_at')->get();
Group where
$results= DB::table('table')->where(function($query)use($starttime,$endtime){$query->where('starttime','<=',$starttime);$query->where('endtime','>=',$endtime);})->orWhere(function($query)use($otherStarttime,$otherEndtime){$query->where('starttime','<=',$otherStarttime);$query->where('endtime','>=',$otherEndtime);})->orWhere(function($query)use($anotherStarttime,$anotherEndtime){$query->where('starttime','>=',$anotherStarttime);$query->where('endtime','<=',$anotherEndtime);})->get();
Sub query
$sub= Abc::where(..)->groupBy(..);// Eloquent Builder instance $count= DB::table( DB::raw("({$sub->toSql()}) as sub")) // ->where(..) wrong ->mergeBindings($sub->getQuery())// you need to get underlying Query Builder // ->where(..) correct ->count();
DB::query()->fromSub(function($query){$query->from('abc')->groupBy('col1');},'a')->count();
Запросы на добавление/обновление
$friend= Friend::updateOrCreate(['user_id'=>$friendId,'friend_id'=>$userId],['status'=> Friend::STATUS_ACCEPTED]);
# https://laravel.com/docs/5.3/eloquent#inserting-and-updating-models
Update all
YourModelName::where(['siteView'=>6])->update(['siteView'=>7]); YourModelName::where('siteView',6)->update(['siteView'=>7]); YourModelName::query()->update(['siteView'=>8]);
Заполнение/обновление/проверка пивот таблиц
$room->games()->attach($gameId);$owner->rooms()->attach($room->id,['is_admin'=>true]);$user->roles()->updateExistingPivot($roleId,$attributes); $post->comments()->saveMany([new App\Comment(['message'=>'A new comment.']),new App\Comment(['message'=>'Another comment.']),]); $user->roles()->attach($roleId);$user->roles()->attach($roleId,[‘expires’ =>$expires]); App\User::find(1)->roles()->save($role,['expires'=>$expires]); $user->roles()->toggle([1,2,3]); $messages= Message::where('message_id',$id)->get();foreach($messagesas$message){$message->users()->updateExistingPivot($user,array('status'=>1),false);} $items=$invoice->items->pluck('name','id')->toArray();foreach($itemsas$key=>$item){$invoice->items()->updateExistingPivot($key,['quantity'=>$request->quantity]);} $user->rooms()->get()->contains($room->id)
Increment
$article= Article::find($article_id);$article->increment('read_count'); Article::find($article_id)->increment('read_count'); Article::find($article_id)->increment('read_count',10);// +10 Product::find($produce_id)->decrement('stock');// -1
Запросы на удаление
$ids=array(10,20,30); DB::table('table_name')->whereIn('id',$ids)->delete(); MyModel::truncate(); \App\Model::query()->delete(); DB::statement("SET foreign_key_checks=0"); Model::truncate(); DB::statement("SET foreign_key_checks=1"); DB::table('table_name')->truncate(); DB::table('table_name')->delete(); Model::whereRaw('1=1')->delete(); User:where('id','like''%%')->delete(); DB::table('users')->whereIn('id',$ids_to_delete)->delete();
Transactions
app('db')->beginTransaction(); app('db')->commit(); app('db')->rollBack();