php - 在 Laravel 中提交表单后如何重定向到另一个页面

标签 php laravel

表格

当我提交表单时,它会重定向回表单本身,有人可以帮助我吗?

    <form action="/jisajili" method="POST">
        @csrf
        <div class="card-panel z-depth-5">
            <h5 class="center red-text">Jiunge Nasi</h5>

            <div class="input-field">
                <i class="material-icons prefix">account_circle</i>
                <input type="text" name="username" class="validate">
                <label>Jina lako</label>
            </div>

            <div class="input-field">
                <i class="material-icons prefix">phone</i>
                <input type="number" name="phone" class="validate">
                <label>Nambari ya simu</label>
            </div>

             ....

            </p>
            <input type="submit" name="submit" value="Jiunge" class="btn left col s12 red">

Controller

class registration extends Controller{

     public function create(){
         return view('jisajili.jiunge');
     }

     public function store(Request $request){
         $reg = new regist;

         $reg->jina = $request->input('username');
         $reg->simuNumber = $request->input('phone');
         $reg->email = $request-> input('email');
         $reg -> password = bcrypt($request->input('password'));
         $cpassword = $request -> input('cpassword');

         $reg->save();

         $validated = $request->validate([
             'name' => 'required|unique:posts|max:10',
             'body' => 'required',
         ]);
         
         return redirect('home');
         
     }
}

最佳答案

我要做的就是在将对象添加到数据库之前首先检查数据要求。我也愿意 add the columns of the models into the Model file使用带有数组参数的 Object::create 函数。

我建议在 Blade 文件中使用路由。我注意到您使用了 action="/route"。您想要做的是在路线文件中使用 ->name('route_name') 命名您的路线。要在 Blade 文件中使用它们,请使用全局路由函数route="{{route('route_name') }}"

<?php

class PostController extends Controller
{
    public function index()
    {
        return view('post.create');
    }

    public function store(\Illuminate\Http\Request $request)
    {
        $validator = Validator::make(
            $request->all(),
            [
                'name' => 'required|unique:posts|max:10',
                'body' => 'required'
            ]
        );

        // Go back with errors when errors found
        if ($validator->fails()) {
            redirect()->back()->with($validator);
        }

        Post::create(
            [
                'name' => $request->get('name'),
                'body' => $request->get('body')
            ]
        );

        return redirect()
            ->to(route('home'))
            ->with('message', 'The post has been added successfully!');
    }
}

在此之后您可以做的是将自定义错误添加到 Controller 中或将它们添加到您的 Blade 文件中。您可以在the documentation of Laravel中找到更多相关信息。 .

关于php - 在 Laravel 中提交表单后如何重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71457828/

相关文章:

php - 表单中的选项值以在数据库中搜索两个值

javascript - 如何使用 SmartyStreets 将 AJAX POST 发送到 PHP 脚本以进行地址验证?

javascript - 如何在 Javascript 中添加单选按钮?

php - 为什么值没有保存到数据库中?

sql - 如何在 Laravel 中获取保存查询?

php - 通过字符串调用 Laravel 模型

javascript - 我如何将 iCheck Checkbox 值传递给 Laravel?

php - 为什么在 PHP 5 中是静态的...示例

php - 更新 Laravel Eloquent 模型两次

php - 在 Laravel 中,我需要什么验证规则来检查数字中的字符数?