douwen3127 2016-02-06 09:18
浏览 49
已采纳

Laravel:无法获得发布表格

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 :)

  • 写回答

1条回答 默认 最新

  • drhgzx4727 2016-02-06 09:49
    关注

    The create method on an Eloquent model is normally a static method. In your code it seems to be called as if it's an instance method.

    I believe you have (at least) 3 options.

    1. Change your create call into an insert call. (I don't think this will return an instance of the Post model, just a boolean).
    2. Call it statically Post::create($postData);
    3. Use the newInstance method

      $post = $this->post->newInstance($postData);
      return $post->save() ? $post : null;
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?