Laravel withErrors 5.4.36在视图内为空

时间:2018-09-25 10:22:47

标签: php laravel

奇怪的问题在这里!我在控制器内的返回代码如下:

return back()->withErrors([ 'not_updated' => 'Unable to update record or no changes made' ]);

然后显示刀片内的错误:

@if ($errors->any())
    <article class="message is-danger">
        <div class="message-body">
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{!! $error !!}</li>
                @endforeach
            </ul>
        </div>
    </article>
@endif

但是这似乎根本不起作用,$errors由于某种原因为空,但是在另一个控制器上可以正常工作!

这是此方法起作用的方法,我包括了 use 类。

namespace App\Http\Controllers;

use App\Pages;
use App\PlannerStatus;
use App\SubPages;
use App\VehicleMake;
use App\Website;
use App\WebsiteRedirects;
use Illuminate\Http\Request;
use Redirect;

class RedirectsController extends Controller
{

    public function store(Request $request, Website $website)
    {

        $error = [ 'test' => 'test error' ];

        if (!empty($error)) {
            return back()->withErrors($error)->withInput();
        }


        return back();

    }
}

这是起作用的控制器,您可以看到它们或多或少都是相同的!

namespace App\Http\Controllers;

use App\ResultsText;
use App\VehicleMake;
use App\VehicleModel;
use App\VehicleType;
use App\SearchPath;
use App\Website;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\MessageBag;
use Redirect;

class ResultsTextController extends Controller
{

    public function update(Website $website, ResultsText $resultsText, Request $request)
    {

        $data = request()->except(['_token','id']);

        $result = ResultsText::where('id', $resultsText->id)->update($data);

        if (!$result) {
            return back()->withErrors([ 'not_updated' => 'Unable to update record or no changes made' ]);
        }

        return Redirect::action('ResultsTextController@index', $website);

    }


}

这也是我的路线,只是为了让您看到它们完全相同:

Route::prefix('/redirects')->group(function () {
    Route::get('/', 'RedirectsController@index')->middleware('SettingStatus:redirect');
    Route::patch('/update', 'RedirectsController@update');
});

Route::prefix('/results-text')->group(function () {
    Route::post('{resultsText}/update', 'ResultsTextController@update');
});

2 个答案:

答案 0 :(得分:0)

在刀片内部尝试

@if ($errors->any())
<article class="message is-danger">
    <div class="message-body">
        <ul>
            @foreach ($errors as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
</article>
@endif

答案 1 :(得分:0)

在您的问题中很容易忽略它。问题不在于表面。您可能会觉得很有趣,为什么它不起作用。

替换

return back()->withErrors([ 'not_updated' => 'Unable to update record or no changes made' ]);

使用

return redirect()->back()->withErrors([ 'not_updated' => 'Unable to update record or no changes made' ]);