1

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();
}
Faizuddin Mohammed
  • 4,118
  • 5
  • 27
  • 51

2 Answers2

2

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]);
Goper Leo Zosa
  • 1,185
  • 3
  • 15
  • 33
0

I simply had to add

$branch = new branch;
Faizuddin Mohammed
  • 4,118
  • 5
  • 27
  • 51