php - Laravel: Can't get post form -
me , partner have been working combined period of 11 hours trying figure out can't seem crack it.
we're building web forum application in users able make own threads. managed edit work, although redirect still doesn't work right yet. have online preview on @ http://detune-niuwang.c9users.io/
if try using 'new thread' page , click on submit button, delete typed form , checking on frontpage, no threads have been created. edit functionality works though.
here snippets of our code:
routes.php
route::resource('posts', 'channel\post\posts'); route::get('/', 'channel\post\posts@index');
controllers\channel\post\posts.php
/** * show form creating new post. * * @return response */ public function create() { return view('posts.create'); } /** * store 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 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 { /** * database table used model. * * @var string */ protected $table = 'posts'; /** * attributes 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']); } /** * post 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); /** * post 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 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; } } /** * post 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 appreciated :)
the create
method on eloquent model static method. in code seems called if it's instance method.
i believe have (at least) 3 options.
- change
create
callinsert
call. (i don't think return instance of post model, boolean). - call statically
post::create($postdata);
use
newinstance
method$post = $this->post->newinstance($postdata); return $post->save() ? $post : null;
Comments
Post a Comment