muhammet28 yazdıarkadaşlar whereIn ile istediğim oldu arraydaki değerlerin her biri için sorgu yapıp sonuç dönderiyor ancak şimdi 2 array için ortak sorgu yapmam lazım bunu nasıl sağlarım? Şu an kod bu şekilde
/* Bu kod marka ve ekran boyutları aynı anda check edildiği zaman ortak sonucu döndermeli yani marka [samsung,apple] iken ekran 5 seçilirse şu durumda bütün 5" lik ekranları dönderiyor halbuki sadece samsung ve apple arasından screen_size 5 olanları döndermeli */
if(isset($mark))
{
$result = Feature::whereIn('device_mark', $mark);
}
if(isset($screen_size))
{
$result = Feature::whereIn('screen_size', $screen_size);
}
return $result->get();
iç içe if ile sorgu yapmayı düşündüm ancak ekran ve marka gibi daha birçok parametre var bunları birbiri ile kombinasyonlamak çok vakit alır daha doğru bir yöntem olmalı sanırım
Merhaba,
Üste yazdığınız kodda sadece son if işliyor. Bunun için de yazmıştım cevabı.
Alternatif olarak
if(isset($screen_size))
{
$result = $result->whereIn('screen_size', $screen_size);
}
olabilir di.
Yine de scope daha temiz, tercih sizin. Birden fazla tabloda aynı scope gerekiyorsa trait yazabilirsiniz. Örnek (@mgsmus vermişti diye hatırlıyorum)
<?php
namespace App\Models;
trait ScopeActive
{
/**
* Scope active
* @param mixed $query
* @return mixed
*/
public function scopeActive($query)
{
return $query->where('active', 1);
}
}
Model
class Product extends Model
{
use ScopeActive;
Kullanım aynı
$products = Product::active();
veya soft delete gibi gerekli sorgulara eklenmesi için Global Scope kullanılabilir. Örnek;
<?php
namespace App\Models\Scopes;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class FirmScope implements Scope
{
/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
$builder->where($model->getTable().'.firm_id', '=', session('session-name-??'));// firm id yi session da tutmuştum
}
}
Model
use App\Models\Scopes\FirmScope;
class Product extends Model
{
use ScopeActive;
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::addGlobalScope(new FirmScope);
}
eklenebilir. Detaylı bilgi için link :
https://laravel.com/docs/5.4/eloquent#query-scopes
İyi çalışmalar..