I have these columns in the table:
Status should be 0 or 1.
I want to add status_name field in my item listing. Is this possible?
$items = Item::where('type', 'test');
if(item.active == 1)
{
// add new column status_name="active"
}
else
{
// add new column status_name="inactive"
}
$items->get();
I don't want to use loop. Is there any way to do this with this query only without using loop.
In this example, I wanted to turn a VARCHAR field called curbsidePickup that either contains "true" or "false" (strings) into a TINYINT field called pickup to make it atomary. For this, you have to iterate all items before getting rid of the old field:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
class ChangeOrdersForBooleanPickup extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('orders', function (Blueprint $table) {
$table->boolean('pickup');
});
DB::table('orders')
->where('curbsidePickup', '=', 'true')
->update([
'pickup' => true
]);
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn('curbsidePickup');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('orders', function (Blueprint $table) {
$table->string('curbsidePickup');
});
DB::table('orders')
->where('pickup', '=', true)
->update([
'curbsidePickup' => 'true'
]);
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn('pickup');
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With