AdamEsterle

Experience

65,890

0 Best Reply Awards

  • Member Since 1 Year Ago
  • 647 Lessons Completed
  • 0 Favorites

23rd March, 2017

AdamEsterle left a reply on Prevent Sweet Alert On Back • 3 days ago

Is there a solution with no-cache meta tags? I feel like it is right in from of my face but I cannot get it

<meta http-equiv="cache-control" content="max-age=0"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="expires" content="0"/>
<meta http-equiv="pragma" content="no-cache"/>

22nd March, 2017

AdamEsterle left a reply on Change "Whoops, Looks Like Something Went Wrong" Page • 4 days ago

@nisaac2fly That is perfect.

I was about to post a solution to override convertExceptionToResponse, but your solution makes more sense.

17th March, 2017

AdamEsterle started a new conversation Modern Http Error Handling • 1 week ago

Which http errors should be (or do you recommend) handled with a special page?

Laravel comes with a 503.blade which is for maintenance mode

I'm assuming we also want 404.blade

What about other ones? Can other ones be combined into a generic one? I could have sworn you used to be able to do 40x.blade.php, was that an older Laravel version?

Any advice is appreciated

16th March, 2017

AdamEsterle started a new conversation Storing And Display Time 101 • 1 week ago

I need a full end-to-end recommendation for storing and display time for a user:

  1. Should I store times in database in server's time zone or UTC?

  2. Should I convert to user's time zone with PHP or JavaScript?

  3. How should I get the user's time zone? A setting they can set, PHP, or JavaScript?

  4. If I use a setting, how do I present them a list to choose from? Twitter's is nice https://twitter.com/settings/account but the php function timezone_identifiers_list() returns way too many even if I limit it to just America's

  5. If I convert to user's time zone with Carbon, does that mean I need to call a fluent setter every time I want to display it? $model->created_at>setTimeZone(auth()->user()->timezone)->diffForHumans() ?? or is there a way to automatically do that? Like an Eloquent Model cast?

14th March, 2017

AdamEsterle left a reply on Tell Browser To Cache • 1 week ago

Awesome! Thank you

I use the Mix versioning to do the busting of css and js

