Mattiman

Mattiman

Member Since 4 Years Ago

Experience Points 22,880
Experience
Level
Lessons Completed 265
Lessons
Completed
Best Reply Awards 4
Best Answer
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

28 Mar
8 months ago

Mattiman left a reply on Homestead Use Of Undefined Constant MCRYPT_RIJNDAEL_128 - Assumed 'MCRYPT_RIJNDAEL_128'

Found the solution! I was editing the wrong Homestead.yaml file. I actually have two of them:

One here:
~/code/Homestead/Homestead.yaml

and another here:
~/Homestead/Homestead.yaml

I was editing the first one, I should have edited the second one.

Not sure why there are two. What is that folder ~/code/Homestead/ doing there anyway?

Mattiman left a reply on Homestead Use Of Undefined Constant MCRYPT_RIJNDAEL_128 - Assumed 'MCRYPT_RIJNDAEL_128'

@run4ik yes you're correct. I fixed that typo, did a complete destroy and rebuild of vagrant. Still get the error

No input file specified. 

@m-rk I have this in my app config:

'cipher' => 'AES-256-CBC',

Not using encryption anywhere in my app

Mattiman left a reply on Homestead Use Of Undefined Constant MCRYPT_RIJNDAEL_128 - Assumed 'MCRYPT_RIJNDAEL_128'

@rin4ik the code in /home/vagrant/Code/Laravel/config/app.php is from a very old 4.0 project (as 36864 correctly guessed). I removed that folder completely. I don't understand why it suddenly points to that folder. I have been working on my project for a while, upgrading laravel, installing and using packages in the correct folder, etc, so I can't imagine it being that case all the time.

@36864 Using Homestead because that's what is recommended, and to keep it isolated from my other sites in MAMP, which are non-laravel so don't need a specific version of PHP etc.

After removing the folder /home/vagrant/Code/Laravel/ I now get the error

No input file specified. 

I tried vagrant destroy and vagrant up, and vagrant up --provision This is my Homestead.yaml

---
ip: "192.168.10.10"
memory: 2048
cpus: 1

authorize: /Users/matthijs/.ssh/id_rsa.pub

keys:
    - /Users/matthijs/.ssh/id_rsa

folders:
    - map: /Users/matthijs/code
      to: /home/vagrant/Code

sites:
    - map: larabook.test
      to: /home/vagrant/code/larabook/public
    - map: myproject.test
      to: /home/vagrant/Code/myproject.test/public

variables:
    - key: APP_ENV
      value: local

Mattiman left a reply on Homestead Use Of Undefined Constant MCRYPT_RIJNDAEL_128 - Assumed 'MCRYPT_RIJNDAEL_128'

Since Laravel 5.1 mcrypt is not needed anymore. So why am I getting this error running Laravel 5.5?

I also noticed something weird in the error message: it is pointing to a folder require( '/home/vagrant/Code/Laravel/config/app.php' ) which shouldn't be referenced. The project is in Code/myproject

So maybe something is wrong with the file reference in Homestead?

Warning: Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' (this will throw an Error in a future version of PHP) in /home/vagrant/Code/Laravel/config/app.php on line 83
Call Stack
#   Time    Memory  Function    Location
1   0.0033  394472  {main}( )   .../index.php:0
2   0.1399  2866200 App\Http\Kernel->handle( )  .../index.php:52
3   0.1399  2866200 App\Http\Kernel->sendRequestThroughRouter( )    .../compiled.php:1218
4   0.1399  2866200 App\Http\Kernel->bootstrap( )   .../compiled.php:1227
5   0.1399  2866200 Illuminate\Foundation\Application->bootstrapWith( ) .../compiled.php:1242
6   0.1402  2867664 Illuminate\Foundation\Bootstrap\LoadConfiguration->bootstrap( ) .../compiled.php:726
7   0.1402  2867976 Illuminate\Config\Repository->offsetGet( )  .../compiled.php:1177
8   0.1402  2867976 Illuminate\Config\Repository->get( )    .../compiled.php:4994
9   0.1402  2868832 Illuminate\Config\Repository->load( )   .../compiled.php:4886
10  0.1402  2868832 Illuminate\Config\FileLoader->load( )   .../compiled.php:4906
11  0.1404  2868912 Illuminate\Filesystem\Filesystem->getRequire( ) .../compiled.php:5068
12  0.1420  2869280 require( '/home/vagrant/Code/Laravel/config/app.php' )  .../compiled.php:5166

Mattiman left a reply on Homestead Use Of Undefined Constant MCRYPT_RIJNDAEL_128 - Assumed 'MCRYPT_RIJNDAEL_128'

@rin4ik have looked at Valet docs but seems quite complex to me with everything I have to install. I need to install php and mysql on my own machine (if not already there) and maintain those. I'm certain that will cause problems in the future when OS X updates mess with the PHP and Mysql installs. And, I'm also running MAMP already so that's probably going to not work together since it also uses port 80.

