mercredi 10 novembre 2021

Not null violation: 7 ERROR: null value in column - Laravel 5.8

I don't understand this, I alter my table to add these columns

Schema::table('clusters', function($table)
{

    $table->string('ssaEnabled',1)->default('0');
    $table->string('ssaBackendUrl')->default(NULL);
    $table->string('ssaPortalApiUrl')->default(NULL);

});

in my store() I have this

$cluster->ssaEnabled            = Input::get('ssaEnabled','0');
$cluster->ssaBackendUrl         = Input::get('ssaBackendUrl','');
$cluster->ssaPortalApiUrl       = Input::get('ssaPortalApiUrl','');

$cluster->save();

I kept getting

prod.ERROR: SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "ssaBackendUrl" violates not-null constraint

Why ??

  1. I set it to default null already
  2. I also add a backup value in my Input::get() in the 2nd param as an empty string

Any hints?

How can I stop that ?



via Chebli Mohamed

How to Fetch Dynamic Id of an object and show data related to it in Laravel

So let me give you an overview of the problem I am facing. I am working on a project which has handpicked project as shown below on home page.click to see image

So now I want to show all the associated properties with the specific projects like this click to see image

I have a databse table named "properties" which has a column "project_id". I want this "project_id" to be dynamic . Right Now I am entering the project id manually i.e. 1,2,3,4.

This is the ajax I am using in my blade file

<script>
     $(document).ready(function(){
        getProperties();
        function getProperties() {
            $.ajax({
        url:'/handpickedproperties/{$id}',
        type:'POST',
        data:{'property_type':property_type},
        success:function(response){
            if(response.success){
                var str = '';
                stringlength = 135;
                if($( window ).width()<1363)
                    var stringlength = 100;
                $.each(response.data, function(k,v){
                    description = v.description;
                    if (description.length > stringlength)
                        description = description.substr(0,stringlength)+'.....more';
                    photos = JSON.parse(v.images);
                    if(jQuery.isEmptyObject(photos)){
                        image_count = 0;
                        cover_photo = '/images/default/photo not available.png';
                    }
                    else{
                        image_count = Object.keys(photos['withPhotos']).length;
                        cover_photo = photos.coverPhoto;
                    }
                    area_conversion = JSON.parse(v.area_conversions);
                    str += '<div class="property-box" data-aos="fade-up">\
                                <div class="pic-area">\
                                    <figure><img src="'+cover_photo+'">\
                                        <span class="photo-count">\
                                        <img src="images/photos-icon.png">'+image_count+'</span>\
                                    </figure>\
                                </div>\
                                <div class="listing-property-details">\
                                    <a href="/viewPropertyDetails/'+v.id+'">\
                                        <h5>'+v.name+' in '+v.address+'</a><a href="javascript: void(0)" onclick="addFavourite(`'+v.id+'`)" class="wishlist" style="margin-top:0"><img src="/images/home/heart2.png" id="pic_'+v.id+'"></a><a href="/viewPropertyDetails/'+v.id+'"></h5>\
                                        <span class="price-box">\
                                            <strong>'+v.currency+v.price+v.price_unit+'</strong>\
                                            '+v.currency+' '+v.price_per_area+'/sq.ftadsds.\
                                        </span>\
                                        <span class="area-box">\
                                            <img src="/images/listing-area.png">\
                                            <strong><span class="sq_ft" style="width:75%;display:inline-block;">'+v.area_per_sq_ft+' sq.ft.</span>\
                                            <span class="sq_mt" style="width:75%;display:none;">'+area_conversion.sq_mt+' sq.mt.</span>\
                                            <span class="sq_yrd" style="width:75%;display:none;">'+area_conversion.sq_yrd+' sq.yrd.</span></strong>\
                                            ('+v.carpet_area+') Carpet Area\
                                        </span>\
                                        <span class="bed-box">\
                                            <img src="/images/listing-bed.png">\
                                            <strong>'+v.bedroom+' BHK</strong>\
                                        </span>\
                                        <span class="bath-box">\
                                            <img src="/images/listing-bath.png">\
                                            <strong>'+v.bathroom+' Bath</strong>\
                                        </span>\
                                        <div class="clear"></div>\
                                        <p class="property-brief" style="color:black;">'+description+'</p></a>';
                    if(v.featured == 1)
                        str += '<label style="margin-right: 5px;" class="featured-highlight-buttton">Featured</label>';
                    if(v.verified == 1)
                        str += '<label style="margin-right: 5px;" class="highlight-buttton">Verified</label>';
                    if(v.construction_status == 3)                      
                        str += '<label style="margin-right: 5px;" class="highlight-buttton">Under Construction</label>';
                    if(v.construction_status == 4)
                        str += '<label style="margin-right: 5px;" class="highlight-buttton">Ready to Move</label>';
                                // <label class="highlight-buttton">Resale</label>
                    str += '</div>\
                                <div class="listing-box-bottom">\
                                    <span>\
                                        Posted on '+v.creation_date+' by '+v.created_by+'\
                                        <a href="/total-listed-properties" target="_blank"><strong>'+v.posted_by+'</strong></a>\
                                    </span>\
                                    <a href="#" onClick = "contactDetails(`'+v.id+'`);">Contact '+v.posted_by+'</a>\
                                </div>\
                            </div>';
                });
                $('#results_number').empty();
                $('#results_number').append(Object.keys(response.data).length);
                $('#property-data').append(str);
                $('#no-result-found-div').hide();
            }
            else{
                $('#property-data').empty();
                $('#results_number').empty();
                $('#results_number').append(0);
                $('#no-result-found-div').show();
            }
        }
    });
        }
     });   
