Skip to content

Commit 2de4c0d

Browse files
committed
add more progress ...
1 parent eef2f85 commit 2de4c0d

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

src/SortJoinTrait.php

+6-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ trait SortJoinTrait
1414

1515
public function scopeWhereJoin(Builder $builder, $column, $operator = null, $value = null, $boolean = 'and')
1616
{
17-
$column = $this->performJoin($builder, $column);
1817
$this->relationClauses[] = ['column' => $column, 'operator' => $operator, 'value' => $value, 'boolean' => $boolean];
18+
$column = $this->performJoin($builder, $column);
1919

2020
return $builder->where($column, $operator, $value, $boolean);
2121
}
2222

2323
public function scopeOrWhereJoin(Builder $builder, $column, $operator = null, $value)
2424
{
25-
$column = $this->performJoin($builder, $column);
2625
$this->relationClauses[] = ['column' => $column, 'operator' => $operator, 'value' => $value, 'boolean' => 'and'];
26+
$column = $this->performJoin($builder, $column);
2727

2828
return $builder->orWhere($column, $operator, $value);
2929
}
@@ -69,8 +69,10 @@ private function performJoin($builder, $relations){
6969
$builder->leftJoin($relatedTable . ' as ' . $relatedTableAlias, $relatedTableAlias . '.' . $keyRelated, '=', $currentTable . '.' . $relatedPrimaryKey);
7070
}
7171

72-
//apply where deleted_at is null is model using soft deletes
73-
if(method_exists($relatedModel, 'getQualifiedDeletedAtColumn')){
72+
$columnsWhere = collect($relatedModel->relationClauses)->pluck('column')->toArray();
73+
74+
//by default apply where deleted_at is null if model is using soft deletes, if any where clause have deleted_at columnn do not apply
75+
if(method_exists($relatedModel, 'getQualifiedDeletedAtColumn') && ! in_array('deleted_at', $columnsWhere)){
7476
$builder->where([$relatedTableAlias . '.deleted_at' => null]);
7577
}
7678
}

tests/Models/Location.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ class Location extends BaseModel
1010

1111
protected $table = 'locations';
1212

13-
protected $fillable = ['address', 'seller_id'];
13+
protected $fillable = ['address', 'seller_id', 'is_primary', 'is_secondary'];
1414

1515
public function seller()
1616
{
17-
return $this->belongsTo(sSeller::class);
17+
return $this->belongsTo(Seller::class);
1818
}
1919
}

tests/Models/Seller.php

+12
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,16 @@ public function location()
1212
{
1313
return $this->hasOne(Location::class);
1414
}
15+
16+
public function locationPrimary()
17+
{
18+
return $this->hasOne(Location::class)
19+
->whereJoin('is_primary', '=', 1);
20+
}
21+
22+
public function locationSecondary()
23+
{
24+
return $this->hasOne(Location::class)
25+
->whereJoin('is_secondary', '=', 1);
26+
}
1527
}

tests/database/migrations/2017_11_04_163552_create_database.php

+8-6
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ public function up()
4848
$table->softDeletes();
4949
});
5050

51-
Schema::create('locations', function (Blueprint $table) {
52-
$table->increments('id');
53-
$table->string('address')->nullable();
54-
$table->integer('seller_id')->unsigned()->index()->nullable();
51+
Schema::create('locations', function (Blueprint $table) {
52+
$table->increments('id');
53+
$table->string('address')->nullable();
54+
$table->boolean('is_primary')->default(0);
55+
$table->boolean('is_secondary')->default(0);
56+
$table->integer('seller_id')->unsigned()->index()->nullable();
5557

56-
$table->foreign('seller_id')->references('id')->on('sellers')
57-
->onUpdate('cascade')->onDelete('cascade');
58+
$table->foreign('seller_id')->references('id')->on('sellers')
59+
->onUpdate('cascade')->onDelete('cascade');
5860

5961
$table->timestamps();
6062
$table->softDeletes();

0 commit comments

Comments
 (0)