Me and my partner have been working a combined period of almost 11 hours trying to figure this out but we just can't seem to crack it.
We're building a web forum application in which users are able to make their own threads. We managed to get edit to work, although redirect still doesn't work right yet. We have an online preview over at http://detune-niuwang.c9users.io/
If you try using the 'New Thread' page and click on the submit button, it will just delete anything typed into the form and checking back on the Frontpage, no threads would have been created. The Edit functionality works though.
Here are some snippets of our code:
Routes.php
Route::resource('posts', 'Channel\Post\Posts');
Route::get('/', 'Channel\Post\Posts@index');
Controllers\Channel\Post\Posts.php
/**
* Show the form for creating a new post.
*
* @return Response
*/
public function create()
{
return view('posts.create');
}
/**
* Store the newly created post
*
* @param PostRequest $request
* @return Response
*/
public function store(PostRequest $request)
{
$postData = $this->post->create(['title', 'content']);
if($this->post->create($postData)) {
return redirect()->back()->withSuccess('Post successfully created.');
}
return redirect()->back()->withError($postData);
}
create.blade.php
@extends('_shared.master')
@section('title')
Create New Post
@endsection
@section('content')
<div class="panel panel-default">
<div class="panel-heading">New Thread</div>
<div class="panel-body">
{!! Form::open(['route' => 'posts.store', 'class' => 'form-horizontal'])!!}
@include('posts.form')
{!! Form::close() !!}
</div>
</div>
@stop
Models\Channel\Post\Post.php
<?php
namespace Detune\Models\Channel\Post;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
/**
* The database table used by the Model.
*
* @var string
*/
protected $table = 'posts';
/**
* The attributes that are mass assignable
*
* @var array
*/
protected $fillable = ['title', 'content', 'created_at'];
}
Repositories\PostRepository.php
<?php
namespace Detune\Repositories\Post;
use Detune\Models\Channel\Post\Post;
use Illuminate\Database\Eloquent\Collection;
/**
* Class PostRepository
* @Package Detune\Repository
*/
class PostRepository implements PostRepositoryInterface {
/**
* @var Post;
*/
protected $post;
/**
* @param Post $post
*/
public function __construct(Post $post)
{
$this->post = $post;
}
/**
* Create New Post
*
* @param array $postData
* @return Post|null
*/
public function create(array $postData)
{
return $this->post->create($postData);
}
/**
* Post Pagination
*
* @param array $filter
* @return collection
*/
public function paginate(array $filter)
{
return $this->post->paginate($filter['limit']);
}
/**
* Get Post by ID
*
* @param $id
* @return Post
*/
public function find($id)
{
return $this->post->find($id);
}
}
Repositories\PostRepositoryInterface.php
<?php
namespace Detune\Repositories\Post;
use Detune\Models\Channel\Post;
use Illuminate\Database\Eloquent\Collection;
/**
* Interface PostRepositoryInterface
* @package Detune\Repository
*/
interface PostRepositoryInterface {
/**
* Create New Post\
*
* @param array $postData
* @return Post
*/
public function create(array $postData);
/**
* Post Pagination
*
* @param array $filter
* @return collection
*/
public function paginate(array $filter);
/**
* Get Post by ID
* @param $post_id
* @return Post
*/
public function find($id);
}
Services\Post\PostService.php
<?php
namespace Detune\Services\Post;
use Detune\Services\Service;
use Illuminate\Contracts\Logging\Log;
use Illuminate\Support\ServiceProvider;
use Detune\Repositories\Post\PostRepositoryInterface;
/**
* Class PostService
* @package Detune\Services\Post
*/
class PostService extends Service {
/**
* @var PostRepositoryInterface
*/
protected $post;
/**
* @var Log
*/
protected $logger;
/**
* @param PostRepositoryInterface $post
* @param Log $logger
*/
public function __construct(PostRepositoryInterface $post, Log $logger)
{
$this->post = $post;
$this->logger = $logger;
}
/**
* Create New Post
*
* @param array $postData
* @return Post | null
*/
public function create()
{
try{
return $this->post->create($postData);
} catch (\Exception $e) {
$this->logger->error('Post->create: ' . $e->getMessage());
return null;
}
}
/**
* Post Pagination
*
* @param array $filter
* @return collection
*/
public function paginate(array $filter =[])
{
$filter['limit'] = 20;
return $this->post->paginate($filter);
}
/**
* Update the Post
*
* @param array $id
* @param array $postData
* @return bool
*/
public function update($id, array $postData)
{
try{
$post = $this->post->find($id);
$post->title = $postData['title'];
$post->content = $postData['content'];
return $post->save();
} catch (\Exception $e) {
$this->logger->error('Post->update: ' . $e->getMessage());
return false;
}
}
/**
* Delete Post
*
* @param $id
* @return mixed
*/
public function delete($id)
{
try {
$post = $this->post->find($id);
return $post->delete();
} catch (\Exception $e){
$this->logger->error('Post->delete: ' . $e->getMessage());
return false;
}
}
/**
* Get Post by ID
*
* @param $id
* @return Post
*/
public function find($id)
{
try {
return $this->post->find($id);
} catch (\Exception $e) {
$this->logger->error('Post->find: ' .$e->getMessage());
return null;
}
}
}
Any help with this are appreciated :)