</script>

enter code here

This is my controller Code

 public function handpickedProperties(Request $request,$id)
  
    {
        try
        {
            $data           = $request->all();
            $validator      = Validator::make($data, [
            ]);
            if ($validator->fails())
                return $this->sendError("Validation Failure",$validator->errors(),403);

            $price_min = 0;
            $price_max = 10000000;
            $search = PropertyServices::fetchHandpickedProperties($request->search,$request->type,$request->city,$request->property_type,$price_max,$price_min,$id);
            if(is_array($search))
            return view('site.handpicked-properties');
            else if($search == 0)
                return $this->sendError('Property not found',[],206);
            else if($search == null)
                return $this->sendError('Property not found. Due to Exception in Service',[],500);
        }
        catch(Exception $e)
        {
            Log::error('Exception encountered. PropertyController->searchProperties Exception: '.$e);
            return $this->sendError('Property not found. Due to Exception.',[],500);
        }
    }
enter code here

This is the code I have used in listing

public function fetchHandpickedProperties($search,$type,$city,$property_type,$price_max,$price_min,$id)
    {
        {
            try
            {
               
                $result = Property::where('project_id','2')
                                    ->select('description','images','id','address','name','currency','price','price_unit','price_per_area','area_per_sq_ft','area_conversions','bedroom','bathroom','created_by','posted_by',\DB::raw('DATE_FORMAT(created_at,"%M-%d-%Y") as creation_date'),'carpet_area','verified','construction_status','featured','lat','long')
                                    ->get()->toArray();
                    
                return $result;

            }
            catch(Exception $e)
            {
                Log::error('Exception encountered. PropertyServices->searchProperty Exception: '.$e);
                return $result = null;
            }
        }
    }


via Chebli Mohamed

Selecting from multiple tables in laravel

please help.I have a test booking table that looks like this

Schema::create('test_bookings', function (Blueprint $table) {
        $table->unsignedInteger('RequestID');
        $table->string('bookingDate');
        $table->string('timeSlot');
        $table->unsignedInteger('nurse_id');
        $table->timestamps();
    });

and a tests table that looks like this

