9 changed files with 158 additions and 2 deletions
@ -0,0 +1,26 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers; |
||||
|
|
||||
|
use App\Http\Forms\SchoolForms\AddParentForm; |
||||
|
use App\Http\Forms\SchoolForms\ManipulateParentForm; |
||||
|
|
||||
|
class ParentsController extends Controller |
||||
|
{ |
||||
|
public function add(AddParentForm $form) |
||||
|
{ |
||||
|
return $form->run(); |
||||
|
} |
||||
|
|
||||
|
// add many -_- |
||||
|
|
||||
|
public function update(ManipulateParentForm $form) |
||||
|
{ |
||||
|
return $form->save(); |
||||
|
} |
||||
|
|
||||
|
public function delete(ManipulateParentForm $form) |
||||
|
{ |
||||
|
return $form->delete(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,30 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Forms\SchoolForms; |
||||
|
|
||||
|
use App\StudentParent; |
||||
|
use Illuminate\Foundation\Http\FormRequest; |
||||
|
|
||||
|
class AddParentForm extends FormRequest |
||||
|
{ |
||||
|
public function rules() |
||||
|
{ |
||||
|
return [ |
||||
|
'mobile' => 'required|regex:/^(\+\d{1,3}[- ]?)?\d{10}$/', |
||||
|
//student |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function run() |
||||
|
{ |
||||
|
$parent = StudentParent::where(['mobile' => $this->mobile])->first(); |
||||
|
if ($parent) { |
||||
|
return $parent; |
||||
|
} else { |
||||
|
$parent = StudentParent::create(['mobile' => $this->mobile]); |
||||
|
$parent->save(); |
||||
|
//sync student |
||||
|
return $parent; |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,31 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Forms\SchoolForms; |
||||
|
|
||||
|
use App\StudentParent; |
||||
|
use Illuminate\Foundation\Http\FormRequest; |
||||
|
|
||||
|
class ManipulateParentForm extends FormRequest |
||||
|
{ |
||||
|
public function rules() |
||||
|
{ |
||||
|
return [ |
||||
|
'mobile' => 'required|exsits:student_parents', |
||||
|
]; |
||||
|
} |
||||
|
|
||||
|
public function save() |
||||
|
{ |
||||
|
$parent = StudentParent::where('mobile', $this->mobile)->first(); |
||||
|
$parent->mobile = $this->mobile; |
||||
|
$parent->save(); |
||||
|
return $parent; |
||||
|
} |
||||
|
|
||||
|
public function delete() |
||||
|
{ |
||||
|
$parent = StudentParent::where('mobile', $this->mobile)->first(); |
||||
|
$parent->delete(); |
||||
|
return []; |
||||
|
} |
||||
|
} |
@ -0,0 +1,10 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App; |
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
|
||||
|
class Parent extends Model |
||||
|
{ |
||||
|
// |
||||
|
} |
@ -0,0 +1,19 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App; |
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Model; |
||||
|
|
||||
|
class StudentParent extends Model |
||||
|
{ |
||||
|
protected $fillable = [ |
||||
|
'mobile', 'fcm_token', |
||||
|
]; |
||||
|
|
||||
|
public $timestamps = false; |
||||
|
|
||||
|
public function scopeVerified($query) |
||||
|
{ |
||||
|
return $query->whereNotNull('fcm_token'); |
||||
|
} |
||||
|
} |
@ -0,0 +1,34 @@ |
|||||
|
<?php |
||||
|
|
||||
|
use Illuminate\Database\Migrations\Migration; |
||||
|
use Illuminate\Database\Schema\Blueprint; |
||||
|
use Illuminate\Support\Facades\Schema; |
||||
|
|
||||
|
class AddParentTable extends Migration |
||||
|
{ |
||||
|
/** |
||||
|
* Run the migrations. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function up() |
||||
|
{ |
||||
|
Schema::create('student_parents', function (Blueprint $table) { |
||||
|
$table->id(); |
||||
|
$table->string('mobile')->unique(); |
||||
|
$table->string('fcm_token')->nullable()->default(null); |
||||
|
$table->timestamps(); |
||||
|
$table->softDeletes(); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Reverse the migrations. |
||||
|
* |
||||
|
* @return void |
||||
|
*/ |
||||
|
public function down() |
||||
|
{ |
||||
|
Schema::dropIfExists('parents'); |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue