Skip to content

Commit 507599d

Browse files
committed
Templating Out The Main Methods And Traits
Signed-off-by: David Thorpe <davzie@davzie.com>
1 parent 06e06b0 commit 507599d

File tree

4 files changed

+54
-11
lines changed

4 files changed

+54
-11
lines changed

src/Davzie/LaravelBootstrap/Abstracts/Traits/TaggableRelationship.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php namespace Davzie\LaravelBootstrap\Abstracts\Traits;
2+
use Davzie\LaravelBootstrap\Tags\Tags as TagEloquent;
23

34
trait TaggableRelationship
45
{
@@ -25,4 +26,25 @@ public function getTagsCsvAttribute()
2526
return implode( ',' , $tags );
2627
}
2728

29+
/**
30+
* Save tags, pass in a CSV separated list
31+
* @param string $tags A comma separated list of tags
32+
* @return void
33+
*/
34+
public function saveTags( $tags )
35+
{
36+
// Delete all existing tags for this item
37+
$this->tags()->delete();
38+
39+
if( $tags = explode(',',$tags) ){
40+
foreach($tags as $tag){
41+
$tagObject = new TagEloquent();
42+
$tagObject->tag = $tag;
43+
$this->tags()->save( $tagObject );
44+
}
45+
}
46+
47+
return;
48+
}
49+
2850
}

src/Davzie/LaravelBootstrap/Posts/Posts.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use Davzie\LaravelBootstrap\Core\EloquentBaseModel;
33
use Davzie\LaravelBootstrap\Abstracts\Traits\TaggableRelationship;
44
use Davzie\LaravelBootstrap\Abstracts\Traits\UploadableRelationship;
5+
use Str, Input;
56

67
class Posts extends EloquentBaseModel
78
{
@@ -23,8 +24,31 @@ class Posts extends EloquentBaseModel
2324

2425
protected $validationRules = [
2526
'title' => 'required',
26-
// 'slug' => 'required|unique:posts,<id>',
27+
'slug' => 'required|unique:posts,id,<id>',
2728
'content' => 'required'
2829
];
2930

31+
/**
32+
* Fill the model up like we usually do but also allow us to fill some custom stuff
33+
* @param array $array The array of data, this is usually Input::all();
34+
* @return void
35+
*/
36+
public function fill( array $attributes )
37+
{
38+
parent::fill( $attributes );
39+
$this->slug = Str::slug( $this->title , '-' );
40+
}
41+
42+
/**
43+
* Hydrate the model with more stuff and
44+
* @return this
45+
*/
46+
public function hydrate()
47+
{
48+
$this->saveTags( Input::get('tags') );
49+
return $this;
50+
}
51+
52+
53+
3054
}

src/controllers/ObjectBaseController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,5 +206,5 @@ private function setTraitableProperties()
206206
$this->uploadable = in_array( 'Davzie\LaravelBootstrap\Abstracts\Traits\UploadableRelationship' , ( new ReflectionClass( $this->model->getModel() ) )->getTraitNames() );
207207

208208
}
209-
209+
210210
}

src/controllers/PostsController.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,16 @@ public function __construct( PostsInterface $posts )
2727
public function postEdit( $id )
2828
{
2929
$record = $this->model->requireById( $id );
30+
$record->fill( Input::all() );
3031

31-
if( !$record )
32-
return Redirect::to( $this->object_url )->with( 'errors' , new MessageBag( array( 'No record found with ID: '.$id ) ) );
33-
34-
$record->title = Input::get('title');
35-
$record->slug = Str::slug( Input::get('title') , '-' );
36-
$record->content = Input::get('content');
37-
38-
if( !$record->isValid( Input::all() ) )
32+
if( !$record->isValid() )
3933
return Redirect::to( $this->edit_url.$id )->with( 'errors' , $record->getErrors() );
4034

41-
$record->save();
35+
// Run the hydration method that populates anything else that is required / runs any other
36+
// model interactions and save it.
37+
$record->hydrate()->save();
4238

39+
// Redirect that shit man! You did good! Validated and saved, man mum would be proud!
4340
return Redirect::to( $this->object_url )->with( 'success' , new MessageBag( array( 'Item Saved' ) ) );
4441
}
4542

0 commit comments

Comments
 (0)