search

Home  >  Q&A  >  body text

How does laravel return a success message?

If the save fails, this is the code

1

<code>return redirect()->back()->withInput()->withErrors('保存失败!');</code>

If the save is successful, how to return if there is a template?

1

<code>return redirect('/模版)->??????('保存成功');</code>

Or a prompt of success appears in the template

1

<code>return redirect('/模版)->with('保存成功',$ok);  //总不能一直显示。。</code>

================================================ ===============================

router:

1

2

3

4

5

6

7

<code>  

    Route::get('/config', 'ConfigsController@index');

 

    Route::post('config/add', [

        'as' => 'add',

        'uses' => 'ConfigsController@add'

    ]);</code>

Controller

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

<code><?php namespace App\Http\Controllers\Admin;

 

use App\Http\Requests;

use App\Http\Controllers\Controller;

 

use Illuminate\Http\Request;

use App\Configs;

 

use Input;

use Illuminate\Http\Response;

 

 

class ConfigsController extends Controller {

 

 

    /**

     * 显示配置页面,从数据库ID=1获取

     */

 

    public function index()

    {

        $config_view = Configs::find(1);

        return view('admin.write_config')->with('config_db',$config_view);

    }

 

    /**

     * 提交更新数据,表单验证

     */

 

    public function add(Request $request)

    {

 

 

 

        $this->validate($request, [

                'web_name' => 'required',

                'web_add' =>'required',

                'web_email' => 'required|email',

                'web_copy' => 'required',

                'web_keywords' => 'required',

                'web_description' => 'required',

            ]

        );

 

 

        $configs = Configs::find(1);

 

        $configs->web_name = Input::get('web_name');

        $configs->web_add = Input::get('web_add');

        $configs->web_email = Input::get('web_email');

        $configs->web_copy = Input::get('web_copy');

        $configs->web_keywords = Input::get('web_keywords');

        $configs->web_description = Input::get('web_description');

 

 

 

        //判断,如果通过验证,那么第一步跳转到config页面,并且显示成功

        if ($configs->save()) {

            return redirect('/admin/config')->with('status', 'Update Success! 成功! :)');

        } else {

            return redirect()->back()->withInput()->withErrors('保存失败!');

        }

 

    }

 

}

</code>

Template page

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

<code>@extends('admin.admin')

 

@section('content')

 

    <p class="home_con">

        <h3>基本设置</h3>

 

 

        <form method="post" action="/admin/config/add" class="forms">

 

 

       

           //对中文显示无效

  

            @if (session('status'))

                  

                <p class="tools-alert tools-alert-green">

                    {{ session('status') }}

                </p>

            @endif

 

 

 

 

 

 

 

     

 

 

            @if (count($errors) > 0)

                <p class="tools-alert tools-alert-red">

                    <strong>错误</strong>你填写数据有问题!请重新填写!<br><br>

                    <ul>

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

                            <li>{{ $error }}</li>

                        @endforeach

                    </ul>

                </p>

 

            @endif

 

 

 

 

            <label>

                网站名称

                <input type="text" name="web_name" class="width-50" value="{{$config_db->web_name}}" />

            </label>

 

            <label>

                网站地址

                <input type="text" name="web_add" class="width-50"  value="{{$config_db->web_add}}" />

            </label>

 

            <label>

                管理邮箱

                <input type="text" name="web_email" class="width-50"  value="{{$config_db->web_email}}" />

            </label>

            <label>

                版权信息

                <input type="text" name="web_copy" class="width-50"  value="{{$config_db->web_copy}}" />

            </label>

 

            <br>

            <h3>SEO管理</h3>

            <label>

                网站关键词

                <input type="text" name="web_keywords" class="width-50"  value="{{$config_db->web_keywords}}" />

            </label>

 

            <label>

                网站描述

                <input type="text" name="web_description" class="width-50"  value="{{$config_db->web_description}}" />

            </label>

 

            <input type="submit" class="btn btn-blue" value="更新" />

            <input type="hidden" name="_token" value="{{ csrf_token() }}">

 

 

 

 

        </form>

 

 

 

 

 

 

 

 

 

    </p>

 

@endsection</code>

ringa_leeringa_lee2872 days ago606

reply all(1)I'll reply

  • 某草草

    某草草2017-05-16 16:57:22

    First of all, my personal understandingredirect()并不是redirect('/模板'),而是redirect('/routePath'), if you want to use it like this, for example, implement the following jump:

    1

    <code>return redirect('success')->with('status', 'Profile updated!');</code>

    The general process is like this, first you have to register your route in routes.php, for example:

    1

    2

    <code>Route::get('/success','UsersController@storeSuccess');

    </code>

    Then the UsersControllerstoreSuccess method usually has the following statement:

    1

    <code>return view('users.success');</code>

    Finally, you can use a statement similar to the following in success.blade.php to get the jump information:

    1

    2

    3

    4

    5

    <code>@if (session('status'))

        <p class="alert alert-success">

            {{ session('status') }}

        </p>

    @endif</code>

    The status就是最开始redirect()->with()中的status,理解为key。用session取这个key here is fine

    Finally, if you want to learn laravel, you can check out the community I just launched. There are a series of articles and tutorials here, and I will try to record videos later:

    Link---》Laravist

    Happy Hacking

    reply
    0
  • Cancelreply