提交后没有任何反应

After submit nothing happens

我正在与 Laravel 建立一个论坛。单击提交按钮创建新线程后没有任何反应,用户未被重定向且线程未出现在数据库中。

这是我的控制器:

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());
}

型号:

<?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');
    }
}

路线:

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>

如果你能告诉我哪里出了问题,我将不胜感激,因为我的测试确实通过了。

/**
 * @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);
}

更新:表格请求:

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

您的 post 请求不包含 channel_id,因此此验证将失败:'channel_id' => request('channel_id') 这就是您的请求无法正确处理的原因。

您必须添加一个包含此变量的输入字段,或将其从验证中删除。在订单请求中,这将起作用,因为 url 有时包含 id,但由于此 url 是 /threads,它显然不包含此变量。

如果你不能或不想强制这个变量,你可以删除验证规则并在请求变量不存在时提供默认值(但是我建议不要这样做,因为最好要求这个变量如果您在其他任何地方都需要它,因为如果已设置,则可以省去更多检查的麻烦)。为此,您可以使用 Null coalescing operator,如下所示:

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

希望对你有帮助