Mattiman left a reply on Homestead Use Of Undefined Constant MCRYPT_RIJNDAEL_128 - Assumed 'MCRYPT_RIJNDAEL_128'

@rin4ik ok thanks. I'll run the command if there's no other way to fix this issue.

First I want to understand what's going on. Which version of Laravel needs which version of PHP? The 5.5. docs say: "Laravel 5.5 requires PHP 7.0.0 or higher".

Mattiman left a reply on Homestead Use Of Undefined Constant MCRYPT_RIJNDAEL_128 - Assumed 'MCRYPT_RIJNDAEL_128'

@lostdreamer_nl reading a bit more about this, I found out that mcrypt is not included in PHP7.1 because it's deprecated. So I shouldn't have to install it.

The question then becomes: why is Laravel 5.5 complaining about it missing? Is 5.5 not able to run on PHP 7.1?

Mattiman left a reply on Homestead Use Of Undefined Constant MCRYPT_RIJNDAEL_128 - Assumed 'MCRYPT_RIJNDAEL_128'

@rin4ik I'll look into Valet. If this problem can't be fixed and Valet is better it's an option

@lostdreamer_nl so where do I run these commands?

It' strange that crypt is not installed. Maybe I can create a git issue for that on the Homestead repo.

Mattiman started a new conversation Homestead Use Of Undefined Constant MCRYPT_RIJNDAEL_128 - Assumed 'MCRYPT_RIJNDAEL_128'

Since a few days I suddenly get this error:

Use of undefined constant MCRYPT_RIJNDAEL_128 - assumed 'MCRYPT_RIJNDAEL_128' 

I'm running this project on Laravel 5.5 on Homestead. To be sure, I just updated homestead, Virtualbox, Vagrant to the latest version. All on Mac.

Searching I find many people which have this problem and the solution is to install crypt. But that's outside of Homestead. I thought Homestead would contain everything that's needed for a Laravel project? So what could cause the problem in my situation?

14 Mar
9 months ago

Mattiman left a reply on Homestead Updating

Ok I will leave out the rm -rf .vagrant command from now on.

About what you say "Destroy removes the currently built box and its providers while remove deletes the box and prune deletes old box versions" This is not very clear. If I break up the sentence:

  • Destroy removes the currently built box and its providers
  • remove deletes the box
  • prune deletes old box versions

What is the difference between remove and delete (of a box)?

What is "its providers"?

Prune is the same as vagrant box remove laravel/homestead --box-version=0.4.4 (but then for all older boxes?)

Thanks for the help, appreciated

Mattiman left a reply on Homestead Updating

Hi @ejdelmonico I created that list of commands thanks to your help last year https://laracasts.com/discuss/channels/general-discussion/homestead-upgrade?page=1 You description now is different from the one you gave then.

About the "I do not delete the .vagrant file and you should not have to either": To be honest I don't even think I have a .vagrant file. So the command $ rm -rf .vagrant probably doesn't do anything. I do have a .vagrant.d directory with a directory inside called "boxes".

At the moment I have these files in my user directory:

.homestead/
  .vagrant/
    machines/
      default/
        virtualbox/
          // empty
    after.sh
    aliases
.vagrant.d/
  boxes/
    laravel-VAGRANTSLASH-homestead/
      5.2.0/
        virtual box/
    // some more files
code/Homestead/
   // some more files

But I did solve the problem, by doing this command:

$ vagrant box update
==> homestead-7: Box 'laravel/homestead' not installed, can't check for updates.
$ vagrant box add laravel/homestead

$ rm -rf .vagrant
$ vagrant up

It's all still very fuzzy to me. You have Vagrant, vagrant boxes, Homestead box, homestead code, etc. Haven't found a clear explanation of what everything is exactly yet. The official docs are lacking as well, since the only command you need according to those are:

vagrant box update
git pull origin master

So nothing about destroying or removing boxes.

13 Mar
9 months ago

Mattiman started a new conversation Homestead Updating

So, like every six months or so I'm back asking for help fixing Homestead update problems. From the last time I asked https://laracasts.com/discuss/channels/general-discussion/homestead-upgrade?page=1 I have had this list of command I run to update. It worked for a couple of times:

Download and install latest VirtualBox
https://www.virtualbox.org/wiki/Downloads
Download and install latest vagrant
https://www.vagrantup.com/downloads.html
Update box and repo Homestead:
$ cd ~/Homestead
$ git fetch origin
$ git checkout v7.1.0 // whichever tag is released
$ vagrant box update
$ vagrant destroy
$ rm -rf .vagrant
$ vagrant up

But today I tried this again but I get this:

$ cd ~/Homestead
$ git fetch origin
remote: Counting objects: 15, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 15 (delta 8), reused 14 (delta 8), pack-reused 0
Unpacking objects: 100% (15/15), done.
From https://github.com/laravel/homestead
   4ee5e98..9f36545  master     -> origin/master
 * [new tag]         v7.3.0     -> v7.3.0
 * [new tag]         v7.2.0     -> v7.2.0
$ git checkout v7.3.0
Previous HEAD position was 4ee5e98... ? ? Update version to 7.1.2
HEAD is now at 9f36545... ❄️ ? ? Require box v5.2.x and tag v7.3.0
$ vagrant box update
==> homestead-7: Box 'laravel/homestead' not installed, can't check for updates.

So why do I get this message? I certainly have Homestead installed. And what can I do now?

01 Dec
1 year ago

Mattiman left a reply on Homestead Upgrade

OK so today another Homestead update was released. Maybe a good way to find out if I understand everything correctly.

So there are four applications/levels:

  1. Virtualbox. I was using Virtualbox 5.1. Downloaded latest version 5.2.2 from https://www.virtualbox.org/wiki/Downloads and installed that.

  2. Vagrant. If I check

$ vagrant -v
Vagrant 2.0.0

Looking at https://www.vagrantup.com/ the current version is 2.0.1. How do I upgrade Vagrant?

  1. Homestead box and package upgrading:
$ vagrant box update
$ git pull origin master

What I do'nt understand here is what the difference is between the box and the package. And why you first update the box and after that download the homestead source code. Isn't that Homestead source code needed for the box?

30 Nov
1 year ago

Mattiman left a reply on Homestead Upgrade

Haven't considered Valet, no. First time I hear about it. Will check it out.

Day to day I use MAMP Pro, a very convenient way to run about 60 websites locally. But started using Homestead (in parallel) for a specific project after learning about it here at Laracasts/Laravel community. So far my experience with it is not very good, but don't want to give up too soon. People say it has big advantages, so maybe one day I will see the light ;)

29 Nov
1 year ago

Mattiman left a reply on Homestead Upgrade

Thanks for the input @ejdelmonico, appreciated. I'm not 'mentally resisting'. It's just very frustrating to encounter these upgrade problems with Homestead every six months or so. And every time there's something changed so the old advice doesn't work anymore. I keep a file on my desktop with exact notes on the commands I need to run to start up/shut down/install/upgrade everything. But I keep having to change the notes because things don't work anymore.

Mattiman left a reply on Homestead Upgrade

Ok but vagrant box list gives me:

$ vagrant box list
laravel/homestead (virtualbox, 4.0.0)

So this gives me the Virtualbox version. That's just one out of four levels. I can check vagrant with:

$ vagrant -v
Vagrant 2.0.0

So that's version 2.0.0 of Vagrant. But what about Homestead box and package?

By the way, this is the first time I lean about the difference between Homestead box and Homestead package. Another confusing thing. This is not described in the Laravel documentation.

I really don't understand why there's such a mismatch between the Homestead docs and the other docs. Every other detail about laravel is described very well, but when it comes to Homestead you're supposed to be a pro sys admin/linux developer or something to understand what's going on.

Mattiman left a reply on Homestead Upgrade

This is confusing: "If a newer version of homestead box or package then destroy the vagrant box and run vagrant up after updating everything"

How do I know if there is a new version of homestead box or package? How do I check which versions of box and package I have running?

28 Nov
1 year ago

Mattiman left a reply on Homestead Upgrade

Thanks, that makes a few things clearer.

Is there a way to check for those version numbers? Virtualbox I can start up and check 'about'. But Vagrant, Homestead box and 'Homestead package'?

Yes a lot of previous knowledge is assumed, apparently. That's the problem.

Mattiman left a reply on Homestead Upgrade

So now it's version 2.0.1? I thought it was version 4? Or 6. This homestead/vagrant stuff doesn't make any sense at all. Every few months when I try to update it's the same trouble. Over and over again. Apparently for some people it's easy or logical. The documentation on Homestead is severely lacking.

Sorry for the rant, but it's really frustrating to have to ask for help every few months again and get replies with different advice every single time. I follow up the advice and it works once but then the next time it doesn't.

Mattiman left a reply on Homestead Upgrade

@ejdelmonico OK that was not clear indeed. It is very confusing. Is there a way to see which Homestead version you're running? And which Vagrant?

And if I may ask, in which order should I do these commands? Like this?

$ cd ~/Homestead
git fetch
git checkout v6.6.0
vagrant destroy
vagrant up

And how is git fetch/ git checkout v6.6.0 different from vagrant box update?

Mattiman started a new conversation Homestead Upgrade

So Homestead 6.6.0 was released https://laravel-news.com/laravel-homestead-6-6-0 And so I tried updating:

