通过返回重定向将数据传递回更新请求

时间:2018-07-22 19:46:46

标签: php laravel laravel-blade laravel-5.6

我正试图允许用户在提交表单后更新其信息,但未选中某个框。一切都在同一个页面中,我通过返回一条消息来控制不同的模式,该消息触发了脚本以打开不同的模式。

由于某种原因,我似乎无法将ID或电子邮件传递给下一步。有人可以帮忙吗?

每当尝试尝试时,都会出现以下错误:

  

未定义变量:线索

有什么主意吗?

谢谢!

文件:

  • web.php
  • index.blade.php
  • LeadsController.php
  • Leads.php

Web.php

Route::post('/', [
    'uses' => 'LeadsController@store',
    'as' => 'leads.store'
]);
Route::patch('/{email}', [
    'uses' => 'LeadsController@update',
    'as' => 'leads.update'
]);

Index.blade.php

<html>
<div id="contact" class="modal fade">
    <div class="modal-dialog modal-content modal-lg">
        <div class="modal-body">
            <form id="form" class="form" action="/" method="post" accept-charset="utf-8">
                {{ csrf_field() }}
                <input type="email" name="email" value="{{ old('email') }}">
                <input type="checkbox" name="newsletter">
                <button type="submit">Submit</button>
            </form>
        </div>
    </div>
</div>

@if(session()->has('message'))
    <div id="sign_up" class="modal fade">
        <div class="modal-dialog modal-content modal-lg">
            <div class="modal-body">
                <form method="post" action="{{ route('leads.update', $leads->email) }}">
                     {{ csrf_field() }}
                     {{ method_field('PATCH') }}
                     <input type="checkbox" name="newsletter">
                     <button type="submit">Submit</button>
                </form>
            </div>
        </div>
    </div>
@endif

</body>

</html>

LeadsController.php

public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'email'        => 'required|email',
    ]);
    if ($validator->fails()) {
        return redirect()->back()->withErrors($validator)->withInput($request->all);
    } else {
        try {
            $leads = new Leads;
            $leads->email        = $request->email;
            $leads->newsletter   = $request->newsletter;
            $leads->save();
            if($request->newsletter == ''){
                return redirect()->back()->with('message','sign up')->withInput($request->all)->with($leads->email, $request->get('email'));
            }
            if($request->newsletter == 'true'){
                return redirect()->back()->with('success','success');
            }
        } catch (Exception $e) {
            return response()->json(
                [
                    'status' => false,
                    'error' => base64_encode($e->getMessage()),
                ],
                Status::HTTP_INTERNAL_SERVER_ERROR
            );
        }
    }
}

public function update($email)
{
    $leads = Leads::find($email);
    $leads->newsletter = $input('newsletter');
    $leads->save();
    return redirect()->back()->with('success','success');
}

Leads.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Leads extends Model
{
    protected $table = 'my_renamed_table';
    public $timestamps = false;
    protected $fillable = ['email', 'newsletter'];
}

1 个答案:

答案 0 :(得分:0)

感谢您的所有帮助和疑问!您帮助将我推向正确的方向。

这是我解决的方法:

我不得不纠正自己如何将$ email推送到视图:

LeadsController

return redirect()
    ->back()
    ->with('message','sign up')
    ->withInput($request->all)
    ->with('email', $request->get('email'));

在此处通知我如何通过“电子邮件”发送电子邮件。

然后,我以第二种形式通过视图推送电子邮件:

索引

<form method="post" action="{{ route('leads.update', session('email')) }}">

然后,最后,为了再次捕获电子邮件,使用它来找到我想要的潜在客户,我不得不彻底更改更新:

public function update($email)
{
    DB::table('my_renamed_table')
        ->where('email', $email)
        ->update(['newsletter' => Input::get('newsletter')]);
    return redirect()->back()->with('success','success');
}

再次感谢!