Tag: laravel

  • add soft delete in laravel

    In the migration file add:

    $table->softDeletes();
    

    In the model:

    <?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use Illuminate\Database\Eloquent\SoftDeletes;
    
    
    class CalNotes extends Model
    {
        use HasFactory;
        protected $table = 'cal_notes';
        protected $fillable = ['user_id','title','description','price','ip'];
        protected $primaryKey = 'id';
        use SoftDeletes;
    
    }

    In the controller:

     $id = $request->id;
     $calNotesModel  = new CalNotes();
     $calNotes = $calNotesModel->find($id);
     $calNotes->delete();

     if you need to restore the deleted user

    $calNotes = CalNotes::withTrashed()->find($id);
            $calNotes->restore();
  • group by in MySQL example only date from datetime

    MySQL Example

    SELECT date(created_at),count(*) FROM `users` WHERE 1 group by date(created_at) ORDER BY `date(created_at)` DESC

    Laravel Example with groupByRaw

     $data = DB::connection('default')->table('users')->select(DB::raw('DATE(created_at) date'),DB::raw('count(created_at) as page_count'))->groupByRaw('DATE(created_at)')->get();

    Laravel Example with groupBy

     $data = DB::connection('default')->table('users')->select(DB::raw('DATE(created_at) created_date'),DB::raw('count(created_at) as page_count'))->groupBy('created_date')->get();
  • ignore time in datetime mysql in select in laravel 8

    They do the SQL DATE() work for you, and manage the differences of SQLite.

    Your result can be achieved as so:

    ->whereDate('date', '<=', '2014-07-10')

    Though the above method is convenient, as noted by Arth it is inefficient on large datasets, because the DATE() SQL function has to be applied on each record, thus discarding the possible index.

    Here are some ways to make the comparison (but please read notes below):

    ->where('date', '<=', '2014-07-10 23:59:59')
    
    ->where('date', '<', '2014-07-11')
    
    // '2014-07-11'
    $dayAfter = (new DateTime('2014-07-10'))->modify('+1 day')->format('Y-m-d');
    
    ->where('date', '<', $dayAfter)
    ->whereDate('date','=','2014-07-10')
    
    ->whereDate('date', '<=', '2014-07-10')
  • double data type in Laravel migration

    <?php
    
    use Illuminate\Database\Migrations\Migration;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Support\Facades\Schema;
    
    class CreateExpenseTableTable extends Migration
    {
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('expense_table', function (Blueprint $table) {
                $table->id();
                $table->integer('user_id')->nullable();
                $table->double('amount', 10, 2)->nullable();
                $table->text('description')->nullable();
                $table->string('ip',150)->nullable();
                $table->softDeletes();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::dropIfExists('expense_table');
        }
    }