简体   繁体   中英

Laravel: How do I submit a form and stay in the same view?

I have in Laravel a view in which already foreach loops have been incorporated. At the bottom of the page is a small form. Now I want to send this form to save the data into the database.

At the same time I want to stay in the same view. If I enter the same page in the form, I get an error message. If I want to go back to the view via the controller, I also get an error.

In this error, the output of the data in the loop that was previously passed by another controller no longer works. - What can I do?

Undefined variable: data (View: /srv/users/smartfinance/apps/smartfinance/public/laravel/resources/views/home.blade.php)

  <?php $__currentLoopData = $data; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?> 

I hope you understand my problem and can give me a hint how to solve it.

These are the two controller:

class checkDataController extends Controller
{
    public function data()
    {
        $data = DB::table('test')->where('booking_date', '=', $today)->get();
        return view('home', compact("data"));
    }
}

class AddContractController extends Controller
{
    public function addNewData(Request $request)
    {
        $bez = $request->bez;
        return view('home');
    }
}

Your mistake is that you try to display the same view from two different controller methods. Normally, a view is only being used by one controller method. Other methods can redirect to this method in order to (re-)display the same view. This way, the logic of retrieving data for the view is only required in one place.

class CheckDataController extends Controller
{
    // route 'check.data'
    public function data()
    {
        $data = DB::table('test')->where('booking_date', '=', $today)->get();
        return view('home', compact("data"));
    }
}

class AddContractController extends Controller
{
    // route 'contract.create'
    public function addNewData(Request $request)
    {
        Contract::create($request->input()); // unsafe, only for demonstration...

        return redirect()->route('check.data');
    }
}

As you can see, instead of return view('home') , we are returning redirect()->route('check.data') . This will redirect the user to the other controller with the defined route. Of course this means you are actually executing two controller methods within one user action , but that's common practice.

More information about redirects can be found in the official documentation .

An advise for you my friend don't return a view from a post method.

I suppose the addNewData() method is a method that comes from the post route. so when you return a view after a post method you don't provide the data for your page. so laravel throws an error and complains about the missing variable. so what you must do is redirect to the route that views the page. So your method would look something like this:

public function addNewData(Request $request)
    {
        $bez = $request->bez;
        return redirect('YOUR ROUTE TO VIEW THE PAGE (URL)');
    }

Typically this is done by redirecting back to the same page you submitted from. Here is what that would look like in your controller:

public function addNewData(Request $request)
{
    $bez = $request->bez;
    return back();
}

For a better user experience, you should also add a message to the view with the form:

@if(session('status'))
    <div class="alert alert-success" role="alert">
        {{ session('status') }}
    </div>
@endif

And make one small change to your controller:

return back()->with('status', "Successfully submitted {$bez}!");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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