Buy your loved one the ultimate gift. Lifetime gift certificates are $100 off.

ctroms

Experience

61,150

46 Best Reply Awards

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

1st December, 2017

ctroms left a reply on Local And Remote Database Connection • 1 week ago

If you don't specify a connection, the connection specified here will be used. If you are always going to specify a connection then this definition has less value.

You might want the default to reference whichever connection you use the most. Given how you describes your setup, I would probably leave mysql for the default since that is where all of your user input goes.

29th November, 2017

ctroms left a reply on Set Middleware For Individual Route From Controller? • 2 weeks ago

By the time the create method is called, the middleware has already run. To include the middleware in your controller you'll need to add it to the controllers constructor. You can use chain only method onto your middleware to apply it to a single method.

public function __construct() {
    $this->middleware('not-in-guild')->only('create');
}

Here is a reference to the docs for other controller middleware options.

ctroms left a reply on OAuth 2 Token Of MailChimp API Call • 2 weeks ago

I would suggest taking a look at Laravel Socialite and the mailchimp provider from Socialite Providers.

These two packages will handle all of the logic necessary to request, store and retrieve OAuth tokens for mailchimp.

Once you have them configured correctly, it is just a matter of retrieving, the OAuth token and using that to authenticate your API request rather than the Basic Authentication API token.

28th November, 2017

ctroms left a reply on Local And Remote Database Connection • 2 weeks ago

You'll have to define env variables for both connections and add both connections to your config/database.php configuration file. You might consider defining a prefix for each group of environment variable by connection type. Something like MYSQL_ mysql connection and SQLSVR_ for your sql server connection environment variables. Then you can use the `connection() method to select which connection you want to use for your query.

DB::connection('foo')->select(...);

Here is a Docs Reference for multiple database connections.

You'll also want to include the sqlsrv connection parameters in your code sample in the connections array of your database.php config file.

26th November, 2017

ctroms left a reply on Laravel 5.4 From Scratch: Eloquent 101 • 2 weeks ago

You'll want to change your query return from

$query->where('isComplete', 0)->get();

to

$query->where('isComplete', 0);

Remove the get().

When you append get() onto your query you are effectively executing the query and fetching the results. When you define a query scope this is normally not what you want. without get() on the end of your chain, you are going to get the query builder instance back. This will allow you to continue building your query after the scope has been applied.

20th August, 2017

ctroms left a reply on Why Can't You Run Updates On Models You Use Get() On? • 3 months ago

The get() method is like executing a SELECT query. When you call get() on an Eloquent model, an instance of Illuminate\Database\Eloquent\Collection is returned. The save() method is a method on the query builder.

If you want to fetch a model with specific columns only as you have done with get() above, you can pass an array as the second argument to the find method. By sure to include your primary key column(usually id) and any other columns you want.

$model = Model::find(1, ['id', 'field']);

Now you still have a query builder instance and can update the field attribute value and call save().

30th June, 2017

ctroms left a reply on Eloquent Dynamic Methods • 5 months ago

No problem :D

ctroms left a reply on Eloquent Dynamic Methods • 5 months ago

@wilburpowery Have a look here in the api docs.

If you look at the source of this method, you'll see a call to an addDynamic method that will answer your question.

It should come as no surprise that Taylor has really clear documentation right in the source that will help explain further.

25th March, 2017

ctroms left a reply on User Types • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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? • 8 months ago

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

ctroms left a reply on Passing Array Value In Db • 8 months ago

No Problem. Have fun!

ctroms left a reply on Passing Array Value In Db • 8 months ago

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

ctroms left a reply on Passing Array Value In Db • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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 • 8 months ago

Can you post any code?

ctroms left a reply on Best Practice For Easy Upgradeable Project Structure? • 8 months 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? • 8 months 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? • 8 months 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 • 8 months 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? • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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? • 8 months 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? • 8 months 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 • 8 months ago

No problem. Have fun!

ctroms left a reply on How To Test For A Not Null Value For A Object Property • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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 • 8 months 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 • 9 months 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 • 9 months 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 • 9 months 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 • 9 months 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 • 9 months 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 • 9 months ago

Awesome! Happy to help.

ctroms left a reply on Host Laravel 5.3 Project With My Current Website • 9 months 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

Edit Your Profile
Update

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