$ cd ~/Homestead
$ vagrant box update
==> homestead-7: Checking for updates to 'laravel/homestead'
    homestead-7: Latest installed version: 4.0.0
    homestead-7: Version constraints: >= 3.0.0
    homestead-7: Provider: virtualbox
==> homestead-7: Box 'laravel/homestead' (v4.0.0) is running the latest version.

So it can't update.

What is going on here? I know for sure it has been a few months since I updated Homestead, so it definitely isn't uptodate.

26 Nov
1 year ago

Mattiman left a reply on How To Upload Laravel 5.5 Project On The Server?

You can also upload all files with FTP to the server. The only thing that you need to change, compared to local, is the config files and the .env file. Make sure the settings in both of these are correct for the environment you're uploading to.

05 Aug
1 year ago

Mattiman left a reply on How To Check Which Homestead Version Is Running

Yes it is confusing. I downloaded the latest Vitualbox dmg this week and installed/updated that to 5.1.26. So that is uptodate.

I also did

$ cd ~/Homestead
$ vagrant box update

The question though is which Homestead is running now?

To make it even more confusing, I can also do

$ vagrant -v
Vagrant 1.9.1

So there's VirtualBox, Vagrant and Homestead. Three things. Maybe four, because there are also "boxes".

Mattiman left a reply on How To Check Which Homestead Version Is Running

Hi @rumm.an when I do that I get

laravel/homestead (virtualbox, 3.0.0)

But what is that saying? Virtualbox version 3.0.0? But what about Homestead?

Mattiman started a new conversation How To Check Which Homestead Version Is Running

After updating Vitualbox and Homestead this week, I wanted to check if I am running the latest. When I do

$ cd ~/Homestead
$ homestead -v
Laravel Homestead version 2.0.11

But I updated this week, and it is supposed to be version 6 now? https://laravel-news.com/laravel-homestead-version-6-released

How do I check which version is running?

25 Apr
1 year ago

Mattiman left a reply on Form Model Binding Related Many To Many Models

Finally solved the problem!

The solution is to check for old() instead of old('tags') This way I can check if the form was posted or not. So in code:

@foreach($tags as $tag)
<div class="col-sm-3">
    <label class="checkbox-inline " for="tags[{{ $tag->id }}]">
        <input id="tags[{{ $tag->id }}]" name="tags[{{ $tag->id }}]" type="checkbox" value="{{ $tag->id }}"
        <?php
        if(empty(old()) &&  $training->tags->contains($tag->id) ) {
            echo 'checked=checked';
        } elseif( is_array(old('tags')) && in_array($tag->id, old('tags')) ) {
            echo 'checked=checked';
        }
        ?>
        > {{ $tag->name }}    
    </label>
</div>
@endforeach

And if you want to use a multi select:

<?php 
$preselected_tags = array();
// if old() is empty it means form was not posted yet so check if training contains tags
if( empty(old()) ) {
    $preselected_tags = $training->tags->pluck('id')->toArray();
} elseif( is_array(old('tags')) ) {
    $preselected_tags = old('tags');
}
?>
<div class="columns small-4"> 
    {!! Form::label('tags', 'Tags') !!}
    {!! Form::select('tags[]', $tags->pluck('name', 'id')->all(), $preselected_tags , ['id' => 'tags', 'class' => 'form-control tag-group', 'multiple' ]) !!}
</div> 

Mattiman left a reply on Form Model Binding Related Many To Many Models

So still looking for a solution. The central problem is that unchecked checkboxes are not sent via a form. While other input types do send an empty value. So if you have

<input name="food" value="{{ old('food',$favourites->food) }}" />

And this form is posted and reshown with validation errors, the old('food',$favourites->food) will show the posted value for 'food', either empty or some value. If the form is shown for the first time, the function will look at any model value in $favourites->food

So that works correctly.

However, for checkboxes this falls apart:

    @foreach($tags as $tag)
        <div class="col-sm-3">
            <label class="checkbox-inline " for="tags[{{ $tag->id }}]">
                <input id="tags[{{ $tag->id }}]" name="tags[{{ $tag->id }}]" type="checkbox" value="{{ $tag->id }}"
                <?php  
                // Case 1: if old(tags) is an array it means one or more tage were posted
                if( is_array(old('tags'))  ) {
                    // if the posted tags contain the id of this tag, preselect checkbox
                    if( in_array($tag->id, old('tags')) ) {
                        echo 'checked=checked';
                    }
                // Case 2: if training model tags contain this tag, preselect checkbox    
                } elseif($training->tags->contains($tag->id)) {
                        echo 'checked=checked';
                }  
                // Case 3: if form is posted with no tags checkboxes selected
                // and form is shown again because of some validation error
                // checkboxes should NOT be selected
                // even though training model tags contain this tag

                ?>
                > {{ $tag->name }}    
            </label>
        </div>
    @endforeach

