An error occurred while loading the file. Please try again.
-
nuttapong authored12ea9283
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use DB;
use App\Page;
use App\User;
use App\Http\Requests\StorePostRequest;
use Auth;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Post::with([
'comments' => function ($query) {
$query->orderBy('updated_at', 'desc');
}
])
->orderBy('updated_at', 'desc')
->paginate(10);
$pages = Page::where('status',true)->with('followByUsers')->paginate(5)->sortByDesc(function($page){
return $page->followByUsers->count();
});
$user_id = Auth::User()->id;
foreach ($posts as $post) {
if ($post->likeByUsers) {
foreach ($post->likeByUsers as $user) {
if ($user->id == $user_id) {
$post->likeable = false;
break;
}
}
}
if (!isset($post->likeable)) {
$post->likeable = true;
}
if ($post->comments){
foreach ($post->comments as $comment){
if($comment->likeByUsers){
foreach($comment->likeByUsers as $user_comment){
if ($user_comment->id == $user_id){
$comment->likeable = false;
break;
}
}
}
if (!isset($comment->likeable)){
$comment->likeable = true;
}
}
}
}
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
return view('post.newsfeed',
[
'posts' => $posts
,'pages' => $pages
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(StorePostRequest $request)
{
$data = $request->all();
$post = Auth::User()->posts()->create($data);
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$path = $request->file('image')->getRealPath();
$mime_type = $request->file('image')->getClientOriginalExtension();
$destination_path = 'posts/' . $post->id . '.' . $mime_type;
\Storage::put(
$destination_path,
file_get_contents($path)
);
$post->image = $destination_path;
$post->save();
}
return redirect()->back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$post = Post::find($id);
$msg = $post->msg;
return response()->json(['msg' => $msg]);
}