update kolompok belajar
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Web\Program;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Program\DetailKelompok;
|
||||
use App\Models\Program\KelompokBelajarTani;
|
||||
use App\Models\User;
|
||||
use App\Enums\Role;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class DetailKelompokController extends Controller
|
||||
{
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'kelompok_id' => 'required|exists:kelompok_belajar_tani,id',
|
||||
'user_id' => [
|
||||
'required',
|
||||
Rule::exists('users', 'id')->where('role', Role::PETANI->value),
|
||||
],
|
||||
'jabatan' => 'required|string|max:255',
|
||||
]);
|
||||
|
||||
$user = User::with('profile')
|
||||
->where('role', Role::PETANI->value)
|
||||
->findOrFail($request->user_id);
|
||||
|
||||
DetailKelompok::create([
|
||||
'kelompok_id' => $request->kelompok_id,
|
||||
'user_id' => $user->id,
|
||||
'nama_anggota' => $user->name,
|
||||
'jabatan' => $request->jabatan,
|
||||
'no_hp' => $user->profile->telepon ?? null,
|
||||
'alamat' => $user->profile->desakelurahan->nama ?? null,
|
||||
]);
|
||||
|
||||
return back()->with(
|
||||
'success',
|
||||
'Anggota berhasil ditambahkan.'
|
||||
);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$request->validate([
|
||||
'user_id' => [
|
||||
'required',
|
||||
Rule::exists('users', 'id')->where('role', Role::PETANI->value),
|
||||
],
|
||||
'jabatan' => 'required|string|max:255',
|
||||
]);
|
||||
|
||||
$anggota = DetailKelompok::findOrFail($id);
|
||||
|
||||
$user = User::with('profile')
|
||||
->where('role', Role::PETANI->value)
|
||||
->findOrFail($request->user_id);
|
||||
|
||||
$anggota->update([
|
||||
'user_id' => $user->id,
|
||||
'nama_anggota' => $user->name,
|
||||
'jabatan' => $request->jabatan,
|
||||
'no_hp' => $user->profile->telepon ?? null,
|
||||
'alamat' => $user->profile->desakelurahan->nama ?? null,
|
||||
]);
|
||||
|
||||
return back()->with(
|
||||
'success',
|
||||
'Anggota berhasil diperbarui.'
|
||||
);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$anggota = DetailKelompok::findOrFail($id);
|
||||
|
||||
$anggota->delete();
|
||||
|
||||
return back()->with('success', 'Anggota berhasil dihapus.');
|
||||
}
|
||||
|
||||
public function detail($id)
|
||||
{
|
||||
$kelompok = KelompokBelajarTani::with('detailAnggota.user')
|
||||
->findOrFail($id);
|
||||
|
||||
$petani = User::with('profile')
|
||||
->where('role', Role::PETANI->value)
|
||||
->orderBy('name')
|
||||
->get();
|
||||
|
||||
return view('program.kelompokbelajar.detailkelompok', compact(
|
||||
'kelompok',
|
||||
'petani'
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ namespace App\Http\Controllers\Web\Program;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Models\Program\KelompokBelajarTani;
|
||||
use App\Models\Program\DetailKelompok;
|
||||
use App\Models\User;
|
||||
|
||||
class KelompokBelajarTaniController extends Controller
|
||||
{
|
||||
@@ -145,4 +147,19 @@ class KelompokBelajarTaniController extends Controller
|
||||
'data' => $kelompok
|
||||
]);
|
||||
}
|
||||
|
||||
public function detail($id)
|
||||
{
|
||||
$kelompok = KelompokBelajarTani::with('detailAnggota.user')
|
||||
->findOrFail($id);
|
||||
|
||||
$petani = User::where('role', 'petani')
|
||||
->orderBy('name')
|
||||
->get();
|
||||
|
||||
return view('program.kelompokbelajar.detailkelompok', compact(
|
||||
'kelompok',
|
||||
'petani'
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Program;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use App\Models\User;
|
||||
|
||||
class DetailKelompok extends Model
|
||||
{
|
||||
protected $table = 'detail_kelompok';
|
||||
|
||||
protected $fillable = [
|
||||
'kelompok_id',
|
||||
'user_id',
|
||||
'nama_anggota',
|
||||
'jabatan',
|
||||
'no_hp',
|
||||
'alamat'
|
||||
];
|
||||
|
||||
// RELASI KE KELOMPOK
|
||||
public function kelompok()
|
||||
{
|
||||
return $this->belongsTo(
|
||||
KelompokBelajarTani::class,
|
||||
'kelompok_id'
|
||||
);
|
||||
}
|
||||
|
||||
// RELASI KE USER
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(
|
||||
User::class,
|
||||
'user_id'
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace App\Models\Program;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use App\Models\Program\DetailKelompok;
|
||||
|
||||
class KelompokBelajarTani extends Model
|
||||
{
|
||||
@@ -19,4 +20,11 @@ class KelompokBelajarTani extends Model
|
||||
'updated_by',
|
||||
'deleted_by',
|
||||
];
|
||||
|
||||
public function detailAnggota(){
|
||||
return $this->hasMany(
|
||||
DetailKelompok::class,
|
||||
'kelompok_id'
|
||||
);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -37,7 +37,7 @@ class User extends Authenticatable
|
||||
];
|
||||
}
|
||||
|
||||
protected function profile(): HasOne
|
||||
public function profile(): HasOne
|
||||
{
|
||||
return $this->hasOne(\App\Models\Profile::class);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user