Php 拉威尔:可以';我拿不到邮寄表格
我和我的搭档一起工作了将近11个小时,试图弄明白这一点,但我们似乎无法解决它 我们正在构建一个web论坛应用程序,用户可以在其中创建自己的线程。我们设法让编辑工作,尽管重定向仍然不能正常工作。我们有一个在线预览在 如果您尝试使用“新线程”页面并单击“提交”按钮,它只会删除表单中键入的任何内容,并在Frontpage上检查,则不会创建任何线程。但编辑功能仍然有效 下面是我们的一些代码片段: Routes.php Controllers\Channel\Post\Posts.php create.blade.phpPhp 拉威尔:可以';我拿不到邮寄表格,php,forms,laravel,Php,Forms,Laravel,我和我的搭档一起工作了将近11个小时,试图弄明白这一点,但我们似乎无法解决它 我们正在构建一个web论坛应用程序,用户可以在其中创建自己的线程。我们设法让编辑工作,尽管重定向仍然不能正常工作。我们有一个在线预览在 如果您尝试使用“新线程”页面并单击“提交”按钮,它只会删除表单中键入的任何内容,并在Frontpage上检查,则不会创建任何线程。但编辑功能仍然有效 下面是我们的一些代码片段: Routes.php Controllers\Channel\Post\Posts.php create.b
@extends(“\u shared.master”)
@章节(“标题”)
创建新帖子
@端部
@节(“内容”)
新线程
{!!Form::open(['route'=>'posts.store','class'=>'Form horizontal'])
@包括('posts.form')
{!!Form::close()!!}
@停止
Models\Channel\Post\Post.php
雄辩模型上的创建
方法通常是静态方法。在您的代码中,它的调用好像是一个实例方法
我相信你(至少)有3种选择
将create
调用更改为insert
调用。(我认为这不会返回Post模型的实例,只是一个布尔值)
静态地调用它Post::create($postData)代码>
使用newInstance
方法
$post = $this->post->newInstance($postData);
return $post->save() ? $post : null;
雄辩模型上的create
方法通常是静态方法。在您的代码中,它的调用好像是一个实例方法
我相信你(至少)有3种选择
将create
调用更改为insert
调用。(我认为这不会返回Post模型的实例,只是一个布尔值)
静态地调用它Post::create($postData)代码>
使用newInstance
方法
$post = $this->post->newInstance($postData);
return $post->save() ? $post : null;
代替return redirect()->back()->@nbin尝试了它,但仍然有相同的效果。代替return redirect()->back()->你可以尝试just back()->@nbin尝试了它,但仍然有相同的效果。谢谢!我做了#3,虽然花了一点时间才弄明白,但最终还是奏效了!谢谢我做了#3,虽然花了一点时间才弄明白,但最终还是奏效了!
@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
<?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'];
}
<?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);
}
}
<?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);
}
<?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;
}
}
}
$post = $this->post->newInstance($postData);
return $post->save() ? $post : null;