Showing posts with label laravel. Show all posts
Showing posts with label laravel. Show all posts

Tuesday, March 15, 2022

Can't use Jquery UI in Laravel Boilerplate (date picker)

 Solution:

Put jquery ui library file after manifest.js


Do you want to find the issue please comment all js library files except jquery.

then put this code for check UI loaded

<script>
$(document).ready(function() {
if (jQuery.ui) {
alert("loaded");
}else
{
alert("not loaded");
}
})
</script>

 then check and uncomment commented libraries one by one


Thursday, June 11, 2020

Laravel Input multi dimensional array

Hi, in my template I have:
@for($i = 0; $i < 4; $i++) 
        {{ dd(Input::old('custom_link.0.title')) }}
        <div class="form-group">
            <label for="link_title_{{ $i }}">Link Title {{ $i+1 }}</label>
            <input type="text" class="form-control" id="link_title_{{ $i }}" name="custom_link[{{ $i }}][title]" value="{{ Input::old('custom_link.0.title') }}">
        </div>
        
    @endfor
put template 
{{ Input::old('custom_link.' . $i . '.title')) }}

After vlidation on controller I return
return Redirect::route('community-create-step-4')
     ->withErrors($validator)
     ->withInput();

Wednesday, June 10, 2020

Validation rules that depends on what the request is using condition

You could use the request helper:
public function rules()
{
    $baseRules = [
       'email' => 'required|email',
       'g-recaptcha-response' => 'required|recaptcha'
    ];
    if(request()->get("valueFromPost") === '2') {    
        return $baseRules + [                
            'confirm_email' => 'required|email|same:email'
        ];

    }

    return $baseRules;
}
Or the shorter (but less readable) version:
public function rules()
{
    return [
       'email' => 'required|email',
       'g-recaptcha-response' => 'required|recaptcha'
    ] + (request()->get("valueFromPost") === '2' ? [ 'confirm_email' => 'required|email|same:email' ] : []);    
}

Thursday, February 7, 2019

Camel case model relationship is returned as snake case property

Put following code in to your model class

/**
* Indicates whether attributes are snake cased on arrays.
*
* @var bool
*/
public static $snakeAttributes = false;


Sample code as follows,

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;

class BaseModel extends Model {

/**     
* Indicates whether attributes are snake cased on arrays.     
*     
* @var bool     
*/    
public static $snakeAttributes = false;

}

Tuesday, September 18, 2018

How to remove "api" Prefix from URL

For Laravel 5.5:wn vote
accepted
It's just prefix to differ your api routes from other routes. You can add something different from apito here.
In app\Providers\RouterServiceProvider change this function:
   /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }
Remove prefixe line:
   /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }

Monday, September 10, 2018

Laravel 5.4: Specified key was too long error

Laravel 5.4 made a change to the default database character set, and it’s now utf8mb4 which includes support for storing emojis. This only affects new applications and as long as you are running MySQL v5.7.7 and higher you do not need to do anything.
For those running MariaDB or older versions of MySQL you may hit this error when trying to run migrations:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes
As outlined in the Migrations guide to fix this all you have to do is edit your AppServiceProvider.php file and inside the boot method set a default string length:
use Illuminate\Support\Facades\Schema;

public function boot()
{
    Schema::defaultStringLength(191);
}

Friday, September 7, 2018

ReflectionException' with message 'Class path.storage does not exist

after run:
 php artisan migrate

got this errors:
PHP Fatal error:  Uncaught ReflectionException: Class path.storage does not exist in C:\xampp\htdocs\lumen\iq-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:752
Stack trace:
#0 C:\xampp\htdocs\lumen\iq-api\vendor\laravel\framework\src\Illuminate\Container\Container.php(752): ReflectionClass->__construct('path.storage')
#1 C:\xampp\htdocs\lumen\iq-api\vendor\laravel\framework\src\Illuminate\Container\Container.php(631): Illuminate\Container\Container->build('path.storage')
#2 C:\xampp\htdocs\lumen\iq-api\vendor\laravel\framework\src\Illuminate\Container\Container.php(586): Illuminate\Container\Container->resolve('path.storage', Array)
#3 C:\xampp\htdocs\lumen\iq-api\vendor\laravel\lumen-framework\src\Application.php(230): Illuminate\Container\Container->make('path.storage', Array)
#4 C:\xampp\htdocs\lumen\iq-api\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php(110): Laravel\Lumen\Application->make('path.storage', Array)

#5 C:\xampp\htdocs\lumen\iq-api\vendor\laravel\framework\src\Illuminate\Fo in C:\xampp\htdocs\lumen\iq-api\vendor\laravel\framework\src\Illuminate\Container\Container.php on line 752


In my case (lumen 5.3.3) got fixed by adding following lines right after $app definition in bootstrap/app.php file:
$app = new Laravel\Lumen\Application(
        realpath(__DIR__ . '/../')
);

$app->instance('path.config', app()->basePath() . DIRECTORY_SEPARATOR . 'config');
$app->instance('path.storage', app()->basePath() . DIRECTORY_SEPARATOR . 'storage');

//$app->withFacades();
$app->withEloquent();
Summery is only add following two lines,
$app->instance('path.config', app()->basePath() . DIRECTORY_SEPARATOR . 'config');
$app->instance('path.storage', app()->basePath() . DIRECTORY_SEPARATOR . 'storage');


Sunday, February 22, 2015

Laravel 4 Custom Class

The Problem

How to add custom class on Laravel 4 Application?
In most case, we want to leverage our Laravel 4 application capabilities by adding custom class. We don’t want to insert class inside Controller and Model to do this, so we have to write our class in separate files and group it by folder

My Solution 

  • We Create folder inside app. For example app/classes
  • Register ClassLoader to app/start/global .
    ClassLoader::addDirectories(array(
    
     app_path().'/commands',
     app_path().'/controllers',
     app_path().'/models',
     app_path().'/database/seeds',
     app_path().'/classes', // we've added classes folder on Laravel ClassLoader
    
    ));
    
    As long as the folder structure within new app/classes folder follows your namespacing convention, Laravel will autoload all classes / files within this folder. 
  • Write your class
    For example, we want to add EmailNotifier which handle about email sending. We createEmailNotifier.php on app/classes/Notifier/EmailNotifier.php
    <?php namespace Notifier;
    
    class EmailNotifier {
    
        public static function notify()
        {
            return 'User Notified';
        }
    }
    

    Please note that we are using Notifier namespace as the same folder with Notifier
  • Call your class using Notifier\EmailNotifier::notify()
  • If we want to add it on alias so you can do EmailNotifier::notify(), we have add alias to app/config/app.php . Add 'EmailNotifier' => 'Notifier\EmailNotifier' at the end ofaliases array.

Another Solution

Don’t forget that we have Composer. We can always utilze Composer to load our classes. You can refer to http://laravel.com/docs/packages for package development and if you want to publish your class topackagist.