Member Since 3 Years Ago
3,710 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 Undefined Variable Exception When Queuing Notifications
$totalSponsorshipAmt
has been removed from my code and the new variable that is not defined in the exception is now $totalSpent
. I have made the correction in my question to avoid the confusion.
Started a new Conversation Undefined Variable Exception When Queuing Notifications
Hi guys, I want to send a summary of the year's activities for active users. The summary will simply display stuff like no of items purchased, total amount etc. Due to the fact that I will be sending the notification to over 1k users, I have decided to use queues. Strangely the queues keep failing but when I do not queue, the notifications are delivered successfully ( tested using Mailhog from Homestead setup ).
Below is the loop in my schedule that fires the notification for each active user
foreach ($users as $user) {
if (...condition) {
$totalSpent = Purchase::where(...condition)->sum('amount');
$maxAmountSpent = Purchase::where(...condition)->max('total_amount');
$noOfPurchase = $user->purchase->where(...condition)->count();
$user->notify(new Summary($user, $totalSpent, $maxAmountSpent, $noOfPurchase));
}
}
The summary notification is like this
class Summary extends Notification implements ShouldQueue
{
use Queueable;
public $user;
public $totalSpent;
public $maxAmountSpent;
public $noOfPurchase;
public function __construct($user, $totalSpent, $maxAmountSpent, $noOfPurchase)
{
$this->user = $user;
$this->totalSpent = $totalSpent;
$this->maxAmountSpent = $maxAmountSpent;
$this->noOfPurchase = $noOfPurchase;
$this->delay(10);
}
public function toMail($notifiable)
{
return (new MailMessage)
->from(...)
->subject(...)
->view('mails.scorecard', [
'user' => $this->user,
'totalSpent' => $this->totalSpent,
'maxAmountSpent' => $this->maxAmountSpent,
'noOfPurchase' => $this->noOfPurchase,
]);
}
...
}
And the blade template consumes the variables
<table class="transaction-details">
<tr class="txn-row">
<td width="50%" class="half-column">
<p class="cell-title">
Total Amount
</p>
<p class="cell-value">
<span class="currency">NGN</span>
<span class="txn-amount">{{number_format($totalSpent,2)}}</span>
</p>
</td>
<td width="50%" class="half-column">
<p class="cell-title">
Maximum Amount
</p>
<p class="cell-value">
<span class="txn-amount">{{$maxAmountSpent}}</span>
</p>
</td>
</tr>
</table>
Again the issue is that without queuing the notification gets delivered successfully but fails when using queues then throws the exception error below
Undefined variable: totalSpent (View: /project-path/resources/views/mails/summary.blade.php)
How can I fix this error so I can use queues or is there a better way to achieve sending the notification to my 1k users?
@neilstee Thanks, that was exactly what I did to fix the issue back then.
Awarded Best Reply on Laravel Vendor Does Not Exist And Could Not Be Created On Mac
@ahmadmayahi you were right it was a permission issue. However because it was a work laptop and I didn't have access to the root user, I simply solved the issue by running sudo composer install
.
Sorry my response is coming late, but I hope this is helpful to others.
Replied to Laravel Vendor Does Not Exist And Could Not Be Created On Mac
@ahmadmayahi you were right it was a permission issue. However because it was a work laptop and I didn't have access to the root user, I simply solved the issue by running sudo composer install
.
Sorry my response is coming late, but I hope this is helpful to others.
@horsford did you ever find a solution for this? I am having a similar experience right now.
Replied to Changes To Laravel Resource Not Reflecting
I remembered to run php artisan migrate
on the staging and it ran successfully. I have also ran php artisan cache:clear
but surprisingly the new property is still not showing on the staging server.
Replied to Changes To Laravel Resource Not Reflecting
I am not using varnish if it's the varnish I am thinking of.
Started a new Conversation Changes To Laravel Resource Not Reflecting
Hey guys, I added a new column rollover
to an orders table then added the rollover
attribute to OrderResource
and nova Order
resource. On my development system, I am able to see the new attribute from postman so I have pushed the changes to a staging server.
However I can't see the rollover
attribute in the response coming from the staging server but I can see in on staging nova.
I have never experienced anything like this before, it looks like the resource was cached or something. Does anyone know how I can fix this sort of issue?
Started a new Conversation Laravel Vendor Does Not Exist And Could Not Be Created On Mac
I am making a switch from windows to mac, so I installed composer globally and pulled laravel project from github. When I run composer install
inside the the laravel project I keep getting a run time exception error
[RuntimeException] /Users/developer/mike/code/laravel/vendor does not exist and could not be created.
Please how do I resolve this so that I can install the project dependencies?
Replied to Unable To Run Migrations On Laravel Homestead
Interesting. So after posting the question I decided to try to restart mysql service, then ran the command php artisan migrate
again and I was able to migrate successfully.
Problem solved.
Started a new Conversation Unable To Run Migrations On Laravel Homestead
Hey guys, I just set up laravel homestead and loving every bit of what it offers but I have an issue with running migrations. I was able to create a migration file to add new columns to an existing table using php artisan make:migrate
command. But when I run php artisan migrate
, I get the feedback
Migrating: 2020_09_03_151654_add_columns_to_transfers_table
And nothing else after that. I even left it to run for over 10mins and it still doens't get migrated or throw an error.
Here's the migration file
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddColumnsToTransfersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('transfers', function (Blueprint $table) {
$table->integer('otp')->after('bank_code')->nullable();
$table->boolean('authorized')->after('otp')->nullable()->default('0');
$table->string('recipient_code')->after('authorized')->nullable();
$table->boolean('approval_required')->after('recipient_code')->nullable()->default('0');
$table->unsignedBigInteger('approved_by')->after('approval_required')->nullable();
$table->foreign('approved_by')->references('id')->on('users')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('transfers', function (Blueprint $table) {
//
});
}
}
The db connection is fine also since I am able to pull data from it with my controllers. What could be the reason for this?
Replied to Trait 'Spatie\Activitylog\Traits\LogsActivity' Not Found
That worked but I ended up with another error. This time I am not able to add a foreign key to the original table structure
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table
activity_log
add constraintactivity_log_user_id_foreign
foreign key (user_id
) referencesusers
(id
) on delete set null)
Thanks
Replied to Trait 'Spatie\Activitylog\Traits\LogsActivity' Not Found
Do I need to run composer install
considering that the project is currently running even though its staging? I did that on my development system though then pushed it a bitbucket repo that gets pulled into my staging. So should I run composer install on the staging?
Replied to Trait 'Spatie\Activitylog\Traits\LogsActivity' Not Found
I was able to install the package locally with no issues. I have pushed the code to my staging environment which is on a DigitalOcean droplet. It's on my staging that I am having this issue.
Started a new Conversation Trait 'Spatie\Activitylog\Traits\LogsActivity' Not Found
Hi, I am getting the Trait 'Spatie\Activitylog\Traits\LogsActivity' error when trying to run php artisan migrate
command.
The complete error is
PHP Fatal error: Trait 'Spatie\Activitylog\Traits\LogsActivity' not found in /path_to_project/app/User.php on line 29
Here's how I am using the Trait in my User model
namespace App;
// others
use Spatie\Permission\Traits\HasRoles;
use Spatie\Activitylog\Traits\LogsActivity;
class User extends Authenticatable
{
use HasApiTokens, Notifiable, HasRoles, LogsActivity; // line 29 in actual code
...
protected static $logName = 'User profile';
protected static $logAttributes = ['*'];
protected static $logOnlyDirty = true;
public function activityLog()
{
return $this->hasMany('App\ActivityLog');
}
// other stuff
In other to display user activities in Laravel Nova backend, I created a customer ActivityLog
model and copied the contents of Spatie\ActivityLog\Models\Activity
into it. The ActivityLog
model looks like this
<?php
namespace App;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Contracts\Activity as ActivityContract;
class ActivityLog extends Model implements ActivityContract
{
// copied contents from Spatie\Activitylog\Models\Activity here
}
I don't know why I still get this error even after running composer dum-autoload
. What have I done wrongly?
Replied to Schedule Task Exist Before Completing Task
Thanks for drawing my attention to eager loading the users at the initial query. Yes I checked before the first task ran and they did meet the condition.
Started a new Conversation Schedule Task Exist Before Completing Task
Hey guys, I have this weird scenario where a scheduled task exist before completing in a laravel 6 project.
My intention is to have a couple of rows updated in my database if certain conditions is met with this code
$schedule->call(function () {
// get due contributions
Contribution::where('status', 'due')->chunk(20, function ($dueContributions) {
foreach ($dueContributions as $contribution) {
// get contribution own
$user = $contribution->user;
// get usr wallet balance
$walletBalance = $this->getWalletBalance($user);
if ($walletBalance >= $contribution->contribution)
$this->autoWalletDebit($contribution, $user, $walletBalance);
}
});
Log::notice('Wallet auto contribution complete.');
})->everyFiveMinutes()->description('Automatic wallet thrift contribution.');
I noticed from my telescope that the task runs but ends almost immediately. So before running the first time, there were 56 rows that met this condition. After running the first time, only 16 rows were updated, the second time only 7 rows were updated.
I have no clue why this is the case or how else to make it run completely before exisitng. Can anyone tell me why or how to fix it?
Replied to Paginate While Returning Array Of Api Resource Objects To The Resource Collection
Worked for me, many thanks.
I simply chained response()->getData(true)
to my collection like this
ModelResource::collection($model)->response()->getData(true)