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

Tuesday, June 11, 2019

laravel Chunk without "id" column in Sqlite database

When you have to retrieve large data from a database table and want to process through it , In Laravel using Eloquent ORM or Query Builder, (Query Builder is preferred in this case for performance reasons), there is a very nice method called "Chunk" , this method requires primary key  column to be exist on the table .
From Laravel Documentation :
If you need to work with thousands of database records, consider using the chunk method. This method retrieves a small chunk of the results at a time and feeds each chunk into a Closure for processing

So, if you have a table without and Id column in it, then you cannot use this method unless you have it with another name ,if so, then you have to add this as a variable in the eloquent model :

protected $primaryKey = "theIdColumnaName";

In Sqlite databases, fortunately any table has its own id column without creating it Called "rowid" , what you need to do is to assure you have added the right property "primaryKey" to the model as given below:

protected $primaryKey = "rowid";

Then you can enjoy chunking :)

 $capsule->getConnection('sqlite')->table('users')->orderBy('rowid')->chunk(100, function ($users) {
    foreach ($users as $user) {
         echo $user->name;
        
    }
  });

Monday, August 13, 2018

Laravel Models Eloquent Dynamically change table name

Some times you need to use one model for many tables, that means you want the same model but with many tables . So the property table will be dynamically set .to achieve this all you have to do is to use a trait that do the job as shown in the following code:

 

 /* DynamicTable.php */ 
 
trait DynamicTable
{
    protected $connection = null;
    protected $table = null;

    public function bind(string $connection, string $table)
    {
        $this->setConnection($connection);
        $this->setTable($table);
    }

    public function newInstance($attributes = [], $exists = false)
    {
        // Overridden in order to allow for late table binding.

        $model = parent::newInstance($attributes, $exists);
        $model->setTable($this->table);

        return $model;
    }

}


let's say our Model is called Profile :
require_once("DynamicTable.php"); 
 
class Profile extends Illuminate\Database\Eloquent\Model {
 
 use DynamicTable;
 
 public $timestamps = false; 
   
} 
 

Now, when you need to retrieve Model you will create a new object of this model and assign table and connection if the connection is different from default:
profiel1 = new Profile;
prfoile->setTable ("profile01");
profile->setConnection("mysql");
$results = $profile->find(123);

To insert data you can do the same .
Some retrieving commands will not work like all(), but you can use where(1,1) and it will do the job.

Sunday, October 1, 2017

htmlspecialchars() expects parameter 1 to be string, array given Laravel 5.

This Error Happened in View where php trying to echo a value using the function htmlspecialchars(), and this function cannot accept an Array as its first Parameter.

Debug : if you are using Whoops for debugging your application you will see in the left panel a link to the view templates directory .

…\storage\framework\views\eec8a32e98fad0167a61a9f879d076bc29cad5a6.php 43
 
the number in red may change in your case , So open this file in any editor and find the line number 43 , you will find the echo function containing something  function as its parameter, like this :

echo e(app->Somthing()));

the e function is a laravel helper function , which apply htmlspecialchars() function to the expected string between brackets.


Now, the function inside the brackets is causing the error , Here it is the app->Something which is bringing an array as its result while htmlspecialchars () expecting a string , find this function in your view file and change it to give you a string not an array.

Saturday, April 26, 2014

php laravel 4 "You need to specify a file path to store the seed.

This Error happened because your php version is not loading the openssl extension , check the php.ini file and un-comment  this line :

;extension=php_openssl.dll


to be like this :

extension=php_openssl.dll

Wednesday, January 29, 2014

Call to undefined method Illuminate\Foundation\Providers\ArtisanServiceProvider::when()

This error showed when trying to upgrade to Laravel 4.1, Also  when trying to  update composer , the first Solution to this problem is to change all your composer.json file (assumed that you have some in workbench folder), to be compatible with new versions of laravel :

Change this line in Composer.json :

"illuminate/support": "4.1.*" 

to be Like this


 "illuminate/support": "4.*" 

Then Run :

 Composer update --no-scripts  

In the main folder that contain composer.json And also on all the main folders of your packages .

Saturday, November 23, 2013

laravel pdo load data infile only imports first row."This command is not supported in the prepared statement protocol yet"

"This command is not supported in the prepared statement protocol yet". When I tried to use "load data infile" as a raw query on a table , It failed with this error , and after playing with many parameters in the query and tried many DB methods like DB::raw,DB::statement and DB::select, but nothing change the problem still exist . finally I can run the query but only the first raw was inserted,so The problem is in the query itself and Mysql server cannot under the end of line symbol , Just adding double back slash will solve this issue.
"LOAD DATA LOCAL INFILE 'filename'

                    INTO TABLE `tablename`

                    FIELDS TERMINATED BY ','

                    LINES TERMINATED BY '\\n'

                    (col1,col2,col3)";

Sunday, June 16, 2013

Whoops! There was an error in Laravel-4 Startup

Suddenly When I tried to reach laravel 4 Documentation on my server ,a strange colorful screen came out  saying  :

"Whoops! There was an error"







This is a debug stack trace detailed page taking from Symfony Package which is used with laravel packages as  an error stack trace  display script .
(Sorry.. Laravel-4 going to be elites software not for anyone , and I think it will lose its popularity )

Ok. How to solve this issue , As I said this is a detailed information about the error you got,If you are lost in these crowded tech information then you can reach the single line error page by changing the variable debug value from True to False in the configuration file app.php.
You can find this file in app/config/ folder .
After changing the value  to false regenerate the error again and you will get a single line error telling you what is the problem.


Friday, March 22, 2013

Create a Custom Validation Rules in Laravel 3


1.Open Start.php in the application folder and add a path map to your class that you are going to use in validation

Autoloader::map(array(

 'customValidator' => __DIR__.DS.'libraries'.DS.'Cvalidator.php',

));






in this code,   the name of my custom validator class is: 'Cvalidator.php'
and reside in  the directory : application/libraries/
Now, The system will know the place of your class .
2. Also in the smae file 'start.php' register your new validation rule as follows:


Laravel\Validator::register('customrule', function($attribute, $value, $parameters)

{

 return customValidator::CheckThis($attribute, $value, $parameters);

});




3.Next you need to create your class with methodes that will handle the validation staff. but assure that you put in the right path that you map it in start.php, here an example: 

Don't Extend the Laravel Validation Class



<?php class Cvalidator{ public static

 CheckThis(){ 

 //do the validation heare then: return true or false only return true;
}