简体   繁体   English

提交后没有任何反应

[英]After submit nothing happens

I'm building a forum with Laravel. 我正在与Laravel建立论坛。 After clicking the submit button to create a new thread nothing happens, the user is not redirected and the thread doesn't appear in the database. 单击提交按钮以创建新线程后,没有任何反应,用户也没有重定向,并且该线程也没有出现在数据库中。

Here's my controller: 这是我的控制器:

public function store(ThreadValidation $rules, $request)
{
    $thread = Thread::create([
        'user_id' => auth()->id(),
        'channel_id' => request('channel_id'),
        'title' => request('title'),
        'body' => request('body'),
    ]);

    return redirect($thread->path());
}

Model: 模型:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Thread extends Model
{
    protected $guarded = [];
    protected $fillable = ['title', 'body', 'user_id', 'channel_id'];

    public function creator()
    {
        return $this->belongsTo(User::class, 'user_id');
    }

    public function replies()
    {
        return $this->hasMany(Reply::class);
    }

    public function addReply($reply)
    {
        $this->replies()->create($reply);
    }

    public function path()
    {
        return "/threads/{$this->channel->slug}/{$this->id}";
    }

    public function channel()
    {
       return $this->belongsTo(Channel::class, 'channel_id');
    }
}

Routes: 路线:

Route::get('/', function () {
        return view('welcome');
});

Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('threads', 'ThreadsController@index');
Route::get('threads/create', 'ThreadsController@create');
Route::get('threads/{channel}/{thread}', 'ThreadsController@show');
Route::post('/threads', 'ThreadsController@store');
Route::get('threads/{channel}', 'ThreadsController@index');
Route::post('threads/{channel}/{thread}/replies', 'RepliesController@store');

Blade: 刀:

<div class="container">
    <div class="row">
        <div class="col-md-8 offset-md-2">
            <div class="card card-default">
                <div class="card-header">Create a New Thread</div>
                <div class="card-body">
                  <form method="POST" action="/threads">
                    @csrf
                    <div class="form-group">
                      <label for="title">Add a title</label>
                      <input type="text" class="form-control" name="title" id="title">
                    </div>
                    <div class="form-group">
                      <label for="body"></label>
                      <textarea name="body" id="body" class="form-control" rows="8"></textarea> 
                    </div>
                    <button type="submit" class="btn btn-primary">Publish</button>
                  </form>
                </div>
            </div>
        </div>
    </div>
</div>

I would really appreciate if you could tell me what is wrong, because my test actually passes. 如果您能告诉我什么地方出了问题,我将不胜感激,因为我的考试确实通过了。

/**
 * @test
 */
public function an_authenticated_user_can_create_forum_threads()
{
    $this->withExceptionHandling()
        ->signIn();

    $thread = create('App\Models\Thread');
    $this->post('/threads', $thread->toArray());

    $this->get($thread->path())
        ->assertSee($thread->title)
        ->assertSee($thread->body);
}

Update: Form Request: 更新:表单请求:

public function rules()
{
    return [
        'title' => 'required',
        'body' => 'required',
        'channel_id' => 'required||exists:channels, id',
    ];
}

Your post request does not contain a channel_id , thus this validation will fail: 'channel_id' => request('channel_id') and that is why your request will not be handled correctly. 您的发布请求不包含channel_id ,因此此验证将失败: 'channel_id' => request('channel_id') ,这就是为什么您的请求将无法正确处理的原因。

You will have to add an input field which contains this variable, or remove it from the validation. 您将必须添加一个包含此变量的输入字段,或将其从验证中删除。 In the order requests this will work because the url sometimes contains the id, but since this url is /threads it obviously does not contain this variable. 在订单请求中,这将起作用,因为url有时包含id,但是由于此url是/threads因此显然不包含此变量。

If you can not or do not want to force this variable you can remove the validation rule and provide a default value if the request variable is not present (however I would advice against this since it is better to require this variable if you need it anywhere else since this spares you the hassle of more checks if it is set). 如果您不能或不想强制使用此变量,则可以删除验证规则,并在不存在请求变量的情况下提供默认值(但是我建议您这样做,因为如果您在任何地方都需要此变量,则最好使用此变量否则,这样可以避免您麻烦检查更多(如果已设置)。 In order to do this you can use the Null coalescing operator , like this: 为此,可以使用Null合并运算符 ,如下所示:

$thread = Thread::create(
    [
    'user_id' => auth()->id(),
    'channel_id' => request('channel_id') ?? "Default value",
    'title' => request('title'),
    'body' => request('body'),
    ]
);

I hope this helps you 我希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM