Devender Gupta
Devender Gupta

Reputation: 546

Boolean not set to true

There are two tables "Stock" and "Soldstock".

Table stock contains all the stocks that have been added. Im trying to add the stocks that are sold to the SoldStock table. the stock table is shown below:

Schema::create('stocks', function (Blueprint $table) {            

  $table->increments('tag_no');   
  $table->boolean('sold')->default(0);           
  $table->integer('user_id')->unsigned();
  $table->foreign('user_id')->references('id')->on('users');
  $table->timestamps();            
});

So when i add the stocks to SoldStock table .. im trying to find that stock with that tag_no and change its sold boolean to true. But its not working! Below is the store function.

public function store(Request $request)
{

        if(Auth::check()){
            if (SoldStock::where('tag_no','=',$request->input('tag_no'))->exists()) { 
                return back()->withInput()->with('errors', 'Tag number already entered!');                
                }      

            $soldstock = SoldStock::create([
                'tag_no' => $request->input('tag_no'),                
                'user_id' => Auth::user()->id
            ]);               

            $stock = Stock::where('tag_no','=',$request->input('tag_no'))->first();            
            if($stock){
                $stock->sold=1;        
                error_log($sell->sold);                           
                return redirect()->route('soldstock.index', ['stocks'=> $soldstock->tag_no])
                ->with('success' , 'Stock added successfully');
            }                
        }        
        return back()->withInput()->with('errors', 'Error adding Stock');     
    }

I use this to find the stock:

$stock = Stock::where('tag_no','=',$request->input('tag_no'))->first();

and to change its sold boolean to true i use this:

$stock->sold=1; 

But its not working.

Upvotes: 0

Views: 61

Answers (2)

Brian Lee
Brian Lee

Reputation: 18187

You're not saving the record after setting the sold field

$stock->sold=1; 
$stock->save();

Or use update:

$stock->update(['sold', 1]);

Upvotes: 1

sebbz
sebbz

Reputation: 554

You have to save modified model.

Add $stock->save(); after sold state change.

Upvotes: 1

Related Questions