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

I am trying to allow a user to update their information after they have submitted a form, but did not check a certain box. Everything is within the same page and I am controlling the different modals by returning a message, which triggers a script to open the different modals.

For some reason, I can't seem to pass the ID or email through to the next step. Can anyone help with this?

Whenever, I try, I get the following error:

Undefined variable: leads

Any idea?

Thanks!!!

Files:

  • 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'];
}

Thanks for all of your help and your questions! You helped push me in the right direction.

Here is how I solved it:

I had to correct myself in how I pushed the $email through to the view:

LeadsController

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

Notice how I'm sending the email through as 'email' here.

Then, I pushed the email through the view in the 2nd form like this:

index

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

Then, finally, in order to capture the email again, use it to find the lead that I wanted, I had to drastically change the update:

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

Thanks again!