Use a trait : http://php.net/manual/en/language.oop5.traits.php
add your function in this trait, and call it at the beginning of your controller.
<?php
namespace App/Traits;
trait Slugify
{
public function slugify($title){
return $slugifiedTitle = Str::lower( Str::slug($title, '-') );
}
}
class PostController extends Controller
{
use \App\Traits\Slugify;
public function create(Create $request)
{
$post = new Post();
$postCategory = PostCategory::findOrFail($request->input('category'));
$post->postcategory_id = $postCategory->id;
$post->user_id = Auth::user()->id;
$post->url = $postCategory->url.'/'.$this->slugify($request->input('title'));
$post->title = $request->input('title');
$post->description = $request->input('description');
$post->body = $request->input('body');
$post->image = '/img/misc/default.jpg';
$post->video = $request->input('video');
if($request->hasFile('image')){
$uploadFile = $request->file('image');
$filename = str_random(6).'.'.$uploadFile->extension();
$uploadFile->storeAs('uploads', $filename);
$post->image = '/uploads/'.$filename;
}
$post->save();
return response()->json([
'post' => $post,
]);
}