You see Case 3 there: if a user -deselected- the checkboxes, the form doesn't post any data. If the form is shown again (because of a validation error), old('tags') doesn't exist. So it will look for model data. And check the checkboxes again. Even though the user already unchecked them!

There might be a workaround using hidden checkboxes

<input type='hidden' name='checkbox' value='0' />
<input type='checkbox' name='checkbox' value='1' /> 

But that is invalid html, might give problems with any css/js trying to interact with the fields, and is very clunky and ugly (it involves also extra code in the controller to process the array and remove the 0 checkbox fields, etc

Any ideas?

Somehow I need to know in the form whether or not the form is shown for the first time or after a validation failure. In plain PHP I could do something like isset($_POST['submit']) or something. But in Laravel this is not possible.

23 Apr
1 year ago

Mattiman left a reply on Form Model Binding Related Many To Many Models

Am slowly going crazy here. We're talking about repopulating a couple of checkboxes in a form here. The most basic stuff, supposedly. Questions everywhere, like http://stackoverflow.com/questions/40739934/laravel-blade-repopulate-checkbox-if-validation-failed But nobody seems to have a solution.

In pseudo code I need something like this:

// loop over all existing tags
@foreach($tags as $tag)
<label class="checkbox-inline " for="tags[{{ $tag->id }}]">
    <input id="tags[{{ $tag->id }}]" name="tags[{{ $tag->id }}]" type="checkbox" value="{{ $tag->id }}"
    // if form is shown for the first time, make checkbox checked

    // else if form is posted but reshown, make checkbox checked if $_POST contains this checkbox
    
// else nothing
    >
</label>
@endforeach

But there's no way to make the distinction if the form is shown for the first time or reshown after a failed validation error.

  • In both cases Laravel makes a GET request. So no point checking the type of Request

  • Checking whether old('tags') is empty or null is not enough either, because it can be empty/null when showing the form the first time or it can be empty/null when the tags array being posted is empty.

  • The old() function doesn't work for arrays of values either.

The solution given by @Snapey here https://laracasts.com/discuss/channels/laravel/old-data-didnt-display-in-edit-page Only works if the main model has attributes like $transport->bike or $transport->car But this doesn't work if you have an array of relation values.

  <input type="checkbox" name="bike" value="Bike" 
    @if(old('bike',$transport->bike))
        checked
    @endif
    > I have a bike<br>

  <input type="checkbox" name="car" value="Car" 
    @if(old('car',$transport->car))
        checked
    @endif
    > I have a car<br>

Any ideas? It is not possible that this is an edge case without a solution.

Mattiman left a reply on Form Model Binding Related Many To Many Models

Yes it seems that's the reason. In the meantime I did get it working, but I have no idea why:

@foreach($tags as $tag)
    <div class="col-sm-3">
        <label class="checkbox-inline " for="tags[{{ $tag->id }}]">
{!! Form::checkbox('tags[]',$tag->id,(old('tags') == $tag->id || $training->tags->contains($tag->id)),  ['id'=>'tags['.$tag->id.']'   ]) !!} 
            {{ $tag->name }}  
        </label>
    </div>
@endforeach

If I try to rewrite this to plain PHP :

<label class="checkbox-inline " for="tags[{{ $tag->id }}]">
    <input id="tags[{{ $tag->id }}]" name="tags[]" type="checkbox" value="{{ $tag->id }}"
    <?php if( old('tags') == $tag->id || $training->tags->contains($tag->id) ) { ?>
    checked=checked 
    <?php } ?> > 
    {{ $tag->name }}    
</label>

It does NOT work anymore. The problem with posting the form and the checkboxes checked not being remembered. Why this:

(old('tags') == $tag->id || $training->tags->contains($tag->id))

works inside the collective/html but not in plain PHP is not clear. Any idea?

Mattiman left a reply on Form Model Binding Related Many To Many Models

OK the new solution seems like a dead end. Going back to the previous solution, I discovered a problem:

@foreach($tags as $tag)
<div class="col-sm-3">
    <label class="checkbox-inline " for="tag[{{ $tag->id }}]">
       <input id="tag[{{ $tag->id }}]" name="tags" type="checkbox" value="{{ $tag->id }}"
        @if( 
            $training->tags->contains($tag->id) 
            || 
            ( is_array(old('tag') ) && in_array($this_tag_id, old('tag')) )  
            ) 
            checked=checked 
        @endif
        > {{ $tag->name }}    
    </label>
</div>
@endforeach 

The problem is:

  • when first loading the edit form, the tags from the Training model are correctly checked
  • when checking a new checkbox and submitting the form, when the form is reshown in case of an error, that checkbox is checked
  • BUT when unchecking a checkbox loaded from the Training model, when the form is reshown in case of an error, that checkbox is checked again. So the user has to see that and uncheck it again. So the logic in this is not correct:
        @if( 
            $training->tags->contains($tag->id) 
            || 
            ( is_array(old('tag') ) && in_array($this_tag_id, old('tag')) )  
            ) 
            checked=checked 
        @endif

As soon as $training->tags->contains($tag->id) resolves to true, the other part after || is skipped. That's not correct since it should take preference.

Mattiman left a reply on Form Model Binding Related Many To Many Models

You are right, the copy to the separate variable was not needed. I added that because I got an error and this solved it. But removing it now (after more changes) I do'n get the error anymore.

About your solution: it works partly, if I change something:

@foreach($tags as $tag)
    <div class="col-sm-3">
        <label class="checkbox-inline " for="tag[{{ $tag->id }}]">
           <input id="tag[{{ $tag->id }}]" name="tag[{{ $tag->id }}]" type="checkbox" value="{{ $tag->id }}"

            @if( old('tag[$tag->id]', $training->tags->contains($tag->id) ?? null ) )

                checked=checked 
            
            @endif
            
            > {{ $tag->name }}    
        </label>
    </div>
@endforeach

It does 'recheck' the values coming from $training->tags->contains($tag->id) but checked values in the form are not remembered when reshowing the form (if there's an error for example). So this part: old('tag[$tag->id]', is not correct it seems. I also tried

@if( old('tag', $training->tags->contains($tag->id) ?? null ) ==  $tag->id  )

But that has the same problem.

Mattiman left a reply on Form Model Binding Related Many To Many Models

@Snapey thanks. That's an interesting approach. Will think about it doing that way as well.

As far as the form, I have this now:

<div class="form-group ">

@foreach($tags as $tag)

    <div class="col-sm-3">
        <label class="checkbox-inline " for="tag[{{ $tag->id }}]">
           <input id="tag[{{ $tag->id }}]" name="tag[{{ $tag->id }}]" type="checkbox" value="{{ $tag->id }}"
            <?php $this_tag_id = $tag->id; ?>

            @if( 
                $training->tags->contains($tag->id) 
                || 
                ( is_array(old('tag') ) && in_array($this_tag_id, old('tag')) )  
                ) 
                checked=checked 
            @endif

            > {{ $tag->name }}    
        </label>
    </div>

@endforeach

</div>

It works, but it's messy. Would you see a possibility to improve this? I especially dislike the

<?php $this_tag_id = $tag->id; ?>
( is_array(old('tag') ) && in_array($this_tag_id, old('tag')) )  

But I don't see how I can improve that. Since it's a bunch of checkboxes there can be checked more then one. So what comes back from the db or from the request is an array. The total list of tags is also an array. So looping through the total list of tags, I have to do this in_array() check every time.

Mattiman left a reply on Form Model Binding Related Many To Many Models

@Snapey thanks again for your help. Getting closer. But not quite there yet. I am trying to add the old() as well. With your example I managed to solve the "edit" situation (in an ugly way) but now the form doesn't work anymore for the create situation:

@foreach($tags as $tag)

    <div class="col-sm-3">
        <label class="checkbox-inline " for="tag[{{ $tag->id }}]">
           <input id="tag[{{ $tag->id }}]" name="tag[{{ $tag->id }}]" type="checkbox" value="{{ $tag->id }}"
            <?php $this_tag_id = $tag->id; ?>
            @if( 
                $training->tags->contains($tag->id) || 
                ( is_array(old('tag') ) && in_array($this_tag_id, old('tag')) )  
                ) 
                checked=checked 
            @endif
            > {{ $tag->name }}    
        </label>
    </div>

@endforeach

A few things:

  1. The $this_tag_id = $tag->id; is really ugly, but I can't use $tag->id directly in the in_array() function

  2. The @if .. @endif check is quite messy. Maybe this is the only way, but is there a cleaner solution?

  3. When using this form for the create situation, I get a fatal error because $training object doesn't exist. To solve that I either have to pass an empty training object from the controller (ugly) or add yet another exist check for the training object in the @if ... endif part.

  1. Should I forget trying to reuse the same form partial for both create and edit? What is best practice here? At some point the code gets so ugly and messy that creating two form partials might be cleaner, even if I have to repeat it. But I can't imagine this problem hasn't been solved in a clean way a million times already. This is such basic stuff, but one of the reasons to use a framework like Laravel in the first place, to make writing this kind of boilerplate code easier.
22 Apr
1 year ago

Mattiman left a reply on Form Model Binding Related Many To Many Models

OK I found a partial solution. In my TrainingsController:

    public function edit(Training $training)
    {
        $selected_categories = isset($training) ? $training->categories->pluck('id')->toArray() : null;

        $tags = Tag::pluck('name', 'id')->all();

        return view('app.trainings.edit', compact('training', 'tags', 'selected_categories'));
    }

In my blade template form:

{!! Form::model($training, ['method' => 'PATCH', 'route' => ['app.trainings.edit', $training->id], 'class' => 'form']); !!}

                <?php 
                if(!isset($selected_categories)){
                    $selected_categories = array();
                }
                ?>
   <div class="custom-inputs">
    <div class="custom-checkbox">
        {!! Form::checkbox('categories[]', '1', (old('categories') == '1'  || in_array(1, $selected_categories)), ['id'=>'endurance'] ) !!} 
        <label for="endurance">Endurance</label>
    </div>
    <div class="custom-checkbox">
        {!! Form::checkbox('categories[]', '2', (old('categories') == '2'  || in_array(2, $selected_categories)), ['id'=>'resistance'] ) !!} 
        <label for="resistance">Resistance</label>
    </div>
    <div class="custom-checkbox">
        {!! Form::checkbox('categories[]', '3', (old('categories') == '3' || in_array(3, $selected_categories)), ['id'=>'strength'] ) !!} 
        <label for="strength">Strength</label>
    </div>

// etc

Now it does repopulate the checkboxes correctly. And the form still works for creating a new Training.

However: a) this seems like a very ugly method and b) when posting this form from the edit method I get a

