OndOne
OndOne

Reputation: 27

What's wrong with my Laravel 5.5 save() method?

I am using Laravel 5.5, I want to do basic form input where is only one field->"email". I am using Eloquent, model to interact with database for these subscriber inputs. When the controller method is called, this error follows:

FatalThrowableError (E_ERROR) Call to a member function save() on string

The thing is I am using exactly the same solution for other form I've got in my application (contact form). That's the reason why I am pretty sure, that namespacing, models or other stuff I written well.

This is my code:

SubsController.php

class SubsController extends Controller
{
    public function store(Request $request)
    {
        $subscriber = new Subscriber;
        $subscriber=$request->input('email');
        $subscriber->save();
        return redirect()->to(route('homepage'));
    }
}

Upvotes: 0

Views: 449

Answers (3)

Pedro Gomez
Pedro Gomez

Reputation: 1

One more solution is

public function store() { 
    $data = request()->validate('email');
    Subscriber::create($data);
    return redirect()->route('homepage'); 
}

Upvotes: 0

Hemraj Pal
Hemraj Pal

Reputation: 164

Here is the solution :

$subscriber = new Subscriber;  
$subscriber->email = $request->input('email');       
$subscriber->save();  
return redirect()->to(route('homepage'));

Upvotes: 0

plmrlnsnts
plmrlnsnts

Reputation: 1684

Please check this line, you just assigned a string value to your $subscriber variable

$subscriber =$request->input('email'); 

The correct way is

public function store(Request $request) { 
    $subscriber = new Subscriber;
    $subscriber->email =$request->input('email'); 
    $subscriber->save(); 
    return redirect()->to(route('homepage')); 
}

Upvotes: 4

Related Questions