Member Since 1 Year Ago
4,140 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.
Replied to How To Limit Login Attempts Using A Custom Login Controller?
maybe you can reproduce from Laravel Breeze? see https://github.com/laravel/breeze/blob/1.x/stubs/App/Http/Requests/Auth/LoginRequest.php
Replied to Can I Create Separate User And Admin Login In Laravel ?
As ankush981 guidance, you can use different guards for separating the user group as your requirement. If you use this approach then you should separate the auth controllers for admin and common users, so you can check using Auth::guard('admin')->attempt()
as an example.
Replied to Laravel Breeze And Two Factor Authentication
The "LoginController" in Breeze is AuthenticatedSessionController, where you can find this method :
public function store(LoginRequest $request)
{
$request->authenticate();
$request->session()->regenerate();
return redirect(RouteServiceProvider::HOME);
}
So, I think you can put the two-factor auth code either in this store
method or in the LoginRequest
class.
Started a new Conversation Spatie\MediaLibrary\MediaCollections\Exceptions\FileDoesNotExist
I'm using Spatie media library, when I run the code below I got an error Spatie\MediaLibrary\MediaCollections\Exceptions\FileDoesNotExist
$item->addMediaFromDisk($media->getPath())
->toMediaCollection('images');
The error shown is from this line (vendor\spatie\laravel-medialibrary\src\MediaCollections\FileAdder.php) :
public function toMediaCollectionFromRemote(string $collectionName = 'default', string $diskName = ''): Media
{
$storage = Storage::disk($this->file->getDisk());
if (! $storage->exists($this->pathToFile)) { ------------> the error points to this line
throw FileDoesNotExist::create($this->pathToFile);
}
I dump $this->pathToFile and check the path and the file, it exists.
So, I continue to trace what is inside the exists method ($storage->exists()), and found out that $storage is an instance of Filesystem. I found a Laravel Filesystem class (vendor\laravel\framework\src\Illuminate\Filesystem\Filesystem.php), the exists method is :
public function exists($path)
{
return file_exists($path);
}
Since the path is correct and the file exists, I even test manually if file_exists(path)
returns correctly and it does return true. So I guess this is not the Filesystem that is being used by the package, ~~then I found vendor\spatie\laravel-medialibrary\src\MediaCollections\Filesystem.php, I see this is the filesystem that is being used in the package service provider, but unfortunately there is no exists
method!~~
UPDATE: Found out that the Filesystem class being used is vendor\laravel\framework\src\Illuminate\Filesystem\FilesystemAdapter.php which contains:
public function exists($path)
{
return $this->driver->has($path);
}
Replied to Query Builder Table Alias
Ha...got it working by using DB::raw and adding backticks:
DB::raw('select id from `dbname`.`othertable` where mytbl.aaa = bbb)
not sure why I just got this idea after posting here, not one hour before...
at least I can continue...
Thanks everyone!
Replied to Query Builder Table Alias
because that column belongs to the first table, without alias still got the same error (Unknown column) which is correct since the second table doesn't have that column.
Anyway, I want to use DB::raw
for the second sql, but the problem is I need to specify the database name of the table in the real case like this:
DB::table('dbname.othertable')->select('id')
change this to
DB::raw('select id from dbname.othertable')
doesn't work ...
Replied to Query Builder Table Alias
What error message are you getting without toSql?
Column not found: 1054 Unknown column 'mytbl.aaa' in 'where clause'
Replied to Query Builder Table Alias
tried to change it to value('id')
but still got the same error, it seems not recognizing the alias from mytable
since it's outside of the second DB::table
... I guess.
Started a new Conversation Query Builder Table Alias
I want to run this SQL (simplified than the real case):
UPDATE mytable mytbl
SET col = (SELECT id FROM othertable WHERE mytbl.aaa = bbb)
tried this using query builder
DB::table('mytable', 'mytbl')
->update([ 'col' => DB::table('othertable')->select('id')->whereRaw('mytbl.aaa = bbb')->first() ])
got QueryException: Column not found: 1054 Unknown column 'mytbl.aaa' in 'where clause'... (I have checked there is aaa
column in mytable
)
Any idea how to fix it?
Anyway, how to get the sql from query builder update
? tried to add ->toSql()
but got error Call to a member function toSql() on int.
Replied to Show A Page When Exception In The Middleware Occurs
Ohh okay, that's all I need. Thank you!
Started a new Conversation Show A Page When Exception In The Middleware Occurs
I'm using a package wherein the package there is a middleware like this:
public function handle($request, Closure $next)
{
try {
// calling a process
} catch(SpecificException $e) {
if (! is_null(static::$handleException)) {
$exceptionHandler = static::$handleException;
return $exceptionHandler($e, $request, $next);
}
throw $e;
}
}
It's simplified from the original but you got the idea, basically, we can set the callable static::$handleException
and the middleware will somehow call it in the return
statement.
I want to show a notification page to the user whenever the exception occurs, how to do that? Let's say I have a controller and action to show the page, I don't know how to redirect to my controller action from the middleware, or is there any other way?
Started a new Conversation Minify/Compress JS File?
I see my js file already >700KB when compiling in production mode (in dev mode 2MB). I think it's still big, I'm wondering if I'm not doing it correctly because when I see the compiled app.js, it is not somewhat obfuscated or compressed, for example I see a variable name still stay as it is.
In my webpack.mix.js:
mix.js('resources/js/app.js', 'public/js')
and I ran npm run production
.
I tried to use .extract()
but it's just separating the file to vendor.js, the total size is more or less the same.
Is there anything else to do?
Replied to Spatie Media Library - Hide Origin Image
Maybe you need to move it manually by listening to an event? https://spatie.be/docs/laravel-medialibrary/v9/advanced-usage/consuming-events
Replied to Get All Rows From Many To Many Relationship
Thanks all, especially @silencebringer . I'll try that.
Started a new Conversation Get All Rows From Many To Many Relationship
I have a table named subscriptions
that contains plan_id
attribute. plan_id
refers to a table named 'plans' (obviously) and plans
table has many to many relationships to categories
table.
So, a user can have many subscriptions, and may have many plans, and should have many categories too.
My question: How to get all categories that a user has (or is allowed to access)?
My query so far: (I hope it can be more simple)
$planIDs = $user->subscriptions->pluck('plan_id');
$plans = Plan::whereIn('id', $planIDs)->get();
$categories = $plans->categories; -> can not, because $plans is a collection
Do I have to iterate $plans
?
something like:
$plans->each(function($plan) {
... (get the plan category one by one)
})
Replied to How To Check If Checkbox Is Checked?
For checkbox input the important attributes are:
<input type="checkbox" name="cb_name" value="cb_val" checked="checked" />
When the form is submitted, the value of cb_name
will be cb_val
whatever its value if the checkbox is checked, if it is not checked then cb_val
will not even be set.
Now you can figure out where to put your if(isset(...))
code, instead of in the value
attribute it should be outside of it.
<input type="checkbox" name="cb_name" value="1" <?php if ($cb_name == 1) echo 'checked="checked"' ?> />
or if you're using laravel blade template:
<input type="checkbox" name="cb_name" value="1" @if($cb_name == 1) checked="checked" @endif />
Replied to Auth And Guest Middleware, How To Have Different Home Route?
Thanks all, I thought this was as easy as changing a property but after some thoughts I think I'll leave it redirecting to one page.
Replied to Auth And Guest Middleware, How To Have Different Home Route?
Thanks @jlrdw and @gregupton for your reply.
Ok, that should work when a user is logging in. What about a user who tries to visit a url which is not allowed for him. For example: a visitor who tries to enter member area url or a member tries to enter "/admin", as my understanding these should be intercepted by middleware and redirected to their "home" route, cmiiw.
Started a new Conversation Auth And Guest Middleware, How To Have Different Home Route?
In my app there are some user's roles, let's say visitor, member and admin. I want to have different home route for each role. AFAIK, the home route is defined in RouteServiceProvider
, there is only one, maybe I can define more than one but how to make it work with auth
or guest
middleware?
Replied to Evaluate The Result Of A Promise
Oh okay, I never knew we can return a Promise.reject directly. Always learn new ones. Thanks!
Replied to Evaluate The Result Of A Promise
Thanks @piljac1 for your suggestion. I was thinking so, but then I found that the Promise that has been caught will not be caught anymore (sorry I can't explain it more clear). In this case the axios
has the catch, so the error won't go into saveForm
's catch. Unless I missed something here.
Started a new Conversation Evaluate The Result Of A Promise
Hi,
I have this code (Vue methods)
saveForm() {
axios.post('/post', data)
.then(response => response.data) // completed normally
.catch(err) { console.log(err) } // error
},
submitForm() {
this.saveForm();
if (result of saveForm) {
// do something if saving was successful
} else {
// handle error
}
}
How can I evaluate the result of axios Promise?
Thanks!