ctroms

Experience

55,770

43 Best Reply Awards

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

25th March, 2017

ctroms left a reply on User Types • 1 month ago

There was a discussion a few days ago in this thread with a few different ways of going about this. See if any of them give you a better idea on what you might want to do.

ctroms left a reply on Routes Problem With Controllers • 1 month ago

Hrm. Unfortunately, I don't have much experience with xampp so I don't know how much help I'm going to be able to provide.

Your .htaccess is the default that comes with Laravel so that should be ok. I did see in the installation docs a section that suggests trying a different .htaccess alternative if you have problems. Perhaps give that a try.

Also you can try to define your route without the name but as I mentioned before I don't see an issue with your current definition.

Route::get('/', [email protected]');

ctroms left a reply on Routes Problem With Controllers • 1 month ago

Does the /index appear if you use the closure too? Did you modify your htaccess at all? I don't see anything wrong with your route definitions.

In your virtual host definition try adding public to your directory path.

    <Directory "C:/xampp/htdocs/ProjectPtsi/public">

ctroms left a reply on Routes Problem With Controllers • 1 month ago

In your screenshot it looks like you are browsing to laravel.localhost:8080/index. Your route url for your index method is '/' or laravel.localhost:8080/. Also in your closure your view is spelled 'welcom' and in your route it is spelled 'welcome'.

20th March, 2017

ctroms left a reply on Multi User Type Model And Auth • 1 month ago

It sounds like regardless of which method you select, everyone so far is suggesting a single user model for authentication. The only variance in responses are how you relate the user profile data to the user account and all are perfectly valid. Based on what I think you are after from your comments, I agree with @snapey on how to structure the profiles.

ctroms left a reply on Multi User Type Model And Auth • 1 month ago

Just another thought which kind of a combination of your two suggestions...

Create account_types or profiles tables for each user type Student, Parent, Teacher/Personal.

Store all users in a single users table with the attributes needed for authentication (e.g. email, password, reset token, etc) and a two additional columns (profile_id, profile_type) to store a polymorphic relationship.

Then define a polymorphic relationship (maybe profiles) on your users model to the profile models.

When you query a user for simple authentication, you most likely are not interested in the profile data (e.g. firstname, lastname, etc), just the email, password which would all be present on the user table.

When you want to check permissions for a user, you can check the profile_type column on the user table to determine the account type.

If you want all users that are teachers, students or parents, define scopes on your users model to check the profile_type column to handle that.

While relationships between two entities on the same table are totally valid, you can create your relationships between account types to their respective profile models. So the parent model has a relationship to the student model, etc.

I'm not suggesting that this is better or worse than any other ideas you have or may hear, just another alternative.

19th March, 2017

ctroms left a reply on Possible To View Dump Of Tinker History? • 1 month ago

Ha. No problem. It happens to us all. Have fun!

ctroms left a reply on Passing Array Value In Db • 1 month ago

No Problem. Have fun!

ctroms left a reply on Passing Array Value In Db • 1 month ago

Ah. I see you accepted an answer. I assume your issue is resolved?

ctroms left a reply on Passing Array Value In Db • 1 month ago

Your form has an input named question[] and one named answer[]

When your user submits the form, your store() method on your controller receives the request.

I'm referring to this controller method as defined in your your other post .

public function store(Request $request ,Question $question)

When you use the request helper request(question), an array is returned containing the values of all of the inputs on your from with the name "question[]".

Your form above only displays one input for question and one for answer but you would only append '[]' to your input name if you were intending on having multiple inputs named question.

Let me ask a different question, are you intending on having more than one question input and answer input on this form or will you only ever have one question input and one answer input. Maybe post your entire form including the opening '' and closing '' tags.

ctroms left a reply on Passing Array Value In Db • 1 month ago

@asadbinamer Sorry. I am just re-reading what you posted earlier and should have asked for further information before posting my previous reply.

Your question and answer form inputs are arrays so your controller is receiving all questions as an array and all answers as an array.

Assuming the AddQuestion class is your Eloquent model, are you trying to loop through each question and answer combination and add them as a single record or are you trying to add all questions from the questions field in the form to a questions column and all answers from the answers field in the form to an answers column of a single record.

ctroms left a reply on Passing Array Value In Db • 1 month ago

@asadbinamer Based on the migration you posted on this discussion

It looks like you have your question and answer attributes set as string column types. Try to change them to text or json if your DB supports it.

$table->text('question'); 
$table->text('answer'); 

Then in your AddQuestion model cast the question and answer attributes to array.

protected $casts = [
    'question' => 'array',
    'answer' => 'array',
];

ctroms left a reply on How To Pass Array Values In Db • 1 month ago

This seams to be a duplicate of this discussion.

@asadbinamer. you might want to close this discussion with a link to the above discussion as there is more activity on that discussion.

I'll add my reply on that thread.

ctroms left a reply on Passing Array Value In Db • 1 month ago

Hello All. This seams to be a duplicate of this discussion

Since I was the only one to reply to that discussion so far, here is my only relevant post from that thread.

@asadbinamer Please post your migration too. and how are you setting the > $question variable in your Model create method?

@asadbinamer I would suggest pasting a link to this post as the accepted answer in your other post so that you are receiving replies to one post. It will help everyone else stay on the same page and ultimately help you to have everyone working off of the same discussion thread.

ctroms left a reply on How To Pass Array Values In Db • 1 month ago

@asadbinamer Please post your migration too. and how are you setting the $question variable in your Model create method?

ctroms left a reply on How To Pass Array Values In Db • 1 month ago

Can you post any code?

ctroms left a reply on Best Practice For Easy Upgradeable Project Structure? • 1 month ago

All of the Laravel framework files stay in your vendor directory and the app directory is for application specific code.

Replacing the entire app folder wouldn't work as upgrades require changes to your code not just the files provided by Laravel.

Take the upgrade from 5.3 to 5.4 as an example.

Lets say in your 5.3 application, you have a Model that needs to return a date attribute as a Carbon instance with the date and time '2017-01-31 12:30:15'. In 5.3 you would have cast this attribute to date to achieve this. In 5.4 a change was made and now, to receive that attribute the same way, you need to change that cast to datetime since the the date cast now returns the Carbon object and calls the startOfDay method which effectively gives you "2017-01-31 00:00:00"

This model would work in a 5.3 installation but if you just dropped it into a brand new 5.4 installation without making that change, the exact same code would behave in two different ways in the different Laravel versions.

ctroms left a reply on Why Some Variable Are Not .enviable By Default? • 1 month ago

@boynet No problem!

You can also checkout tools like (Laravel Shift)[https://laravelshift.com/] to save time during upgrades.

ctroms left a reply on Why Some Variable Are Not .enviable By Default? • 1 month ago

I don't see any reason why you couldn't add the timezone to your .env if you wanted to. Just make sure you update your config/app.php

'timezone' => env('ENV_TIMEZONE', 'UTC'),

If I had to guess as to why it might not be there by default; .env files are not committed to your repo.

If the timezone is set on your local machine in .env and left as a default in your config/app.php, you might be developing with your app configured for one timezone and the production code configured for another.

This can negatively effect your application including logging and reporting especially if your logs are pulled to a central location.

For non secret things that you want to stay consistent across all developers machines you might just consider leaving them hard coded in config/app.php

18th March, 2017

ctroms started a new conversation Profile Participation History Doesn't Update With Reply Edits • 1 month ago

Hello all.

I noticed an issue with the thread participation history on the member profile page. When you create a reply to a thread, your reply is displayed in the participation history on your profile page. If you then make an edit to that reply, it appears as though the edit does not appear in the participation history on the original reply or as a new edit.

To summarize...

Issue

An edit to a thread reply does not appear in the participation history on the member profile page.

Steps to reproduce

  1. Create a reply to a thread
  2. View profile page, notice the reply in participation history
  3. Edit the reply
  4. View the profile page, notice the edit does not appear in the participation history.

This is far from a major issue but I though I'd mention it.

17th March, 2017

ctroms left a reply on Possible To View Dump Of Tinker History? • 1 month ago

Sure. It is in your Psy Shell history. Try:

~/.config/psysh/psysh_history

16th March, 2017

ctroms left a reply on Forcing Ignore Validation • 1 month ago

I assume you are referring to this section and more specifically the "Forcing A Unique Rule To Ignore A Given ID:" part of the docs?

In the example Taylor is ignoring the unique constraint for the email attribute on the users table if the id attribute (the primary key) is equal to the one provided.

In your case you want to ignore the unique constraint for the store_code attribute on the locations table when the id attribute (primary key) is equal to the one provided.

So assuming the $id passed to your controller method is the location id and that it is the primary key on your locations table you want to ignore, you might do something like this.

'store_code' => [
    'numeric',
    Rule::unique('locations')->ignore($id);
]

You'll need to also define a similar rule for the name attribute.

ctroms left a reply on Set A Default Value In The Code • 1 month ago

@mathishuettl I suppose you could set it in the constructor but your constructor would also have to accept an attributes array and pass it to the call to the parent constructor or you won't be able to set attributes through the models constructor anymore.

Personally I would go way of setting the $attributes property or Observers as @jekinney suggested.

ctroms left a reply on Set A Default Value In The Code • 1 month ago

You can override the attributes and default values on your model by updating the $attributes property. If your code updates these attributes, the default value will be overwritten.

$attributes = [
    'prop1' => 'defaultval1',
    'prop2' => 'defaultval2
];

15th March, 2017

ctroms left a reply on How To Delete Model Without A Primary Key? • 1 month ago

Eloquent can't delete a model without a primary key as it looks for a key name when delete is called. That is how it is able to delete the correct record.

Out of curisoty, Why do you not want a primary key on this table?

If you don't want the default auto incrementing id column you can make your email column or token column the primary key. As long as the data in that column is unique it can be a primary key.

14th March, 2017

ctroms left a reply on What Is Repositories? • 1 month ago

+1 for @SaeedPrez answer. A search here on Laracasts also returns several videos and other discussions. The Repositories Simplified laracast might be a good place to start too.

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

No problem. Have fun!

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

Great!

Your can_win_a_title test is asserting that the title relationship exists in the wrestlers titles.

Lets assume your wrester won the title then lost the title. Then you made some code change that broke the winTitle method. Your test would still pass since the title was won in the past and is present in the collection of titles.

Instead, try to target that specific title by checking that the wrestler title relationship exists and that the relationship has a won_on equal to today.

$this->assertNotNull($wrestler->titles()->where('title_id', $title->id)->whereDate('won_on', Carbon::today()->toDateString())->first());

This will return null and fail if the wrestler title record can't be found and if the won_on date is not equal to today.

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

Ah. Sorry. When you try to get an attribute off of a pivot table you need to call ->pivot->attribute. I forgot to add that.

Try this.

$this->assertNotNull($wrestler->titles()->where('title_id', $title->id)->whereDate('won_on', Carbon::today()->toDateString())->first()->pivot->lost_on);

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

in your test, after you call loseTitle(), What do you get when you run.

dd($wrestler->titles()->where('title_id', $title->id)->whereDate('won_on', Carbon::today()->toDateString())->first());

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

Hrm. Please post your current can_lose_a_title test one more time and your loseTitle method from your wrestler model.

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

To answer your second question. You don't have to change the date in this test . Your results will be the same whether the title is won and lost in the same day or won today and lost tomorrow.

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

Yea I think it should cover those scenarios. I was just pointing them out in the event you wanted to combine them into a single function.

Oops. I totally messed up that assertion. Sorry. It should be what you had earlier.

$this->assertNotNull($wrestler->titles()->where('title_id', $title->id)->whereDate('won_on', Carbon::today()->toDateString())->first()->lost_on);

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

OK.

Lets assume a wrestler retires and they are allowed to retain the title, then there is a new title match and a new wrestler wins the title. In this case your database would contain the retired wrestler title record with lost_on equal to null since they didn't actually lose it. You would also have a wrestler title record with lost_on equal to null for the new champion.

If a wrestler is striped of the title for any reason, their wrestler title record would contain the date they were striped of the title in the lost_on attribute. Until a new wrestler wins that title there would be no wrestler title record where the title was won and not lost.

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

OK. You are probably receiving the property of a non-object message because there are no records that match that query so ->first() is returning null. Meaning that either the title_id doesn’t match or the won_on date doesn’t match. When a record can not be found with the query, null is returned so you are trying to get the lost_on property of null which is a non-object.

In your test is looks like you are wining the title, then using setTestNow(Carbon::parse('+1 day')) to advance the date by 1 day then losing the title and finally running your assertion with the date still mocked to +1 day. In this case the title was won, the day advances 1 day and you are testing that the title was won today when in fact it was won yesterday. After you call loseTitle you’ll want to clear the date mock so you are back to the actual date today.

$wrestler->winTitle($title);
Carbon::setTestNow(Carbon::parse('+1 day'));
$wrestler->loseTitle($title);
Carbon::setTestNow();
$this->assertNotNull($wrestler->titles->where('title_id', $title->id)->pivot_lost_on);

As far as titles changing hands on the same day. That should not be an issue because when you call $wrestler->titles() you are only querying titles for the specified wrestler. It seams unlikely that the wrestler will win the same title twice in one day.

You can definitely wrap the the winTitle and loseTitle methods into one changeHands method or just combine them into the changeHands method but there are a few caveats.

First you might consider using Eloquent to query for the current title holder rather than passing that in directly. If not you’ll want to add a little validation to make sure that the old champion is in fact the current title holder.

Second, and I am thinking back to my childhood here so if this is incorrect my apologies, I seam to remember tag team matches where two wrestlers would win or lose the same title. If you had a changeHands method that accepted the a winning champion and losing champion, you would have to be able to accept multiple wining wrestlers and multiple losing wrestlers if you needed to support tag team matches.

Third, again thinking back, when a wrestler who holds a title retires and is not defeated, the don’t actually lose the title but the title can still be won in a new match with two new wrestlers. So when a title is won it is not necessarily lost by the previous champion.

13th March, 2017

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

No need. I copied the wrong response into the window and had to delete it. Sorry. Here is the response I intended.

Gotcha. In that case ignore my previous advice and consider this.

If a wrestler can win and lose the same title multiple of times, your pivot table may contain multiple relationships where the wrestler_id and the title_id are identical. If your code is going to update the lost_on attribute each time a title is lost you can assume that if the lost_on column is null the wrestler still holds that title.

I assume that your winTitle method sets the won_on timestamp to Carbon::now() or Carbon::today() in your test. I also assume that your wrestler can’t win the same title twice in one day. To get the specific title for you wrestler in your test, you can query for the wrestlers titles that were won on a specific date. Today in this case. Then test the lost_on attribute of that record not null. Note: You might consider updating this to check that the date of the lost_on attribute is what you expect rather than testing for a null value. That way you can make sure the date was set correctly

$this->assertNotNull($wrestler->titles()->where('title_id', $title->id)->whereDate('won', Carbon::today()->toDateString())->first()->lost_on);

Be careful with your loseTitle method on your model. The way it is currently setup, I think you are going to update the lost_on column on every relationship that matches that title id. If the wrestler lost the title before you are going to update the lost_on time to Carbon::now().

Similar to above you only want to update the lost_on column of the record where lost_on is currently null. You can filter relationships via intermediate table columns using the Eloquent's wherePivot method.

Try this:

$this->titles()->wherePivot('lost_on', null)->updateExistingPivot($title->id, ['lost_on' => Carbon::now()]);

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

Gotcha. In that case ignore my previous advice and consider this.

If a wrestler can win and lose the same title multiple of times, your pivot table may contain multiple relationships where the wrestler_id and the title_id are identical. If your code is going to update the lost_on attribute each time a title is lost you can assume that if the lost_on column is null the wrestler still holds that title.

In that case you want to search for all of the titles where the the title id matches the title you are looking for and where the lost_on column is null. This should only ever return 1 result.

This query should accomplish that for your test.

$this->assertNotNull($wrestler->titles()->where('title_id', $title->id)->whereNull('lost_on')->first()->lost_on)

Be careful with your loseTitle method on your model. The way it is currently setup, I think you are going to update the lost_on column on every relationship that matches that title id. If the wrestler lost the title before you are going to update the lost_on time to Carbon::now().

Similar to above you only want to update the lost_on column of the record where lost_on is currently null. You can filter relationships via intermediate table columns using the Eloquent's wherePivot method.

Try this:

$this->titles()->wherePivot('lost_on', null)->updateExistingPivot($title->id, ['lost_on' => Carbon::now()]);

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

@xtremer360 Assuming you are using database transactions in your tests, you are only creating 1 wrestler and 1 title so the first and last title will be the same.

If a wrestler has multiple titles, I would not rely on last() to give you the specific title you are after. You will want to find that title specifically with something like:

$wrestler->titles()->where('id', $title->id)

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

@xtremer360 Assuming you are using database transactions in your tests, you are only creating 1 wrestler and 1 title so the first and last title will be the same.

If a wrestler has multiple titles, I would not rely on last() to give you the specific title you are after. You will want to find that title specifically with something like:

$wrestler->titles()->where('id', $title->id)

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 1 month ago

You can assert a value is not null with assertNotNull()

8th March, 2017

ctroms left a reply on Cant Remember Using 'scope' Before A Function Name • 1 month ago

Awesome! Happy to help.

ctroms left a reply on Host Laravel 5.3 Project With My Current Website • 1 month ago

Eric Barnes just posted a tutorial at laravel-news.com a few days ago talking about this and a bit of security as well. Maybe that will help you.

https://laravel-news.com/subfolder-install

ctroms left a reply on Cant Remember Using 'scope' Before A Function Name • 1 month ago

Prefixing your function with "scope" allows you to create a local query scope. It is basically abstracts the eloquent where constraint behind a method on your model.

Lets say you have an Orders model and you want all orders that are completed. You might use a query that looks something like this...

Orders:where('status', 'Completed')->get();

If you were to create a local scope on your Order model...

scopeCompleted($query) {
    return $query->where('status', 'Completed');
}

You can query completed orders like this...

Orders:completed()->get();

Check out the docs for more info on Eloquent's local scopes

7th March, 2017

ctroms left a reply on Deleted Views File • 1 month ago

Did you ever commit it?

6th March, 2017

ctroms left a reply on Laravel 5 In Subdirectory • 1 month ago

Eric Barnes just posted a tutorial at laravel-news.com a few days ago talking about this and a bit of security as well. Maybe that will help you.

https://laravel-news.com/subfolder-install

13th January, 2017

ctroms left a reply on Can't See Who Liked A Comment In Forum • 3 months ago

I liked it too and also used it the way @SaeedPrez mentioned. When other devs that I have come to respect through various interactions at Laracasts agree with one of my responses, it is nice to be able to reassure myself that at least one of my ideas isn't completely off the wall.

ctroms left a reply on Is It Worth Testing Really Small Wrapper Methods • 3 months ago

I would argue that determining if your user is active or not is a critical feature to an application. That single method is likely used in middleware, filtering queries and probably some other things. I would want to have test coverage for that. With the test not depending on a database and being only 2 lines of code, there is much more to gain than to lose here IMO.

As you said, it may become more complex down the road. The fact that you are thinking this method might change is another reason why I would test it. I would want to know that any refactoring I do doesn't break this method.

4th January, 2017

ctroms left a reply on What Should I Learn First? I Need Your Help • 3 months ago

In the main navigation you'll find the skills section. This is a list of video series curated by Jeffrey that are a good path through a specific skill set.

You can start with PHP, then move on to Laravel.

I would suggest watching all of the video's but at minimum watch PHP Practitioner and Object Orented Boot Camp before moving on to Laravel videos.

Beyond that, all of the content on Laracasts is really good and will help you hone your skills if you are willing to put in the work.

1st January, 2017

ctroms left a reply on Laravel Datatables Yajra Lib , Add Column • 3 months ago

@lifesound No problem. Have fun!

ctroms left a reply on How To Read Post Value, Verify And Insert Into Db? • 3 months ago

At the end of your controller method after you have done your insert, you can flash a message back to your user.

Here is a link to the docs on flashing data.

Jeffrey also did a video on this in the Building Project Flyer series. It is a little older but still will help you i think.

Edit Your Profile
Update

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