Skip to content

Commit c9c7085

Browse files
authored
Merge pull request #4 from IFRCGo/feature/WN-172
#WN-172# Add Models backend (key messages, supporting messages)
2 parents d292fc0 + 0444d68 commit c9c7085

File tree

3 files changed

+150
-2
lines changed

3 files changed

+150
-2
lines changed

app/Models/KeyMessage.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\Validator;
7+
8+
class KeyMessage extends Model
9+
{
10+
/**
11+
* The database table used by the model.
12+
*
13+
* @var string
14+
*/
15+
protected $table = 'key_messages';
16+
17+
/**
18+
* @var
19+
*/
20+
protected $errors;
21+
22+
/**
23+
* @var bool
24+
*/
25+
public $timestamps = false;
26+
27+
/**
28+
* The attributes that are mass assignable.
29+
*
30+
* @var array
31+
*/
32+
protected $fillable = ['entities_stage_id', 'title'];
33+
34+
/**
35+
* Model validation rules
36+
*
37+
* @var array
38+
*/
39+
protected $rules = [
40+
'entities_stage_id' => 'required|integer',
41+
'title' => 'required|string|between:2,255',
42+
];
43+
44+
public function validate($data)
45+
{
46+
$v = Validator::make($data, $this->rules);
47+
48+
if ($v->fails()) {
49+
$this->errors = $v->errors();
50+
51+
return false;
52+
}
53+
54+
return true;
55+
}
56+
57+
/**
58+
* @return mixed
59+
*/
60+
public function errors()
61+
{
62+
return $this->errors;
63+
}
64+
65+
public function entityStage()
66+
{
67+
return $this->belongsTo(WhatnowEntityStage::class, 'entities_stage_id');
68+
}
69+
70+
public function supportingMessages()
71+
{
72+
return $this->hasMany(SupportingMessage::class, 'key_message_id');
73+
}
74+
}

app/Models/SupportingMessage.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Support\Facades\Validator;
7+
8+
class SupportingMessage extends Model
9+
{
10+
/**
11+
* The database table used by the model.
12+
*
13+
* @var string
14+
*/
15+
protected $table = 'supporting_messages';
16+
17+
/**
18+
* @var
19+
*/
20+
protected $errors;
21+
22+
/**
23+
* @var bool
24+
*/
25+
public $timestamps = false;
26+
27+
/**
28+
* The attributes that are mass assignable.
29+
* @var array
30+
*/
31+
protected $fillable = ['key_message_id', 'content'];
32+
33+
/**
34+
* Model validation rules
35+
* @var array
36+
*/
37+
protected $rules = [
38+
'key_message_id' => 'required|integer',
39+
'content' => 'required|string|between:2,1000',
40+
];
41+
42+
/**
43+
* @param $data
44+
* @return bool
45+
*/
46+
public function validate($data)
47+
{
48+
$v = Validator::make($data, $this->rules);
49+
50+
if ($v->fails()) {
51+
$this->errors = $v->errors();
52+
53+
return false;
54+
}
55+
56+
return true;
57+
}
58+
59+
/**
60+
* @return mixed
61+
*/
62+
public function errors()
63+
{
64+
return $this->errors;
65+
}
66+
67+
public function keyMessage()
68+
{
69+
return $this->belongsTo(KeyMessage::class, 'key_message_id');
70+
}
71+
}

app/Models/WhatNowEntityStage.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class WhatNowEntityStage extends Model
4343
'translation_id',
4444
'language_code',
4545
'stage',
46-
'content'
4746
];
4847

4948
/**
@@ -54,7 +53,6 @@ class WhatNowEntityStage extends Model
5453
protected $rules = [
5554
'language_code' => 'required|string|between:2,10',
5655
'stage' => 'string|max:10',
57-
'content' => 'string'
5856
];
5957

6058
/**
@@ -86,5 +84,10 @@ public function translation()
8684
{
8785
return $this->belongsTo('App\Models\WhatNowEntityTranslation', 'translation_id', 'id');
8886
}
87+
88+
public function keyMessages()
89+
{
90+
return $this->hasMany(KeyMessage::class, 'entities_stage_id', 'id');
91+
}
8992
}
9093

0 commit comments

Comments
 (0)