我和我的伴侣已经工作了近11个小时的合并时间试图解决这个问题,但我们似乎无法破解它。
我们正在构建一个网络论坛应用程序,用户可以在其中创建自己的主题。我们设法让编辑工作,虽然重定向仍然无法正常工作。我们在http://detune-niuwang.c9users.io/
进行了在线预览如果您尝试使用“新主题”'页面并单击提交按钮,它将只删除键入到表单中的任何内容并在Frontpage上重新检查,不会创建任何线程。编辑功能可用。
以下是我们代码的一些片段:
routes.php文件
Route::resource('posts', 'Channel\Post\Posts');
Route::get('/', 'Channel\Post\Posts@index');
控制器\频道\邮政\ 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
模型\频道\邮政\ 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'];
}
库\ 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);
}
}
库\ 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);
}
服务\邮政\ 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;
}
}
}
对此有任何帮助表示赞赏:)
答案 0 :(得分:0)
Eloquent模型上的create
方法通常是静态方法。在你的代码中,它似乎被称为实例方法。
我相信你有(至少)3种选择。
create
来电更改为insert
来电。 (我认为这不会返回Post模型的实例,只是一个布尔值)。Post::create($postData);
使用newInstance
方法
$post = $this->post->newInstance($postData);
return $post->save() ? $post : null;