I have custom code to change the filenames of the images I am using (like a user's profile picture). That is needed too since cache-control effects images also? Is there anything I'm missing other than images, css, and js?

AdamEsterle left a reply on Tell Browser To Cache • 1 week ago

So Laravel does not control this by default since it is an apache/nginx thing?

AdamEsterle started a new conversation Tell Browser To Cache • 1 week ago

Is there a way to tell the browser (via your code) to cache your js/css files for x amount of time? Or is that just a browser setting the user has to set on their own browser?

I hear it being referred to byJeffrey as if the developer has this power either via PHP or JS

13th March, 2017

AdamEsterle left a reply on Where Should Home Page Url Direct Logged In Users? • 1 week ago

@Snapey

Nothing other than - some websites seem to do it (facebook.com)

I am getting some usability testing results too of people thinking the Logo in the navbar will take them to the dashboard even though it takes them to the landing page (dashboard navbar link right next to logo lol).

Maybe that is just a small bit of confusion after a user does that once and realizes the site layout

AdamEsterle left a reply on Where Should Home Page Url Direct Logged In Users? • 1 week ago

@jlrdw Agreed. But I am talking about something different

EXAMPLE domain.com domain.com/dashboard - only for logged in users

When a logged in user goes to domain.com - should I just take them to domain.com/dashboard - i.e. they cannot access the landing page anymore?

AdamEsterle left a reply on Where Should Home Page Url Direct Logged In Users? • 1 week ago

I just was curious if there was any standards that I was missing on whether or not to do it.

Like what kind of site would the landing page be considered pointless after logged in

12th March, 2017

AdamEsterle started a new conversation Where Should Home Page Url Direct Logged In Users? • 2 weeks ago

When should you show or not show the domain.com page for logged in users?

Is seeing the home page (that a non-logged in user sees) pointless when logged in?

Or should domain.com redirect to, say, domain.com/dashboard when logged in?

15th November, 2016

AdamEsterle left a reply on Recursive Conversion Of Array To Collection • 4 months ago

Does this work? I nice little recursive function that will convert all nested arrays to collections of collections instead of arrays of arrays

function r_collect($array)
    {
        foreach ($array as $key => $value) {
            if (is_array($value)) {
                $value = r_collect($value);
                $array[$key] = $value;
            }
        }

        return collect($array);
    }

8th September, 2016

AdamEsterle left a reply on Laravel Echo Not Receiving Event From Pusher • 6 months ago

Holy Jamole!

If you do a custom event name with "broadcastAs", then you need to prefix it with a "." in Echo.listen()

The documentation mentions the dot, but only for specifying namespaces. It makes sense it would be like this, just not crystal clear at first read

AdamEsterle started a new conversation Laravel Echo Not Receiving Event From Pusher • 6 months ago

Echo is not receiving event, however, capturing the event the old way (pusher code and not echo) works fine. Laravel sends event and pusher receives just fine too.

I am having trouble setting up Echo on the client side. Does Echo have any dependencies? Is there a compiled dist/echo.js that I can get ahold of? I do not want to have to compile with Webpack.

the node_modules dist/echo.js file seems compiled nicely, but the last line seems to be causing errors ln 538: module.exports = Echo;

24th August, 2016

AdamEsterle started a new conversation Has Many Through Parent Scope • 7 months ago

I can do a HasManyThrough and add on a query scope like this:

$user->hasManyThrough('Comments', 'Post', 'owner_id')->approved()

The approved() scope gets applied to the Comments class. However, how do I add a scope to the Post class? Specifically, I need to add withTrashed(), but I cannot find out how to build onto that relationship. I tried looking into the getParent() function with no luck

Thoughts?

25th July, 2016

AdamEsterle left a reply on Unions, Pagination, And Sorting • 8 months ago

I have a solution.

$pictures= $this->pictures()
                      ->select('id', 'created_at')
                      ->addSelect(DB::raw('"Pictures" as model'));

        $videos= $this->videos()
                         ->select('id', 'created_at')
                         ->addSelect(DB::raw('"Videos" as model'));

        $items = $videos->unionAll($pictures)
                          ->orderBy('created_at')
                          ->skip(($page - 1) * $perPage)
                          ->take($perPage)
                          ->get();

        $items->transform(function ($item, $key) {
            if ($item->model == "Picture") {
                return Picture::find($item->id);
            } else {
                return Video::find($item->id);
            }
        });

We only select the 'id' and 'created_at' from the two models. This is because "unionAll" needs to have the same amount of columns in each. We then can order by created_at and use skip() and take() to do some pagination.

I put the addSelect() in there so that I can differentiate what Model it is (since id = 1 could be either)

Then, I take the collection (made up of id, created_at, and model) and cycle through them and transform it to an Actual model.

AdamEsterle left a reply on Unions, Pagination, And Sorting • 8 months ago

I have a solution.

$pictures= $this->pictures()
                      ->select('id', 'created_at')
                      ->addSelect(DB::raw('"Pictures" as model'));

        $videos= $this->videos()
                         ->select('id', 'created_at')
                         ->addSelect(DB::raw('"Videos" as model'));

        $items = $videos->unionAll($pictures)
                          ->orderBy('created_at')
                          ->skip(($page - 1) * $perPage)
                          ->take($perPage)
                          ->get();

        $items->transform(function ($item, $key) {
            if ($item->model == "Picture") {
                return Picture::find($item->id);
            } else {
                return Video::find($item->id);
            }
        });

23rd July, 2016

AdamEsterle started a new conversation Unions And Pagination • 8 months ago

So it looks like unions and paginations cannot be done with Laravel? Please let me know if I am wrong.

Problem: I am trying to display a list of Picture and Video objects ordered by 'created_at'

Temporary solution: Get Collection of all Pictures, Collection of all Videos and do a "push" on them and sortByDesc('created_at') Then I do a ->forPage($page, $perPage) and that paginates nicely!

However, as you can see, that means with EVERY request I am retrieving EVERY record, putting it into memory and filtering.

I tried thinking of solutions like: Picture::paginate(10) and Video::paginate(10)

But because of the sortByDesc('created_at') this cannot be done. Why? because, say, I am on page 2. That means I get Pictures 11-20 and Videos 11-20, BUT because of the created_at sorting, I may need to display Videos 0-20 BEFORE ANY pictures are displayed. But this paginate(10) will not even retrieve Videos 1-10 (since I am on page two).

Any solutions would be greatly appreciated!

12th July, 2016

AdamEsterle left a reply on $error->has() For ANY Errors In Input Array • 8 months ago

Here is what I am doing so that the view can be as clean as possible.

I am running a check after the validation to see if there exists an error on the user.*. If there is, I add the error with the name of 'members'. This way, on my view side, I can keep it clean with $error->has('members') etc.

public function getValidatorInstance()
    {
        $validator = parent::getValidatorInstance();

        $validator->after(function () use ($validator) {
            if ($error = $this->getIndividualMemberError($validator)) {
                $validator->errors()->add('members', $error);
            }
        });

        return $validator;
    }

10th July, 2016

AdamEsterle left a reply on $error->has() For ANY Errors In Input Array • 8 months ago

@Snapey Great idea, however, there is no parent element http://pasteboard.co/9YMNwLzzq.png

@Guardian I could try that. Maybe do a check with a regular expression? That would be cool to allow $errors->get() to take a regular expression. Maybe I'll do a PR if it comes out well.

AdamEsterle left a reply on $error->has() For ANY Errors In Input Array • 8 months ago

@Snapey I cannot do that as I have other fields on the form

@Thijmen $errors->get('user'); does not work so neither would count() around it

AdamEsterle started a new conversation $error->has() For ANY Errors In Input Array • 8 months ago

I want to check, on my view, if there are any errors.

Normally: $errors->has(name-of-field) Or with an array: $errors->has(array.index)

How do I check if there was an error for ANY of my input arrays?

I want so display an error if $error->has('user.0') || $error->has('user.1') || ... But I do not know how many user's will be validated, I just want to display the error if user.any has an error

I tried: $errors->has('user.*') , but that did not work

Any ideas?

8th July, 2016

AdamEsterle left a reply on Create Symlink On Windows With Homestead • 8 months ago

Newest 5.0.24

I actually got it to work shortly after posting this I ran my git bash as administrator and it worked

I also created the symlink with full paths ln -s /the/full/path /the/full/path2

I don't know if that is needed, but that is what someone on Github mentioned trying

AdamEsterle started a new conversation Create Symlink On Windows With Homestead • 8 months ago

Regarding the Laravel filesystem:

The documentation (https://laravel.com/docs/master/filesystem#configuration) says to create a symlink from public/storage to storage/app/public

That is a great idea. However, when I try to create a symlink on homestead with ln -s I get a protocol error since I am on a Windows 7 host machine

I have also tried mklink in cmd. exe on the windows side with /D and /J The links appear on the homestead side (they are red though), however, my server does not follow them I also tried disable_symlinks off;

Thoughts?

1st July, 2016

AdamEsterle started a new conversation Where Do Method Data Validation Checks Belong? • 8 months ago

I always find myself stopping to ask "Where do method data validation checks belong?"

This may be more of a general software design philosophy question (than directly applying to Laravel).

What I DO NOT mean: NOT Request validation - that just gets validated in controller thank to the IOC NOT Policies - the user is authorized at the top of the controller to see if they should even be doing this action

What I do mean: An example will work best.

I am writing a method to transfer an amount from User 1 to User 2. public function transfer(User $from, User $to, $amount){} That method gets called in my controller route::post() --> [email protected] --> calls $class->transfer($from, $to, $amount);

Before I do the transfer, I need to make sure $from has a bank account, $to has an account, $to allows money to be received, $amount is between my global min and max, and on and on.

ALAS, MY DESIGN QUESTION: Should I do those checks inside the "transfer" method or do them outside - in this case, the controller?

Should methods be "dumb"? I call "transfer" and it better transfer, now, no checking, no nothing (I already did the checking)

Or should the methods be careful? "Ok, I'm told to do this transfer, but let me make sure all of the data is valid and the user has this attribute, etc, etc.?"

Any help would be much appreciated as I keep running into this problem and I want to understand the modern standards and be consistent.

5th February, 2016

AdamEsterle left a reply on Multiple Polymorphic Relationships With The Same Controller • 1 year ago

Thanks! Makes sense now! I will implement this and hit you up if something is weird, but it looks good on paper.

Thanks for all your help!

AdamEsterle left a reply on Multiple Polymorphic Relationships With The Same Controller • 1 year ago

I am not sure how that strategy would work? If the CommentController uses the CommentTrait, then how will it know which "morphClass" to use (for store)? Right now the morphClass is set in the BlogController and PictureController. But it sounds like your newer solution does not involve those two Controllers?

Would you routes look like this?

//  store any
    Route::post('comment', 'CommentController@store');

// update any
    Route::put('comment/{comment}', 'CommentController@update');

// delete any
    Route::delete('comment/{comment}', 'CommentController@destroy');

Thanks!

2nd February, 2016

AdamEsterle left a reply on Multiple Polymorphic Relationships With The Same Controller • 1 year ago

Thanks for the insight!

Does this seem good?

    // Blogs
    Route::post('blog/{blog}/comment', [
        'as'   => 'blog.comment.storeComment',
        'uses' => 'BlogController@storeComment'
    ]);
    Route::put('blog/{blog}/comment/{comment}', [
        'as'   => 'bill.comment.updateComment',
        'uses' => 'BlogController@updateComment'
    ]);
    Route::delete('blog/{blog}/comment/{comment}', [
        'as'   => 'blog.comment.destroyComment',
        'uses' => 'BlogController@destroyComment'
    ]);

    // Pictures
    Route::post('picture/{picture}/comment/{comment}', [
        'as'   => 'picture.comment.storeComment',
        'uses' => 'PictureController@storeComment'
    ]);
    Route::put('picture/{picture}/comment/{comment}', [
        'as'   => 'picture.comment.updateComment',
        'uses' => 'PictureController@updateComment'
    ]);
    Route::delete('picture/{picture}/comment/{comment}', [
        'as'   => 'picture.comment.destroyComment',
        'uses' => 'PictureController@destroyComment'
    ]);
trait CommentTrait
{
    public function storeComment(Request $request, $commentableId)
    {
        $comment = new Comment;
        $comment->user_id = Auth::user()->id;
        $comment->commentable_id = $commentableId;
        $comment->commentable_type = $this->morphClass;
        $comment->body = $request->body;
        $comment->save();
    }

    public function updateComment(Request $request, $commentableId, Comment $comment)
    {
        $comment->body = $request->body;
        $comment->save();
    }

    public function destroyComment(Request $request, $commentableId, Comment $comment)
    {
        $comment->delete();
    }
}

This is what I have now, however, there are some unnecessary things. The update/destroy methods do not use/need the commentableId since they just update the comment with the primary key (id). So I could make them generic and have the routes be like

// blog store
Route::post('blog/{blog}/comment', [
        'as'   => 'blog.comment.storeComment',
        'uses' => 'BlogController@storeComment'
    ]);

// Pictures store
    Route::post('picture/{picture}/comment/{comment}', [
        'as'   => 'picture.comment.storeComment',
        'uses' => 'PictureController@storeComment'
    ]);

// update any
    Route::put('blog/{blog}/comment/{comment}', [
        'as'   => 'bill.comment.updateComment',
        'uses' => 'BlogController@updateComment'
    ]);

// delete any
    Route::delete('blog/{blog}/comment/{comment}', [
        'as'   => 'blog.comment.destroyComment',
        'uses' => 'BlogController@destroyComment'
    ]);

But then you'll notice that I have to have the updateComment and destroyComment logic somewhere (in my case BlogController). But that seems misleading. I could make a "CommentController" for update & destroy and have the CommentTrait JUST have the store logic. BUT then that means update & delete logic are in CommentController and store is spread in Blog/PictureController via a trait.

Summary: I keep store, update, delete in the Trait, but then there are unnecessary routes and parameters not being used OR I extract Update & delete to CommentController and keep store in the CommentTrait, but then the three Comment methods (store, update, delete) are not located in the same spot.

Thoughts?

Please let me know if you need clarification.

Thank you in advance for any help!

28th January, 2016

AdamEsterle left a reply on Multiple Polymorphic Relationships With The Same Controller • 1 year ago

I like where you are going.

So would you have all 7 resource methods for the Comment (create, index, edit, update, etc) duplicated in EACH BlogController and PictureController (which each have their own 7 resource methods)?

I feel like that would be a bunch of duplication, unless that is where your trait comes in.
Would you care to explain more?

27th January, 2016

AdamEsterle started a new conversation Multiple Polymorphic Relationships With The Same Controller • 1 year ago

I am wondering how to design the controller to be used by multiple polymorphic relationship models.

Example: Models: Blog, Picture Polymorphic relationship: comment

In my routes.php I have

Route::resource('blog.comment', 'CommentController');
Route::resource('picture.comment', 'CommentController');

In CommentController I have:

 public function create(Blog $blog, Picture $picture){
    // do a check to see which one it is
    // blog/1/comment/create
    // or
    // picture/1/comment/create
}

How should I go about this? I feel like my solution above is not correct. If it is, how should I go about checking which Model the comment is about to be created for (or edit, store, update, etc.)? Like the below?

if ($blog->exists != null)

Thanks for advising

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.