lundi 30 janvier 2017

Laravel 5.1 How to build forgot password functionality

I am using laravel 5.1 and I do not know how I should built in the functionality that a user can reset his / her password.

Typing the mail address and getting a route to reset and submit a new password.

I could not find a good tutorial for this purpose so I ask here.



via Chebli Mohamed

dimanche 29 janvier 2017

Laravel Collection In One To Many Relationships

I have a query like this(A one to many relationships)

Camp is related to Campaign

$camp =Camp::where('campaign_id', $cid)->where('weight','!=',0)->lists('weight','id')->toArray()

That would result long sql queries

 select `weight`, `id` from `camp` where `campaign_id` = '15' and `weight` != '0'


select `weight`, `id` from `camp` where `campaign_id` = '25' and `weight` != '0'


select `weight`, `id` from `camp` where `campaign_id` = '48' and `weight` != '0'

select weight, id from campaign_creatives where campaign_id = '20' and weight != '0'

select `weight`, `id` from `campaign_creatives` where `campaign_id` = '38' and `weight` != '0'


select `weight`, `id` from `campaign_creatives` where `campaign_id` = '23' and `weight` != '0'

How to make this in one query with multiple campaign_id?



via Chebli Mohamed

samedi 28 janvier 2017

Triggering a function after ajax call has finished loading images in Laravel

I've got an image gallery on my page, which works a lot like pinterest.

$images = Images::paginate(5);

On my image page, I start off with 5 images.

Controller:

if($request->ajax()) {
    return [
        'images' => view('browse.partials.imagePartials')->with(compact('images'))->render(), 'next_page' => $images->nextPageUrl()
    ];
}           

return view('browse.combined_results')->with('images', $images);

Markup:

<div id="freewall" class="freewall endless-pagination" data-next-page=>
@foreach ($images as $image)
<a href="">
    <div class='brick featuredTextContainer' style="width:150px">
        <img src='' width='100%' class="profileImg">
        <span class="featuredArtistName"></span>             
    </div>
</a>
@endforeach
</div>

These images are arranged using freewall:

var wall = new Freewall("#freewall");
wall.reset({
    selector: '.brick',
    animate: true,
    cellW: 150,
    cellH: 'auto',
    onResize: function() {
        wall.fitWidth();
    }
});

var images = wall.container.find('.brick');
images.find('img').on('load', function() {
    wall.fitWidth();
});

wall.fitWidth();    

wall.fitWidth() is the method that arranges the images all neatly, like so:

enter image description here

However, if you scroll down, you get an ajax request

$(window).scroll(fetchImages);

function fetchImages() {
    var page = $('.endless-pagination').data('next-page');

    if(page !== null) {

        clearTimeout( $.data( this, "scrollCheck" ) );

        $.data( this, "scrollCheck", setTimeout(function() {
            var scroll_position_for_images_load = $(window).height() + $(window).scrollTop() + 100;

            if(scroll_position_for_images_load >= $(document).height()) {
                $.get(page, function(data){
                    $('.endless-pagination').append(data.images);
                    $('.endless-pagination').data('next-page', data.next_page);
                });

            }
        }, 350))

    }
}

The ajax request loads a partial, which is essentially just the next page of the pagination.

imagePartial.blade.php

@foreach ($images as $image)
        <a href="">
            <div class='brick featuredTextContainer' style="width:150px">
                <img src='' width='100%' class="profileImg">
                <span class="featuredArtistName"></span>             
            </div>
        </a>
@endforeach

In other words, it loads in more images from the $images pagination into the images container.

Which is great. It works. However, the images aren't arranged right away. You have to execute wall.fitWidth() once more for the images to fit into place. The problem is, I can't simply put this at the end of the fetchImages function, because the images take a moment to load in. Therefore, all the images are loaded behind the other images, unarranged.

A solution to this would be to, say, have the new images loaded in with opacity set to 0, have the wall.fitWidth() function executed after a check to make sure all the images have been loaded into the DOM, THEN set the images back to opacity: 1. But I'm not sure how to do this.

