3,590 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.
Replied to How To Conditionally Add Query In Eloquent?
What about convert your query to an Eloquent query and use the when function.
Replied to Calculating Permalink For Each Reply, Without N+1 Problem
At first you say:
$post = Post::where('id', $post)->with('replies.author')->withCount('likes')->first();
And later on when you say:
$reply->post->permalink;
The $reply DOES NOT access the $post you define earlier backward. Its gonna run an extra DB query to the database to get an identical post.
So you got to use the solution given by @sinnbeck.
I recommend reading Illuminate\Database\Eloquent\Model::__get() and this eager loading explained video to get to know this further.
Replied to Laravel - How To Calculate The Sum Of Objects In An Array In Blade View
It would be a bit better if you extract the sum() function to a custom accessor.
class Transaction extends Model
{
public function getTotalAttribute()
{
return $this->options->sum(function ($option) {
return $option->pivot->price;
})
}
}
So later on in the view, you might simply say:
<h4>Options total: {{ $transaction->total }}</h4>
Commented on Inviting Users As A Feature Test
I don't know that I can write validation and authorization rules in so many ways.
Commented on Rendering Activity With Polymorphism
Maybe at some later stage you would change $project->activity to $project->activities.
Commented on Sometimes Validation With Form Requests
at 9:31 I immediately thought about tap function. And you did it anyway.
Commented on Improve Test Arrangements With Factory Classes
Excellent lesson! I just wonder if there is an alternative name for ProjectFactory. So that will not be confused with the ProjectFactory file to create an instance of Project.
Replied to LatestActivity Conditional
Is that the outcome that you want?
Also, do you name your model 'Tickets'? Ticket could be a better name.
Replied to Laravel Feature Test
Can you post the controller code as well?
There's nothing wrong with status code 302, its a redirect response. You may want to change the assertions to something like this:
$response->assertStatus(302)
->assertViewHas('settings')
->assertViewIs('setting::admin.index');
Replied to Laravel Feature Test
Does it say any errors when you uncomment $this->withoutExceptionHandling()?
Replied to LatestActivity Conditional
Inside the Ticket model, let's define a lastReply relationship:
class Ticket
{
public function lastReply()
{
return $this->hasOne(Reply::class)->latest();
}
}
Then get the tickets.
$tickets = Ticket::whereHas('lastReply', function ($query) {
$query->where('user_id', '!=', auth()->user());
});
Replied to Complex Eloquent / SQL Request, Need Backup
What about this:
$authors = Author::whereHas('books.pages', function ($query) {
$query->where('published', true);
});
Replied to Assign Value To Data Property From A Method In Vuejs
It's good that you are able to solve your own issue. I think that event handling thing got to be registered inside the CREATED or MOUNTED hooks.
export default {
mounted() {
grecaptcha.ready(() => {
grecaptcha.execute('6LeawrcUAAAAAIrA-LQ-kytjPFEBcedXDLcWHHHM', {action:
'homepage'}).then((token) => {
this.RegForm.token = token;
});
});
}
}
Replied to Question Regarding Best Practice For Controller Methods
Checkout this talk . This is the best guide for making controllers! Maybe this is what you are looking for.
Replied to When Running Phpunit Test Following Error Occurs In Building Laravel App With TDD Episode 5
Can you show the code for a_user_can_create_a_project test case?
Replied to Phpunit: AssertArrayHasKey() - Undefined Index: Errors
Instead of creating your own function, what about using $response->assertSessionHasErrors();
Replied to Refactoring An Sql Sentence To Eloquent Style
As I understand, it would be more performant if we handle everything in one query.
Replied to Refactoring An Sql Sentence To Eloquent Style
Good point. I have updated my answer accordingly.
Replied to Refactoring An Sql Sentence To Eloquent Style
I assume that you have a Brand model, you might rewrite the query to something like:
$brands = Brand::select('brands.id', 'brands.name')
->join('products', 'products.brand_id', '=', 'brands.id')
->join('opportunities', 'opportunities.product_id', '=', 'products.id')
->where('opportunities.status', 1)
->groupBy('brands.name')
->get();
An alternative is to use whereHas and relationships. From your query I assume that a brand has many products and a product has many opportunities:
$brands = Brand::select('brands.id', 'brands.name')
->whereHas('products.opportunities', function ($query) {
$query->where('status', 1);
})->get();
Commented on Object-Oriented Forms: Part 1
Sorry, I didn't hear Jeffrey said that its not gonna work for more complex form.
Commented on Object-Oriented Forms: Part 1
Good lesson! But @keydown is not gonna work with some form elements, such as select. Is there any other event that can be used for all form elements?
Replied to Getting HasOne Relationship
Cool. Just a note on that, you may say:
optional($user->credit)->amount;
So even if a user has no credit, it still return null and don’t raise any error.
Replied to Getting HasOne Relationship
Because you define a HasOne relationship so I think credit() could be a better function name.
public function credit()
{
return $this->hasOne(Credit::class);
}
If you call the credit() function, you are gonna have a Illuminate\Database\Eloquent\Relations\HasOne object. If you want to access the credit model, pls say:
$user->credit->amount;
Replied to Groupby And Sum The Total_amount Of Same Suppliers
Interesting. Because we paginate the results, so $suppliers is an instance of Illuminate\Pagination\LengthAwarePaginator. Therefore, we are not able to use Higher Order Messages directly.
I have updated my answer slightly to consider that.
Replied to FirstOrCreate On Multiple Related Records From XML Feed
So what data format did you have after using Rodenastyle/stream-parser?
Replied to Pass Array From Child To Parent
That looks a bit complicated. What about storing products in the WorkSheet component and then pass it down to the ProductCart components?
// WorkSheet.vue
<template>
<div>
<ProductCart :products="products"/>
</div>
</template>
<script>
import ProductCart from './ProductCart'
export default {
components: {
ProductCart
},
data() {
return {
products: []
}
}
}
</script>
So the WorkSheet component already got the products in the beginning.
Replied to Call Controller Function From Another Controller
I am not sure what your question is but it may not necessary to do so. Laravel supports heaps of redirecting mechanisms, including this redirecting to actions.
Replied to Call Model From Associate Table
This is a feature named Higher Order Message.
So the full form is gonna be:
$totalAmountDue = $child->user->payments->sum(function ($payment) use ($userId) {
return $payment->usrAmountdue($child->user->id);
})
if ($totalAmountDue == 0) {
....
}
And of course, with higher order message, it become much simpler:
if ($child->user->payments->sum->usrAmountdue($userId) == 0) {
...
}
Replied to Call Model From Associate Table
If I understand your question correctly, what you want may be something like this:
if ($child->user->payments->sum->usrAmountdue($userId) == 0) {
...
}
Replied to Groupby And Sum The Total_amount Of Same Suppliers
@abdulbazith That's an interesting point. Payments for a supplier could have different due dates, so it's not possible to group by suppliers. I have updated my answer above, I basically put all due dates in one cell.
Replied to Redirecting To Another Route Based On Condition Of A Model Attribute
You might simply use redirect
Replied to Groupby And Sum The Total_amount Of Same Suppliers
For better naming, I renamed the relationship from purchasetostore to payments.
public function payments()
{
return $this->hasMany(PurchaseToStore::class, 'supplier_id');
}
We could make a query starting from the Supplier model, not the PurchaseToStore one.
$suppliers = Supplier::whereHas('payments', function ($query) use ($fromDate, $toDate) {
$query->whereBetween('due_date', [$fromDate, $toDate]);
})
->with(['payments' => function () use ($fromDate, $toDate) {
$query->whereBetween('due_date', [$fromDate, $toDate]);
}])
->paginate(50);
From the view (I assume that you use timestamp for the due_date field):
<thead>
<tr>
<th>S.No</th>
<th>Supplier</th>
<th>Due Dates</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
@foreach($suppliers as $supplier)
<tr>
<td>{{$supplier->id}}</td>
<td>{{$supplier->name}}</td>
<td>{{
$supplier->payments
->pluck('due_date')
->map
->format('d/m/Y')
->unique()
->implode(', ')
}}</td>
<td>{{$supplier->payments->sum('amount')}}</td>
</tr>
@endforeach
</tbody>
<tfoot>
<tr>
<td colspan="4">Total: ${{$suppliers->getCollection()->map->payments->collapse()->sum('amount')}}</td>
</tr>
</tfoot>
Replied to Help Me Refactor This Code Its A Bit Lengthy
Is it alright to make the order field a sequential value? If so, we can make that auto incremented in the migration file:
Schema::create('course_items', function (Blueprint $table) {
// ...
$table->signedInteger('order')->autoIncrement();
// ...
});
Please let me know @successdav so I can update my answer accordingly.
Replied to PDF With QrCode
I did the same task before and those are the two packages that I used:
Replied to Grouping By Pivot Table Columns
You might try this:
$result = $thread->tags->groupBy(function ($tags) {
return $tag->pivot->tag_type;
});
Replied to How To Add Extra Values In Every Result?
This would be a perfect case for accessor:
https://laravel.com/docs/5.8/eloquent-mutators#accessors-and-mutators
You could do something like:
use Illuminate\Support\Str;
public function getTempCodeAttribute($value)
{
return "$value-" . Str::random(4);
}