MethodNotAllowedHttpException in RouteCollection.php line 251:

error. I did not change anything to my routes or form method so baffled where this error comes from.

Mattiman left a reply on Form Model Binding Related Many To Many Models

Thanks for your help!

@Screenbeetle I think I have used such a method for multiselect lists as well. But in this case I do need checkboxes in the interface, and I can't find a method to do the same with those.

@Snapey the form bind to the Training model. What is that dotted notation?

And if you ditched the Form library, how do you do your forms now? I just need to see a set of examples for a) create+edit form, repopulated from request and db data b) for textfield, texture, checkbox, radio boxes, select list

Mattiman started a new conversation Form Model Binding Related Many To Many Models

For a form which is used to both create and update a Training model I have this:

                {!! Form::label('categories[]', 'Category', ['class' => 'control-label']) !!}
                <div class="custom-inputs">
                    <div class="custom-checkbox">
                        {!! Form::checkbox('categories[]', '1', null, ['id'=>'endurance'] ) !!} 
                        <label for="endurance">Endurance</label>
                    </div>
                    <div class="custom-checkbox">
                        {!! Form::checkbox('categories[]', '2', (old('categories') == '2'), ['id'=>'resistance'] ) !!} 
                        <label for="resistance">Resistance</label>
                    </div>
                    <div class="custom-checkbox">
                        {!! Form::checkbox('categories[]', '3', (old('categories') == '3'), ['id'=>'strength'] ) !!} 
                        <label for="strength">Strength</label>
                    </div>
                </div>

