create many to many relation between rahdaran and road item
This commit is contained in:
@@ -50,14 +50,14 @@ class RoadItemsProjectController extends Controller
|
|||||||
$query = null;
|
$query = null;
|
||||||
|
|
||||||
if ($user->hasPermissionTo('show-road-item-supervise-cartable')) {
|
if ($user->hasPermissionTo('show-road-item-supervise-cartable')) {
|
||||||
$query = RoadItemsProject::query()->where('is_new', 1)->with('files');
|
$query = RoadItemsProject::query()->where('is_new', 1)->with('files', 'rahdaran:id,name,code');
|
||||||
}
|
}
|
||||||
elseif ($user->hasPermissionTo('show-road-item-supervise-cartable-province')) {
|
elseif ($user->hasPermissionTo('show-road-item-supervise-cartable-province')) {
|
||||||
if (is_null($user->province_id)) {
|
if (is_null($user->province_id)) {
|
||||||
return $this->errorResponse('استانی برای شما در سامانه ثبت نشده است!');
|
return $this->errorResponse('استانی برای شما در سامانه ثبت نشده است!');
|
||||||
}
|
}
|
||||||
$query = RoadItemsProject::query()->where('is_new', 1)
|
$query = RoadItemsProject::query()->where('is_new', 1)
|
||||||
->with('files')->where('province_id', auth()->user()->province_id);
|
->with('files', 'rahdaran:id,name,code')->where('province_id', auth()->user()->province_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = DataTableFacade::run(
|
$data = DataTableFacade::run(
|
||||||
@@ -154,7 +154,7 @@ class RoadItemsProjectController extends Controller
|
|||||||
$allowedFilters = $columns;
|
$allowedFilters = $columns;
|
||||||
$allowedSortings = $columns;
|
$allowedSortings = $columns;
|
||||||
|
|
||||||
$query = RoadItemsProject::with('files')
|
$query = RoadItemsProject::with(['files', 'rahdaran:id,name,code'])
|
||||||
->where('is_new', 1)
|
->where('is_new', 1)
|
||||||
->where('user_id', auth()->user()->id);
|
->where('user_id', auth()->user()->id);
|
||||||
|
|
||||||
@@ -207,7 +207,6 @@ class RoadItemsProjectController extends Controller
|
|||||||
$start_coordinates = explode(',', $request->start_point);
|
$start_coordinates = explode(',', $request->start_point);
|
||||||
$end_coordinates = $info_item->needs_end_point ? explode(',', $request->end_point) : null;
|
$end_coordinates = $info_item->needs_end_point ? explode(',', $request->end_point) : null;
|
||||||
$cmmsMachine = CMMSMachine::query()->find($request->cmms_machine_id);
|
$cmmsMachine = CMMSMachine::query()->find($request->cmms_machine_id);
|
||||||
$rahdar = Rahdaran::query()->find($request->rahdar_id);
|
|
||||||
|
|
||||||
$attributes = [
|
$attributes = [
|
||||||
'start_lat' => $start_coordinates[0],
|
'start_lat' => $start_coordinates[0],
|
||||||
@@ -238,8 +237,6 @@ class RoadItemsProjectController extends Controller
|
|||||||
'activity_time' => $request->activity_time,
|
'activity_time' => $request->activity_time,
|
||||||
'cmms_machine_id' => $cmmsMachine->id,
|
'cmms_machine_id' => $cmmsMachine->id,
|
||||||
'cmms_machine_code' => $cmmsMachine->machine_code,
|
'cmms_machine_code' => $cmmsMachine->machine_code,
|
||||||
'rahdar_id' => $rahdar->id,
|
|
||||||
'rahdar_code' => $rahdar->code,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
$after_image = ($request->has('after_image') && $info_item->needs_image) ?
|
$after_image = ($request->has('after_image') && $info_item->needs_image) ?
|
||||||
@@ -252,6 +249,8 @@ class RoadItemsProjectController extends Controller
|
|||||||
|
|
||||||
$road_item = RoadItemsProject::store($user, $attributes, $before_image, $after_image, $request->observed_item_id);
|
$road_item = RoadItemsProject::store($user, $attributes, $before_image, $after_image, $request->observed_item_id);
|
||||||
|
|
||||||
|
$road_item->rahdaran()->attach($request->rahdaran_id);
|
||||||
|
|
||||||
if ($road_item == -1) {
|
if ($road_item == -1) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'message' => 'اقدام مربوطه قبلا رسیدگی شده است.'
|
'message' => 'اقدام مربوطه قبلا رسیدگی شده است.'
|
||||||
@@ -286,7 +285,6 @@ class RoadItemsProjectController extends Controller
|
|||||||
}
|
}
|
||||||
|
|
||||||
$cmmsMachine = CMMSMachine::query()->find($request->cmms_machine_id);
|
$cmmsMachine = CMMSMachine::query()->find($request->cmms_machine_id);
|
||||||
$rahdar = Rahdaran::query()->find($request->rahdar_id);
|
|
||||||
|
|
||||||
if ($info_item->needs_image) {
|
if ($info_item->needs_image) {
|
||||||
if ($request->has('after_image')) {
|
if ($request->has('after_image')) {
|
||||||
@@ -324,10 +322,10 @@ class RoadItemsProjectController extends Controller
|
|||||||
'status_fa' => 'در حال بررسی',
|
'status_fa' => 'در حال بررسی',
|
||||||
'cmms_machine_id' => $cmmsMachine->id,
|
'cmms_machine_id' => $cmmsMachine->id,
|
||||||
'cmms_machine_code' => $cmmsMachine->machine_code,
|
'cmms_machine_code' => $cmmsMachine->machine_code,
|
||||||
'rahdar_id' => $rahdar->id,
|
|
||||||
'rahdar_code' => $rahdar->code,
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$roadItemsProject->rahdaran()->sync($request->rahdaran_id);
|
||||||
|
|
||||||
|
|
||||||
auth()->user()->addActivityComplete(1155);
|
auth()->user()->addActivityComplete(1155);
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,8 @@ class StoreRequest extends FormRequest
|
|||||||
'before_image' => 'image|max:4096',
|
'before_image' => 'image|max:4096',
|
||||||
'after_image' => 'image|max:4096',
|
'after_image' => 'image|max:4096',
|
||||||
'cmms_machine_id' => 'required|exists:cmms_machines,id',
|
'cmms_machine_id' => 'required|exists:cmms_machines,id',
|
||||||
'rahdar_id' => 'required|exists:rahdaran,id',
|
'rahdaran_id' => 'required|array',
|
||||||
|
'rahdaran_id.*' => 'required|exists:rahdaran,id',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ class UpdateRequest extends FormRequest
|
|||||||
'before_image' => 'image|max:4096',
|
'before_image' => 'image|max:4096',
|
||||||
'after_image' => 'image|max:4096',
|
'after_image' => 'image|max:4096',
|
||||||
'cmms_machine_id' => 'required|exists:cmms_machines,id',
|
'cmms_machine_id' => 'required|exists:cmms_machines,id',
|
||||||
'rahdar_id' => 'required|exists:rahdaran,id',
|
'rahdaran_id' => 'required|array',
|
||||||
|
'rahdaran_id.*' => 'required|exists:rahdaran,id',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
|||||||
|
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
class Rahdaran extends Model
|
class Rahdaran extends Model
|
||||||
{
|
{
|
||||||
@@ -11,4 +12,10 @@ class Rahdaran extends Model
|
|||||||
|
|
||||||
protected $guarded = [];
|
protected $guarded = [];
|
||||||
protected $table = 'rahdaran';
|
protected $table = 'rahdaran';
|
||||||
|
protected $hidden = ['pivot'];
|
||||||
|
|
||||||
|
public function roadItemsProject(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(RoadItemsProject::class,'rahdaran_road_items_project');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
|||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Http\UploadedFile;
|
use Illuminate\Http\UploadedFile;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
|
|
||||||
class RoadItemsProject extends Model
|
class RoadItemsProject extends Model
|
||||||
{
|
{
|
||||||
@@ -55,8 +56,6 @@ class RoadItemsProject extends Model
|
|||||||
'activity_date_time',
|
'activity_date_time',
|
||||||
'cmms_machine_id',
|
'cmms_machine_id',
|
||||||
'cmms_machine_code',
|
'cmms_machine_code',
|
||||||
'rahdar_id',
|
|
||||||
'rahdar_code',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -134,8 +133,6 @@ class RoadItemsProject extends Model
|
|||||||
'is_new' => 1,
|
'is_new' => 1,
|
||||||
'cmms_machine_id' => $attributes['cmms_machine_id'],
|
'cmms_machine_id' => $attributes['cmms_machine_id'],
|
||||||
'cmms_machine_code' => $attributes['cmms_machine_code'],
|
'cmms_machine_code' => $attributes['cmms_machine_code'],
|
||||||
'rahdar_id' => $attributes['rahdar_id'],
|
|
||||||
'rahdar_code' => $attributes['rahdar_code'],
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// if ($info_item->needs_image) {
|
// if ($info_item->needs_image) {
|
||||||
@@ -165,7 +162,7 @@ class RoadItemsProject extends Model
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return $road_item;
|
||||||
});
|
});
|
||||||
|
|
||||||
return $road_item;
|
return $road_item;
|
||||||
@@ -173,4 +170,9 @@ class RoadItemsProject extends Model
|
|||||||
throw $th;
|
throw $th;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function rahdaran(): BelongsToMany
|
||||||
|
{
|
||||||
|
return $this->belongsToMany(Rahdaran::class,'rahdaran_road_items_project');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use Illuminate\Database\Migrations\Migration;
|
|
||||||
use Illuminate\Database\Schema\Blueprint;
|
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
|
|
||||||
return new class extends Migration
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Run the migrations.
|
|
||||||
*/
|
|
||||||
public function up(): void
|
|
||||||
{
|
|
||||||
Schema::table('road_items_projects', function (Blueprint $table) {
|
|
||||||
$table->foreignId('rahdar_id')->nullable()->constrained('rahdaran');
|
|
||||||
$table->string('rahdar_code')->nullable();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reverse the migrations.
|
|
||||||
*/
|
|
||||||
public function down(): void
|
|
||||||
{
|
|
||||||
Schema::table('road_items_projects', function (Blueprint $table) {
|
|
||||||
$table->dropForeign(['rahdar_id']);
|
|
||||||
$table->dropColumn('rahdar_id');
|
|
||||||
$table->dropColumn('rahdar_code');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('rahdaran_road_items_project', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->foreignId('rahdaran_id')->constrained('rahdaran')->onDelete('cascade');
|
||||||
|
$table->unsignedBigInteger('road_items_project_id');
|
||||||
|
$table->foreign('road_items_project_id')->references('id')->on('road_items_projects')->onDelete('cascade');
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('rahdaran_road_items_project');
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user