Help?



via Chebli Mohamed

vendredi 27 janvier 2017

Apply math operations over column of table

This question is about finding approaches to solve a specific problem.

I have a table which has several metrics with values.

[metric1] [metric2] [metric3] [metric...N]

Those columns are fixed.

I need the user to be able to generate calculated metrics using the existing columns.

Let's say that a user made a custom metric which looks like:

[custom_metric] = ([metric1] + [metric3])*[metric2]

Therefore the user would be able to select a report using this custom metrics and other metrics as well.

I have two options. One is to manage this in the database which is a PostgreSQL. Other one is to split the math formula into pieces and store it which I think is the way to go. For PHP I'm using Laravel 5.1. The problem is how to store the formula in a way that could be easily applied when I'm generating the report.



via Chebli Mohamed

jeudi 26 janvier 2017

Laravel 5.1 - Skip Route with Route Model Binding

Say I've got this set in RouteServiceProvider::boot():

$router->bind('user', function ($value) {
    return \App\Models\User::where('name', str_slug($value))->first();
});

And I have this route:

Route::get('/{user}', ['as' => 'profile', 'uses' => 'ProfileController@getProfile']);

But the unfortunate structure of the website means that the routing rules that would check this are at the same level of other rules which are inserted statically. If a model doesn't exist, it would be ideal for it to skip the routing rule in question.

I've tried shifting the route that needs this change behind every other route, but alas, the problem persists. It tries to match this route no matter what.

What do I need to do to resolve this? I cannot change the structure of the whole web app, but I still need dynamic routing at this path level.



via Chebli Mohamed

Laravel 5.1 - Filtering on Collections

I have the following tables.

 basket_items:

 id  |  basket_id | product_id
 -----------------------------
 1   |  4         |   1
 2   |  4         |   2
 3   |  4         |   1
 4   |  4         |   1
 5   |  4         |   1


 prepaid_credits:

 id  |  order_item_id  |  product_id  | quantity
 ----------------------------------------------
 1   |  2              |   1          |  1
 1   |  3              |   1          |  2

I get a list of prepaid credits as follows which tells me user 1 has 3 credit available for product_id 1

  $prepaidCredits = DB::table('orders')
                   ->join('order_items', 'orders.id', '=', 'order_items.order_id')
                   ->join('prepaid_credits', 'order_items.id', '=', 'prepaid_credits.order_item_id')
                   ->where('orders.user_id', '=', 1)
                   ->select(DB::raw('SUM(prepaid_credits.quantity) as quantity, prepaid_credits.product_id'))
                   ->groupBy('prepaid_credits.product_id')
                   ->havingRaw('SUM(prepaid_credits.quantity) > 0')
                   ->get();

I get a list of basket items as follows:

  $basketItems = DB::table('basket_items')->where('basket_id', '=', 4)->get();

Now I want to create two new collections. One collection of basket items where the user can use an available credit and another collection where the user has to pay. How do I loop through and check the basketItems collection with the prepaidCredits collection to give me the above? So in the example above I should end up with $basketItemsUseCredits with basket_items with id's 1,3 and 4. $basketItemsPay should end up with basket_items with id's 2 and 5.

  $basketItemsUseCredits = $basketItems->filter(function ($basketItem) {
       // return items where user has pre paid credits for that product
  })

  $basketItemsPay = $basketItems->filter(function ($basketItem) {
       // return items where user has no pre paid credits
  })

Any help appreciated.



via Chebli Mohamed

mercredi 25 janvier 2017

how to redirect with https for particular page only in laravel

i want to redirect particular page only to https, rest of page will remain in normal http.

i want to do this for payment page only.after successful payment site will run with normal http.

so please help me for do this.

i already try this one.

Route::resource('paynow', ['uses' => 'Account\PaymentController', 'https' => true]); 

but this will not work for me.



via Chebli Mohamed