The Training model has a many to many with Categories, therefore the checkboxes.

This will work fine when the form is submitted to create, when there's an error the form is shown with the previously selected checkboxes selected. As you can see for the third parameter Form::checkbox('categories[]', '1', null, will work but also {!! Form::checkbox('categories[]', '2', (old('categories') == '2')

However, how can I repopulate this form if I use it to edit the model? I have read dozens and dozens of threads with people asking similar questions but so far no solution. Pulling my hair out here. It's literally the first thing you do in a Hello World Blog application, repopulating a form to edit a model. Using laravelcollective html here, but nothing in the docs there as well.

In my controller method I have:

    public function edit(Training $training)
    {
        $tags = Tag::pluck('name', 'id')->all();

        return view('app.trainings.edit', compact('training', 'tags'));
    }
04 Apr
1 year ago

Mattiman left a reply on Error Homestead After Update To 2

@Cronix I wouldn't know. Which paths? Where should I have changed paths? There's nothing in the docs saying anything about changing paths.

Upgrading Homestead has so far always been a mess. No documentation at all and it assumes every user is a pro command line wizard. With each update I spend hours online looking for answers to weird bugs. Really tempted to just use MAMP for my local development of Laravel projects.

Mattiman left a reply on Error Homestead After Update To 2

Apparently the command changed. Instead of:

$ homestead ssh

It is now

$ vagrant ssh

I have to run that command from within the ~/Homestead folder.

Mattiman started a new conversation Error Homestead After Update To 2

Since updating to Homestead 2 everything seemed to work fine, but now I wanted to run "Homestead ssh" in the terminal and I get this error: ``` /Users/matthijs/.composer/vendor/laravel/homestead/Vagrantfile:17:in `read': No such file or directory @ rb_sysopen - /Users/matthijs/.homestead/Homestead.yaml (Errno::ENOENT)

