Member Since 5 Months Ago
4,110 experience to go until the next level!
In case you were wondering, you earn Laracasts experience when you:
Earned once you have completed your first Laracasts lesson.
Earned once you have earned your first 1000 experience points.
Earned when you have been with Laracasts for 1 year.
Earned when you have been with Laracasts for 2 years.
Earned when you have been with Laracasts for 3 years.
Earned when you have been with Laracasts for 4 years.
Earned when you have been with Laracasts for 5 years.
Earned when at least one Laracasts series has been fully completed.
Earned after your first post on the Laracasts forum.
Earned once 100 Laracasts lessons have been completed.
Earned once you receive your first "Best Reply" award on the Laracasts forum.
Earned if you are a paying Laracasts subscriber.
Earned if you have a lifetime subscription to Laracasts.
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.
Earned once you have achieved 500 forum replies.
Earned once your experience points passes 100,000.
Earned once your experience points hits 10,000.
Earned once 1000 Laracasts lessons have been completed.
Earned once your "Best Reply" award count is 100 or more.
Earned once your experience points passes 1 million.
Earned once your experience points ranks in the top 50 of all Laracasts users.
Earned once your experience points ranks in the top 10 of all Laracasts users.
Started a new Conversation How To Update And Insert Database Data When Releasing A New Application's Version?
Using Laravel 8 with jetstream.
I need to update and insert some records into the database when releasing a new application version.
I could just create a migration to update the data without changing the structure, but I am not sure if this is the best practice on a Laravel environment.
Replied to Build URL From Route And Parameters
Ummm, maybe I wasn't very clear.
Let's suppose I have a string in route format, like
$r = '/test/{FirstTest}/step/{StepNo}';
And a have an array with it's parameters, like
$p = ['mytest', 10];
Does exist some build in method or function to replace each parameter {}
in string $r
with its respective values in $p
, resulting in a string, like
$url = '/test/mytest/step/10';
I mean, I not necessarily have this route defined, and I don't need to define it. I just need the resulting URL.
Well, if a function to do this not exists, I will just do a preg_replace. Thanks.
Started a new Conversation Build URL From Route And Parameters
E.g., I have some route (a string): '/test/{FirstTest}/step/{StepNo}'
And I have an array with its respective parameters: ['mytest', 10]
No I need the resulting URL from this, like:
'/test/mytest/step/10'
Laravel has some builtin method to do this or I need to write my own? I am assuming this already exists, but I could not find it on docs.
Thanks.
Started a new Conversation How To Change Current User Password Without Logout Him?
Using Laravel 8 (with Jetstream).
I am updating the current authenticated user password like below. It works, but the user is automatically logout.
auth()->user()->password = Hash::make(request('new_password'));
auth()->user()->save();
I know the security implications of letting him logged in but... it's possible?
Thanks.
Started a new Conversation How To Get The Foreign Value Of A Relationship Using Eloquent Model::where() ?
How to query the Email model with specific fields AND the name of the user (in another parent table), using one call to Model::where()
like this:
$emails = Email::where('user_id', 1)->get(['id','user_id','email']);
<- HOW TO ADD THE USER NAME TO THIS LIST?
I know I can retrieve the user name of individual records (using "belongsTo", mentioned below) like $emails[0]->user->name
, but I am suposing that exists some way to do this without a loop.
Parent table:
user
id
name
Child table:
email
id
user_id
email
On my Email
model I have:
public function user()
{
return $this->belongsTo('App\Models\User', 'user_id', 'id');
}
Started a new Conversation How To NOT Return To Previous Route When Session Is Lost (Laravel 8/Jetstream).
Hi. I am using Laravel 8 with Jetstream (god, why) for the first time.
When session is lost and I authenticate again, I return to the route I were before the session was lost. What I want is to avoid this behaviour. After session lost and log in again, I need to be sent to a specific route (like '/', or HOME, for instance).
Replied to How To Execute Arbitrary Code ONCE, Only After The User Logs In.
I implemented your solution and it worked flawlessly. I will left here how I made it in case someone in the future needs it:
First I discovered that Laravel already have an event called Illuminate\Auth\Events\Login
. This event only happens when the user successfully authenticates.
So I create an event listener like this:
$ php artisan make:listener UserLoggedInListener --event='Illuminate\Auth\Events\Login'
Then I registered the event and my listener on EventServiceProvider.php
like this:
protected $listen = [
Login::class => [
UserLoggedInListener::class,
]
];
And inside my listener UserLoggedInListener.php
I put:
public function handle(Login $event)
{
session(['my_variable' => 'my_value']);
}
Now I can access this variable anywhere the session is available: session()->get('my_variable')
.
Thank you very much.
Replied to How To Execute Arbitrary Code ONCE, Only After The User Logs In.
Thank you. I forgot to mention I am using the new laravel jetstream for authentication. Description updated.
Replied to How To Execute Arbitrary Code ONCE, Only After The User Logs In.
The session example isn't important. I just need to run something ONCE, just after the user logs in.
Started a new Conversation How To Execute Arbitrary Code ONCE, Only After The User Logs In.
I need to store a variable into the session after the user logs in. The point is: How to do this once?
Currently I am setting this variable value when the user hits the home page, but when the user return to home page the variable is set again, overriding previous value. I can't put this in config because it's a calculated value.
I don't want to check if this variable exist in session every time the user hits the home page (the first route).
Started a new Conversation Using Laravel's Eloquent Models, How Do I Get A Plain Left Join Result?
I want to avoid accessing the tables directly using the DB class.
Let's supose I have these 2 related tables.
user
id
name
age
email
id
user_id
email
I created a hasMany emails
method on User
model, so I can get the emails like: User::find(1)->emails
. That's ok and it's working.
But what I really need in this example is to get some users and his respective emails in a plain collection or array, and I need the users that haven't emails too.
I could do User::where('age', 30)->get()
, iterate over it and get the User::emails
, but this approach isn't nice.
How to achieve this result, using Eloquent models, in a single list, like:
select
u.name,
u.id as user_id,
e.id as email_id,
e.email
from user u
left join email e on u.id = e.user_id and u.age = 30
name user_id email_id email
Jack 1 1 [email protected]
Jack 1 2 [email protected]
Anne 2 3 [email protected]
John 3 null null
Karen 4 null null
So, in this example, I need all users with age 30, even if they haven't any emails. And I need the result in a plain list as this query would return me.
Thank you.
Started a new Conversation Traverse Models In Multiple Levels Using Laravel's Eloquent
I am using Laravel 8.
Suppose we have 3 tables. table_a
, table_b
and table_c
.
A simple 3 levels of tables. Table A has many B and table B has many C.
table_a
id
name
table_b
id
table_a_id
name
table_c
id
table_b_id
name
I configured in TableA
model a method tableB
pointing to TableB
model using hasMany. It works. I can retrieve the table_b
records just using TableA::find(1)->tableB
.
I configured in TableB
model a method tableC
pointing to TableC
model using hasMany. It works. I can retrieve the table_c
records just using TableB::find(1)->tableC
.
Can I retrieve in one step all table_c
records related to a table_a
record ?
e.g.
Something like TableA::find(1)->...
(i need the related records in table_c
)
Thank you.