Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

laravel framework

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use Session;
use App\Category;
use App\Tag;
use Image;
use Purifier;
use Storage;

class resourceController extends Controller
{

public function index()
{
     $post=post::orderBy('id','Desc')->paginate(5);
     return view('resource.index')->withPost($post);
}

public function create()
{   
  $categories=Category::all();
  $tags=Tag::all();
  return view('resource.create')->withCategories($categories)->withTags($tags);
}

public function store(Request $request) {
$this->validate($request, [
'title' => 'required|max:50|min:3',
'slug' => 'required|max:255|min:5|alpha_dash|unique:posts,slug',
'body' => 'required',
'category_id'=>'required|integer',
'featured_image'=>'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);

    $post = new post;
    $post->title = $request->title;
    $post->body = Purifier::clean($request->body);
    $post->slug = $request->slug;
    $post->category_id=$request->category_id;

  if ($request->hasFile('featured_image')) {
    $image = $request->file('featured_image');
    $filename = time().".".$image->getClientOriginalExtension();
    // go to terminal and permission the images folder;
    //sudo chmod -R 777 images 
    $location = public_path('images/'.$filename);
    Image::make($image)->resize(600,300)->save($location); 
    //Image::make($image->getRealPath())->resize(468, 249)->save($location);    

    $post->image="images/".$filename;

  }


    $post->save();

   $post->tags()->sync($request->tags,false);
  session::flash('success','Blog Post Successfully Saved');
  return redirect()->route('resource.show',$post->id);

}

public function show($id)
{
$post=post::find($id);
return view('resource.show')->withPost($post);
}

public function edit($id)
{
    $post=post::find($id);
    $categories = Category::all();
    $cats =[];
    foreach ($categories as $category) {
      $cats[$category->id]=$category->name;

    }
    $tags = Tag::all();
    $tags2 =[];
    foreach ($tags as $tag) {
      $tags2[$tag->id]=$tag->name;

    }
    return view('resource.edit')->withPost($post)->withCategories($cats)->withTags($tags2);
}


public function update(Request $request, $id)
{
      $post=post::find($id);
  if($request->input('slug') == $post->slug){
       $this->validate($request, [
        'title' => 'required|max:50|min:3',
        'body' => 'required',
        'featured_image'=>'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
    ]);
  }

else {
$this->validate($request, [
'title' => 'required|max:50|min:3',
'slug' => 'required|max:255|min:5|unique:posts,slug',
'body' => 'required',
'featured_image'=>'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
}

    $post=post::find($id);
    $post->title=$request->input('title');
    $post->body=Purifier::clean($request->input('body'));
    $post->slug=$request->input('slug');

     if ($request->hasFile('featured_image')) {
    $image = $request->file('featured_image');
    $filename = time().".".$image->getClientOriginalExtension();
    $location = public_path('images/'.$filename);
    Image::make($image)->resize(600,300)->save($location); 
    $oldFilename = $post->image;

// dd($oldFilename;
// exit();
$post->image="images/".$filename;
Storage::delete($oldFilename);

  }

    $post->save();

if(isset($request->tags)){
$post->tags()->sync($request->tags);
}
else{
$post->tags()->sync(array());
}

  session::flash('success','Blog Post Successfully Saved');
  return redirect()->route('resource.show',$post->id);

}

public function destroy($id)
{
     $post=Post::find($id);
     $post->tags()->detach();
     Storage::delete($post->image);
     $post->delete();
    session::flash('success','Blog Delete has been successfully!');
    return redirect()->route('resource.store');
}

}

Tagged:
Sign In or Register to comment.