It probably has something to do with a file path not being correct. With the update from Homestead 1 to 2 I had to move the Homestead.yaml file from ~/.homestead to ~/Homestead, to solve another problem (https://github.com/laravel/homestead/issues/519)

Hoe can I solve the problem above?



14 Dec
2 years ago

Mattiman left a reply on Updating Vagrant And Homestead

Ok thanks both @ejdelmonico and @svpernova09 It's kind of clear now. One procedure wipes out everything and installs fresh, the other updates from within vagrant. If the last one works, that's more convenient of course. If not, wiping out and installing fresh is probably the solution.

Mattiman left a reply on Updating Vagrant And Homestead

Ok I followed the instructions from @ejdelmonico and have everything updated successfully! I did

$ vagrant box add laravel/homestead
$  git clone https://github.com/laravel/homestead.git Homestead
$ cd ~/Homestead
$ vagrant up

After this I had to recreate the project databases and run artisan migrate and seed again.

Not sure which process to follow in the future, the official one or that from ejdelmonico I used now, so if you have any comments on the differences, please share.

Mattiman left a reply on Updating Vagrant And Homestead

Hi @svpernova09 that is great of course. Except for me currently. I'm halfway the process @ejdelmonico describes. In his process I now have to do:

$ vagrant box add laravel/homestead

Which I did. And after that:

$ cd ~ 
$ git clone https://github.com/laravel/homestead.git Homestead

The official docs now say:

git pull origin master

What's the difference between the two? Does it matter?

Mattiman left a reply on Updating Vagrant And Homestead

Ok back from sleep, changed the name of the folder :)

13 Dec
2 years ago

Mattiman left a reply on Updating Vagrant And Homestead

Which one would it be, the last one of these?

.homestead
.vagrant.d
/code/Homestead/
/Homestead

I'm not sure what the others are, or if they are old and can be removed.

Mattiman left a reply on Updating Vagrant And Homestead

Ok that's returning 1.9.1 :)

So far so good

Mattiman left a reply on Updating Vagrant And Homestead

Ok I have done that. I can open VirtualBox and see it's the latest version, but I don't know how to check which version vagrant is installed now.

Mattiman left a reply on Updating Vagrant And Homestead

I get

$ vagrant box list
laravel/homestead (virtualbox, 0.4.4)
laravel/homestead (virtualbox, 0.5.0)

Mattiman left a reply on Updating Vagrant And Homestead

Hi @ejdelmonico, if you say "the easiest way is to do vagrant box destroy laravel/homestead or just delete the vm from Virtualbox", do you mean running the command in the terminal? $ vagrant box destroy laravel/homestead

The steps 5 and 6 are still a bit unclear. What is "the home software repository"? And what the "vagrant box for homestead"?

The problem is that there are -zero, nada- instructions in the official documentation on updating Homestead. It literally only described a fresh installation. I really don't understand this oversight.

Mattiman left a reply on Updating Vagrant And Homestead

Hi @ejdelmonico thanks for your answer. If I may ask some follow up questions:

  • How do you do step 1 "destroy your box and remove old version"?
  • Why is your set of steps different from that from Joe Ferguson for example?

I really appreciate all the help, but it's utterly confusing to read different instructions in different places and every few months when I have to update homestead. Each time I make notes what I did last time, but then half a year later it doesn't work anymore and when asking online I get different instructions. Especially not understanding every line of instruction, why it is there and what it is doing.

For example, why are there two steps (5 and 6) both adding homestead? First the vagrant box add larval/homestead and after that a git clone homestead.

Thanks again for the help.

10 Dec
2 years ago

Mattiman started a new conversation Updating Vagrant And Homestead

I'm currently running VirtualBox 5.0.20 and Box laravel/homestead 0.5.0 on my mac. I think it's running globally, not on a per project bases.

The latest versions are: VirtualBox 5.1.10 Vagrant 1.9.1 Homestead 4.0 box version 1.0.1?

Updating these has always been a gigantic hassle. Always spend hours or days finding answers for problems I encounter. So before updating everything now, I'd like to ask:

1/ What exactly does "Homestead 4.0" and "box version 1.0.1" mean? I thought "Homestead" is a vagrant box itself?

2/ What are the exact update steps for all these? (virtual box, vagrant, homestead) In the official docs there is not a single line of documentation on updating. Elsewhere I find completely different instructions:

http://www.darwinbiler.com/keeping-your-homestead-box-up-to-date/
cd ~/Homestead
vagrant box update
vagrant destroy
vagrant up
# purge old version
vagrant box remove laravel/homestead --box-version=0.4.0

https://laracasts.com/discuss/channels/general-discussion/updating-homestead?page=1
Use "homestead update"

https://www.joeferguson.me/laravel-homestead-4-0-and-box-1-0/
cd /path/to/homestead
git fetch origin
git checkout v4.0.0
vagrant destroy
rm -rf .vagrant
vagrant up

And there are even more explanations I found differing from these.

And this is only about the homestead part. All explanations leave out the Vitualbox and Vagrant updates. Those are software packages (.dmg for mac). Can I assume updating is done by running those installers? Will they overwrite the previous versions?

If anybody can help me out here that'd be appreciated!