Last active
September 3, 2016 06:24
-
-
Save Luizgpp/74a49f9826edcd863d3df7ccd5c8fa14 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Post extends Model | |
{ | |
public function tags() | |
{ | |
return $this->belongsToMany('App\Tag'); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Http\Requests; | |
use Session; | |
use App\Post; | |
use App\Tag; | |
use App\Category; | |
class PostController extends Controller | |
{ | |
/** | |
* Store a newly created resource in storage. | |
* | |
* @param \Illuminate\Http\Request $request | |
* @return \Illuminate\Http\Response | |
*/ | |
public function store(Request $request) | |
{ | |
$this->validate($request, array( | |
'title' => 'required|max:255', | |
'slug' => 'required|alpha_dash|min:5|max:255|unique:posts,slug', | |
'category_id' => 'required|integer', | |
'body' => 'required' | |
)); | |
$post = new Post; | |
$post->title = $request->title; | |
$post->slug = $request->slug; | |
$post->category_id = $request->category_id; | |
$post->body = $request->body; | |
$post->save(); | |
$post->tags()->sync($request->tags_id, false); | |
Session::flash('success','Post gravado com sucesso!'); | |
return redirect()->route('posts.show', $post->id); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Tag extends Model | |
{ | |
public function Posts() | |
{ | |
return $this->belongsToMany('App\Post'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment