samedi 2 avril 2016

Can't specify SQLite as a database in Laravel

Whenever I run php artisan migrate, the following error is shown in the console:

[PDOException]
SQLSTATE[HY000] [14] unable to open database file

The database.sqlite file is located at database/. I'm running Windows 10, Laravel 5.2. Here are my configurations:

.env:

DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=homestead
DB_PASSWORD=secret

I have looked everywhere, but could not find what causes this error and how to resolve it.



via Chebli Mohamed

Why I can't retrieve old request values?

I am flashing request input like this:

Input::flash();

Then, in the next request, I try to get old values:

$subject_id= $request->old('subject_id');

but I get $subject_id as null So, what could be the reason?



via Chebli Mohamed

Generate PDF in Laravel 5.1

What is the best library in Laravel 5.1 for generating PDF with style and image?. I have used dompdf once. Although it works on localhost without style and image, But it does not work on live server.

Anyone please share your experience about generating PDF with style and image in Laravel 5.1 .



via Chebli Mohamed

how to add the key of an array to the array value in php ?

Hi I have an array and I have added the an id to the key of each array but I want that id to be added to the array value also.

The code which adds the keys and value to the array.

foreach ($data as $id => $name) {
            $arr[$id] = Category::where('parent_category_id', $id)->lists('id');
        }

now the array looks like this

Array
(
    [427] => Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => 277
                    [1] => 279
                    [2] => 426
                    [3] => 428
                    [4] => 429
                    [5] => 430
                    [6] => 431
                    [7] => 432
                    [8] => 433
                    [9] => 434
                )

        )

    [280] => Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => 281
                    [1] => 282
                    [2] => 435
                    [3] => 436
                    [4] => 437
                )

        )

    [283] => Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => 284
                    [1] => 285
                    [2] => 286
                )

        )

what I really want to achieve is that I want to add the key for example say the first key which is 427 to the array values so that I get all the ids. How would I be able to achieve this please assist.



via Chebli Mohamed

Returning FatalErrorException when defining roles and permissions on laravel

I have three models that are related. First I have User that belongs to a Role. On the other hand, Role has many roles. Role belongs to many permissions and Permissions belongs to many Role. I am using the AuthServiceProvider as suggested by jeffrey way of laracast. But the problem now, when I want to fetch all the permissions of a User I am having error which is, "Call to a member function getKey() on boolean". Can someone please help me on this. Please refer to the codes below.

User.php

public function role()
{
    return $this->belongsTo('App\Role');
}

public function assignRole($role)
{
    return $this->roles()->save(
        Role::whereName($role)->firstOrFail()
    );
}

public function hasRole($role)
{
    if(is_string($role)){
        return $this->role->contains('name', $role);
    }

    return !! $role->intersect($this->role);

}

Role.php

class Role extends Model
{
    public function users()
    {
        return $this->hasMany('App\User');
    }

    public function permissions()
    {
        return $this->belongsToMany('App\Permission');
    }

    public function givePermissions(Permission $permission)
    {
        return $this->permissions()->save($permission);
    }
}

Permission.php

class Permission extends Model
{
    public function roles()
    {
        return $this->belongsToMany('App\Role');
    }
}

AuthServiceProvider

public function boot(GateContract $gate)
{
    $this->registerPolicies($gate);

    //get all permissions
    foreach ($this->getPermissionTo() as $permission ) {

        $gate->define($permission->name, function($user) use ($permission){
            return $user->hasRole($permission->roles);
        });
    }
}

public function getPermissionTo()
{
    return Permission::with('roles')->get();
}

and lastly, heres the user table that has a foreign key of role_id

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('role_id')->unsigned();

        $table->string('id_no')->unique()->index();
        $table->string('u_first_name');
        $table->string('u_middle_name');
        $table->string('u_last_name');
        $table->string('email')->unique();
        $table->string('password');

        $table->rememberToken();
        $table->timestamps();
    });



via Chebli Mohamed

vendredi 1 avril 2016

where should i add specify extension for mime types in laravel??

$photo = Input::file('photo');

    if($photo) {
        $size = $photo->getSize();
        $mime = $photo->getMimeType();
        $orignalname = $photo->getClientOriginalName();
        $orignalextension = $photo->getClientOriginalExtension();


        $destination = "Documents/edifile/";
        $filename = time().str_replace(' ', '_', $orignalname);

        $photo->move($destination,$filename);

where i have add this line 'file' => 'required|max:10000|mimes:edi,jpeg' i want to upload this extension file only.?



via Chebli Mohamed

Laravel: get translation from language file containing dot

Is it possible to get translation from language file that contains (.)dot?

For example, The language directory looks like

/lang
      /en
          pagination.php
          phone.history.php

     /es
          pagination.php
          phone.history.php

phone.history.php file contains dot and want to get translation for the key 'name' from phone.history.php file.

Lang::get('phone.history.name')

It doesn't work.



via Chebli Mohamed