xxxxxxxxxx
<?php
$flights = App\Models\Flight::all();
foreach ($flights as $flight) {
echo $flight->name;
}
xxxxxxxxxx
$searchTerm ='milad zamir Abc';
$reservedSymbols = ['-', '+', '<', '>', '@', '(', ')', '~'];
$searchTerm = str_replace($reservedSymbols, ' ', $searchTerm);
$searchValues = preg_split('/\s+/', $searchTerm, -1, PREG_SPLIT_NO_EMPTY);
$res = User::where(function ($q) use ($searchValues) {
foreach ($searchValues as $value) {
$q->orWhere('name', 'like', "%{$value}%");
$q->orWhere('family_name', 'like', "%{$value}%");
}
})->get();
xxxxxxxxxx
$results = DB::table('orders')
->where('branch_id', Auth::user()->branch_id)
->when($request->customer_id, function($query) use ($request){
return $query->where('customer_id', $request->customer_id);
})
->get();
xxxxxxxxxx
$query = Author::query();
$query->when(request('filter_by') == 'likes', function ($q) {
return $q->where('likes', '>', request('likes_amount', 0));
});
$query->when(request('filter_by') == 'date', function ($q) {
return $q->orderBy('created_at', request('ordering_rule', 'desc'));
});
$authors = $query->get();
xxxxxxxxxx
// SEARCH QUERY
if (request()->has('search')){
$search = trim(request('search'));
$resumess = $resumess->where(function($query) use ($search){
$query->where('doc_name', 'LIKE', "%{$search}%");
$searches = explode(" ",$search);
foreach($searches as $s){
$query->orWhere('first_name', 'LIKE', "%{$s}%");
$query->orWhere('last_name', 'LIKE', "%{$s}%");
$query->orWhere('position_type', 'LIKE', "%{$s}%");
$query->orWhere('position_type', 'LIKE', "%{$s}%");
}
});
}
xxxxxxxxxx
Model::where(function ($query) {
$query->where('a', '=', 1)
->orWhere('b', '=', 1);
})->where(function ($query) {
$query->where('c', '=', 1)
->orWhere('d', '=', 1);
});
xxxxxxxxxx
This:
Model::where()->get();
Is the same as:
Model::query()->where()->get();
Model::query() used to instantiate a query and then build up conditions based on request variables.
$query = Model::query();
if ($request->color) {
$query->where('color', $request->color);
}
xxxxxxxxxx
//No translations
$banners = Banner::where(function ($query) {
$query->where('title', 'like', '%'.$this->search.'%')
->orWhere('description', 'like', '%'.$this->search.'%');
})
->where('type_id', $this->type->id)
->orderBy($this->sort, $this->direction)
->paginate($this->cant);
//with translations
$banners = Banner::whereNull('deleted_at')->withTranslation()
->where(function ($query) {
$query->where('type_id', $this->type->id)
->orWhereTranslationLike('title', '%'.$this->search.'%')
->where('type_id', $this->type->id)
->orWhereTranslationLike('description', '%'.$this->search.'%')
->where('type_id', $this->type->id);
})
->orderBy($this->sort, $this->direction)
->paginate($this->cant);
xxxxxxxxxx
// Retrieve a model by its primary key...
$flight = App\Models\Flight::find(1);
// Retrieve the first model matching the query constraints...
$flight = App\Models\Flight::where('active', 1)->first();
// Shorthand for retrieving the first model matching the query constraints...
$flight = App\Models\Flight::firstWhere('active', 1);
xxxxxxxxxx
public function index()
{
$q = Dog::query();
if (Input::has('search'))
{
// simple where here or another scope, whatever you like
$q->where('name','like',Input::get('search'));
}
if (Input::has('search-breed'))
{
$q->searchBreed(Input::get('search-breed'));
}
if (Input::has('sex'))
{
$q->where('sex', Input::get('sex'));
}
if (Input::has('radius'))
{
$q->withinRadius(Input::get('radius'));
}
$dogs = $q->orderBy(..)->paginate(5);
// ...
xxxxxxxxxx
DB::table('users')->insert([
'email' => 'kayla@example.com',
'votes' => 5
]);