猿问

Laravel:无法访问从 controller@update 发送的视图中的变量

我找不到原因......但我无法获得我的 var 和任何其他. 函数实际上保存数据并验证正常,然后也将新数据发送回编辑视图,但我无法访问我的 $success 变量...


CourseController@update:


class CourseController extends Controller {


    public function update( Request $request, Course $course ) {

        // Validates data or return to view with errors

        $request->validate( Course::$validation_rules );


        // Pass: Gets the record from db or returns 404

        $course = Course::findOrFail( $course->id );


        // Update the course

        $course->name = Str::ucfirst( $request->name );

        $course->description = Str::ucfirst( $request->description );


        // Save the data

        $course->save();


        // Redirect to view with success

        return redirect()->route( 'courses.edit', $course )

            ->with( 'success', 'Cambios guardados.' ) // problem here? where?

        ;

    }

}

Course (model):


class Course extends Model {


    public static $validation_rules = [

        'name' => 'required',

        'description' => 'required'

    ];

}

edit.blade.php (portion):


{{-- some code before --}}


    @if ( $errors->any() )

        @foreach ( $errors->all() as $error )

            <div class="alert alert-danger alert-dismissible fade show" role="alert">

                <strong class="d-block"> {{ $error }} </strong>


                <button type="button" class="close" data-dismiss="alert" aria-label="Close">

                    <span aria-hidden="true">&times;</span>

                </button>

            </div>

        @endforeach

    @elseif ( $success ?? false )

        <div class="alert alert-success alert-dismissible fade show" role="alert">

            <strong>{{ $success }}</strong>

            <button type="button" class="close" data-dismiss="alert" aria-label="Close">

            <span aria-hidden="true">&times;</span>

            </button>

        </div>

    @endif


{{-- some code after --}}

这就是我目前所拥有的。这非常奇怪,我总是从中得到 null$success以及我尝试的任何其他......


繁星点点滴滴
浏览 98回答 1
1回答

慕桂英4014372

当您使用重定向时,您的$success变量会在会话中闪烁with。在您应该使用的视图文件中@elseif (session('success'))
随时随地看视频慕课网APP
我要回答