Schema::create('tests', function (Blueprint $table) {
        $table->unsignedInteger('RequestID');
        $table->unsignedInteger('patientID');
        $table->string('barcode');
        $table->string('temperature');
        $table->string('pressure');
        $table->string('oxygen');
        $table->unsignedInteger('nurseID');
        $table->timestamps();
    });

I want to show the RequestID,bookingDate,timeSlot, name and surname of the nurse only if the test_bookings RequestID is in tests table. This is my nurse table

Schema::create('nurses', function (Blueprint $table) {
        $table->unsignedInteger('nurseID');
        $table->string('name');
        $table->string('surname');
        $table->string('idNumber');
        $table->string('phone');
        $table->string('email');
        $table->unsignedInteger('suburb_id');
        $table->timestamps();


        $table->index('suburb_id');
    });

This is the code that i tried

$tests =  DB::table('tests')
                ->select('RequestID','bookingDate','timeSlot','name','surname')
                ->join('nurses','nurses.nurseID','test_bookings.nurse_id')
                ->join('test_bookings','test_bookings.RequestID','=','tests.RequestID')
                ->get();


via Chebli Mohamed

Conditionally validate request fields in Laravel 5

My variable carType gets a string value.

If additionalFields variable is carType should be required when it is a truck or caravan. If carType is a truck, then additionalFields should be a array.

'carType' => 'string',
'additionalFields' => 'required_if:carType,truck,caravan',

How do i add an array validation based on condition?



via Chebli Mohamed

mardi 9 novembre 2021

issue in laravel php artisan server [closed]

I have installed a live laravel project on my localhost. when I run there are two warning errors that occur.

Anyone have idea how to fix it

Warning: The use statement with the non-compound name 'DateTime' has no effect in app\helper.php on line 22

Warning: The use statement with the non-compound name 'DateTimeZone' has no effect i
n \app\helper.php on line 23


via Chebli Mohamed

Wildcard-like syntax in an eloquent Where clause to search between two strings

I saw the answer provided in this question Wildcard-like syntax in an eloquent Where clause? now I want to know if is there a way to search between two strings?.

basicasicaly in my code I want to show requests that have a status of new or scheduled.

$requests = DB::table('requests')
        ->select('requestDate','requestTime','status','requestID')
        ->where('requestorID', '=',$userID)
        ->where('status', 'LIKE','%New%')
        ->get();


via Chebli Mohamed

lundi 8 novembre 2021

create dynamically checkbox in blade

I´m traying to create list of checkbox in my blade with all roles that i have in my DB. I´m doing loop to get all roles and if to check that roles have this user that i´m doing edit:

@foreach($roles as $rol)
   @foreach ($selRoles as $role)
     @if ($role == $rol->id)
        <div class="col-md-4">
          <input type="checkbox" name="rol" checked=checked class="form-check-input" value="" id="">
          
       </div>
     @else
      <div class="col-md-4">
         <input type="checkbox" name="rol" class="form-check-input" value="" id="">
         
      </div>                                                   
     @endif
   @endforeach
@endforeach

With this code, my problem it´s that all my roles it´s duplicated. In my controller i have this:

public function edit(User $usuario)
    {
        $roles = Bouncer::role()->orderBy('title', 'DESC')->get();
        $selRoles = $usuario->roles->pluck('id')->toArray(); //selRoles it´s roles from user
    
        $usuario->load('roles');

        return view('admin.empleados.edit', compact('usuario', 'roles','selRoles'));
    }

and retult in blade it´s:

teleoperadora
teleoperadora
teleoperadora
repartidor
repartidor
repartidor
prueba
prueba
prueba
Jefe de equipo
Jefe de equipo
Jefe de equipo
jefa-sala
jefa-sala
jefa-sala
instalador
instalador

for example. I don´t know that i´m doing bad for get this result in my blade. Anybody can help me please?

Thanks for read and help. Sorry for my english



via Chebli Mohamed