Passing Data through Return Redirect Back to Update Request

Posted 9 months ago by almost_pitt

Hey guys,

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' => '[email protected]',
    'as' => 'leads.store'
]);
Route::patch('/{email}', [
    'uses' => '[email protected]',
    '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'];
}

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.