Faizuddin Mohammed
Faizuddin Mohammed

Reputation: 4328

How to solve this error: "Creating default object from empty value"

I have the following code in one of my controllers:

public function branch(Request $request){
    $name = $request->input('name');
    $code = $request->input('code');

    $branch->name = $name;
    $branch->code = $code;

    $branch->save();
}

And when I run the code, I have this error:

Creating default object from empty value

Please help! Am a beginner. Laravel 5.


EDIT

Here's my whole code:

use App\branch;
class AddController extends Controller {

public function branch(Request $request){

    $branch->name = $request->input('name');
    $branch->code = $request->input('code');

    $branch->save();
}

Upvotes: 1

Views: 6790

Answers (2)

Goper Leo Zosa
Goper Leo Zosa

Reputation: 1283

You need to add this:

$branch = new branch();

if you wish to add a new record.

And if you want to update rows you need to find the data first.

$branch = Branch::find(primaryId);

or if you have no primary key us this

$branch = Branch::where(['columnName' => value]);

Upvotes: 2

Faizuddin Mohammed
Faizuddin Mohammed

Reputation: 4328

I simply had to add

$branch = new branch;

Upvotes: 0

Related Questions