mercredi 20 avril 2016

How to downgrade a Laravel 5.1 Project to 4.x?

How does one downgrade a Laravel 5 project to Laravel 4? If it's not that hard, it might work better for my team. Any changes in Eloquent, Controllers, views, and user logins would be the need-to-know. The primary reason we are looking to downgrade is host PHP support. 5.3/5.4 are what we'd need, so whichever version of 4 we use would need to go that far back - I guess I'm also looking for advice on which version of 4 would be good.



via Chebli Mohamed

Laravel 5.1:Read package's config file variables

I want to access the database connection details which are included in an external package's config file.

However, this does not seem to be working for me.

dd(Config::get('vendor/package::database.connection1.database')); 
// > null

The config file is called database.php and is imported via composer in the following path: vendor/package/config/database.phpm which looks like:

return [
        'connection1' => [
            'driver' => 'mysql',
            'host' => env('CONN1_HOST','SomeIPHere'),
            'port' => env('CONN1_PORT', '3306'),
            'database' => env('CONN1_DATABASE', 'db1'),
            'username' => env('CONN1_USERNAME', 'user1'),
            'password' => env('CONN1_PASSWORD', 'pass1'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

         'connection2' => [
            'driver' => 'mysql',
            'host' => env('CONN2_HOST','SomeIPHere'),
            'port' => env('CONN2_PORT', '3306'),
            'database' => env('CONN2_DATABASE', 'db2'),
            'username' => env('CONN2_USERNAME', 'user2'),
            'password' => env('CONN2_PASSWORD', 'pass2'),
            'charset' => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],
];

What is the best way to get the value of ie connection1.database, given that the above way did not work? Is publishing it via php artisan vendor:publish essential?



via Chebli Mohamed

How to Apply CSS on HTML to Excel export

I am trying to export HTML to Excel format using php headers, but CSS styling is not applying on elements (while export to excel file), I also try to implement Bootstrap Classes, but no luck

Note: while applying bootstrap classes, I included the bootstrap.css in my html

Is there any way available to apply CSS?

Headers using

    header('Content-type: application/excel');        
    header("Content-Disposition: attachment; filename=\"$filename\"");

Sample HTML elements

<table width="100%" border="1" class="table table-striped table-bordered">
    <thead>
        <tr>
            <td colspan="9" style="font-weight: bold; font-size: 16px; text-align: center;"><h2>Push Notification </h2></td>
        </tr>
        <tr><td colspan="9"></td></tr>
    </thead>

    <tbody>
        <tr class="heading">
            <th colspan="2" ></th>
            <th style="width:2% !important">S.no</th>
            <th style="width:10% !important">Date</th>
            <th style="width:10% !important">App</th>
            <th style="width:20%">Category</th>
            <th colspan="2" style="width:50%">Message</th>
        </tr>

        @if($push_notifications->count() > 0)
            <?php $counter = 1; ?>
            @foreach($push_notifications as $notification)
                <tr @if(($counter %2) ==1) bcolor="#c9c9c9" @endif>
                    <td colspan="2"></td>
                    <td style="width:2% !important">{!!$counter!!}</td>
                    <td style="width:10% !important">{!!$notification->date!!}</td>
                    <td style="width:10% !important">{!!$notification->notificationAppType->name!!}</td>
                    <td style="width:20%">{!!$notification->notificationCategory->name!!}</td>
                    <td colspan="2" style="width:50%">{!!htmlentities($notification->message, ENT_QUOTES)!!}</td>
                </tr>
                <?php $counter++; ?>
            @endforeach

        @else
                <tr>
                    <td colspan="9">No record available</td>
                </tr>
        @endif
    </tbody>
</table>



via Chebli Mohamed

How to return response in laravel from trait?

I have controller:

class UserController extends Controller
{
    public function index(){
        return '1';
    }
}

now I want return code from trait like:

class UserController extends Controller
{

    use SomeTrait;
    public function index(){
        $this->traitMethod();

        return 2;
    }
}

trait SomeTrait
{
    public function traitMethod(){
        if($this->something == 1){
            return '1';
        }else{
            View::share('somethingElse', 2);
        }
    }
}

In UserController if $something = 1, trait should return 1 and rest of UserController should't be executed, how can I achieve this?



via Chebli Mohamed

pagination on rest api in laravel 5.1 with in request url

I have my URL as api/listing/stores?category=category_name for this store list under category my code using :

$store_list=DB::table('s_local_store_category')->where('s_local_store_category.cat_id',$business_categories_id) 
        ->join('s_loacalstors','s_local_store_category.store_id','=','s_loacalstors.localstoreid')
        ->select('s_loacalstors.*')
        ->orderBy('name','ASC')
        ->Paginate(1);

my output in json :

{ "store": {"total":2, "per_page":1, "current_page":1, "last_page":2, "next_page_url":"http://in10km:8000/api/listing/stores?page=2", "prev_page_url":null, "from":1, "to":1, "data": [{"localstoreid":3,"name":"cuddalore store","slug":"cuddalore-store","shortdescription":"","description":"","addressline1":"100,police line","addressline2":"center city","city":"cuddalore","state":"tamilnadu","cuntry":"india","zipcode":"607001","url":"http://in10km.com","latitude":"11.744699","longitude":"79.76802429999998","ownername":"sathish kumar","ownercontact":"95436161262","metatitle":"cuddalore","metakey":"cuddalore","metadesc":"cuddlore","verified":0,"status":1,"deletestatus":0,"created_at":"2016-04-19 14:29:03","updated_at":"2016-04-19 14:29:03"}] } }

i need output like this in my json code next_page_url should be like api/listing/stores?category=category_name&page=2

i would get with url request also with paginate page value



via Chebli Mohamed

Alias Column in Laravel Scope

Here is my Model Scope

public function scopeUser($query, $user_id)
{
    return $query->where('user_id', $user_id)
                ->select(['_id', 'company_name', 'location', 'template_id', 'current_theme_id', 'site_url', 'site_path', 'addons']);
}

What i want is to us theme_id as alias in place of current_theme_id

Tried already searching for solution but all of theme were based on DB queries.

Thanks



via Chebli Mohamed

Use the timezone set by the user

Basically when I get the datetime column in my table I want to adjust its date and time base on the user timezone.

I have this code but it doesn't seem to work, the time never change even though I already change its timezone:

public function getCreatedAtAttribute($datetime) {
    $timezone = \Auth::user()->timezone;

    $datetime = $this->asDateTime($value);

    return $datetime->timezone($timezone);
}

Please help.



via Chebli Mohamed