Reputation: 179
i have created a resource controller using laravel with post and get method:
class MyController extends Controller{
//get method when page is visited
public function index()
{
$data=array(
'title'=>'Standard Stock',
'standards'=>DB::table('standards')
->join('ps_product_lang','ps_product_lang.id_product','=','standards.id_product')
->select('standards.*','ps_product_lang.name')
->get()
);
return view('pages.standards')->with($data);
}
//post method to update value
public function update(Request $request, $id)
{
$this->validate($request,[
'quantity'=>'required'
]);
$standard=Standard::find($id);
$standard->quantity=$request->input('quantity');
$standard->save();
return redirect('/standards')->with('success','Quantity Updated');
}
}
it works fine by now, but the problem is i have 2000 product in my ps_product_lang database, i want to set the all the $standard->quantity
to 15 for each of them.
I used to update the quantity value via the update()
function in the controller.
But now I want to set an initial value as 15 to all the $standard->quantity
and i am definitely not gonna click 2000 time form button to submit the post requests.
My question is: is there any convenient way to set all $standard->quantity
value to 15? without touching other code?
Upvotes: 0
Views: 7184
Reputation: 165
Yes. You can. You have to edit your create_table_file on "database/migrations" folder.
So just give a default value for the column as '15'.
for example:
Schema::create('platforms', function (Blueprint $table) {
$table->increments('platform_id');
$table->string('quantity')->default(15);
$table->string('price')->default('');
$table->timestamps();
});
Once you done, you have to rollback your table.
This will remove all of your data, so take a backup of your table first.
php artisan migrate:rollback
php artisan migrate
Upvotes: 1
Reputation: 3943
There are several options.
1) SQL Update
You can update the database with a SQL query and set all rows to quantity = 15
UPDATE ps_product_lang SET quantity = 15
2) Laravel Jobs
You could create a job for doing this.
See here in the docs: https://laravel.com/docs/5.6/queues#creating-jobs
3) Migrations
You can use a migration for this and set a default value for this column.
Schema::table('ps_product_lang ', function (Blueprint $table) {
$table->string('quantity')->default('15')->change();
});
See migration details on the docs: https://laravel.com/docs/5.6/migrations#modifying-columns
Upvotes: 2