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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line