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();
}
13
  • What is $branch supposed to be, do you mean to create a specific kind of object there? Commented Mar 28, 2015 at 12:55
  • It's an Eloquent modal... Commented Mar 28, 2015 at 12:56
  • I'm not familiar with laravel but that sounds extremely unlikely. Do you perhaps mean something like $this->branch? Commented Mar 28, 2015 at 12:58
  • No... branch is an eloquent model... It's included like this use App\branch Commented Mar 28, 2015 at 13:05
  • If it were, you wouldn't be receiving that error message... Commented Mar 28, 2015 at 13:09

2 Answers 2

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]);
Sign up to request clarification or add additional context in comments.

Comments

0

I simply had to add

$branch = new branch;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.