Member Since 5 Years Ago
2,780 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 Is There A Way To Have Both The V-model And Value?
Hi @ifrit you have a loop for products v-for="product in products"
but try to assign every product.name to the same datav-model="productName"
? Something goes completely wrong in your case. Try to describe more details what you want to do
I see you are trying to get summary for product, but - for which one? products array can contains many different products
Replied to How To Automatically Add A Row Number
Hi @glitter grace try something like
<tr v-for="(product, index) in products" >
<td>{{ index + 1 }}</td>
<td>{{ product.name }}</td>
</tr>
Replied to Many To Many How To Salve?
Hi @wgerenutti please pay attention to this section of documentation. Save data for belongsToMany
relations works different way then others
Replied to How To Validate Request Using Ajax?
@nickywan123 let's check what you have in request
public function update(Request $request,Reply $reply){
dd($request->all());
// ...
}
Replied to Count(): Parameter Must Be An Array Or An Object That Implements Countable On Laravel Relationship
@simonangatia fisrt of all, you have N + 1
problem, and need to read about eager loading
And - you example do not tells anything. Please, can you show your Forum
model?
Replied to Eloquent Condition Inside Nested With()
@mansoorkhan8 that'ss great. Please consider to making response as best answer to mark conversation as "Solved"
Replied to Query As A Function Inside Model For Reusing
I prefer to use dependency injections (it means - via contructor)
Awarded Best Reply on Query As A Function Inside Model For Reusing
Hi @ramniksingh first of all, query builder have pluck
method, so, you can just call in controller
$sections = Section::pluck('sname', 'id');
now your questions
Section::pluck('sname', 'id')
- it's ok to repeatModel
directly (if is small application)
b. You can use Repository layer
c. You can create separated Query classReplied to Query As A Function Inside Model For Reusing
@ramniksingh
3.a. You can inject Model via __construct
, for example
//controller
protected Section $section;
public function __construct(Section $section)
{
$this->section = $section;
}
public function yourMethod()
{
$sections = $this->section->yourComplexQuery();
}
3.b. For a large projects you Model will grows extremely quick if you'll keep everything (include all the methods) in your model. This way it make sence to move queries in separated layer (repository or queries class)
Replied to Clearing Cache
Hi @ifrit possible something like Redis::eval('flushdb')
will do the trick
Replied to Eloquent Condition Inside Nested With()
@mansoorkhan8 I't not possible with default laravel relations, but you can look to this package, looks like it's what you need
Replied to Query As A Function Inside Model For Reusing
Hi @ramniksingh first of all, query builder have pluck
method, so, you can just call in controller
$sections = Section::pluck('sname', 'id');
now your questions
Section::pluck('sname', 'id')
- it's ok to repeatModel
directly (if is small application)
b. You can use Repository layer
c. You can create separated Query classReplied to Count(): Parameter Must Be An Array Or An Object That Implements Countable On Laravel Relationship
@simonangatia and ideally - show Films model entirely
Replied to Count(): Parameter Must Be An Array Or An Object That Implements Countable On Laravel Relationship
@simonangatia show you query
Awarded Best Reply on Get All Films Which Has One Or More Genre
@hovo possible mistake, you call whereHas
on coutry
instead of genre
Replied to Get All Films Which Has One Or More Genre
@hovo this way I think you example should work. Do you have any problems with it?
Replied to Get All Films Which Has One Or More Genre
@hovo really it's based on your relation and search params. Please show you genre
relation on films model, and what the value you have in $request->genre
? id of genre?
Replied to Authorization Using Different Policies
@nickywan123 it's convention - using police method with the same name as performed action. Really (as Jeffrey love to says in his video) - who cares? Sometimes (if I have the same authorization logic for all performed actions) I create policy method 'access', so, it make more sence where you check if user has access
to model at all, like so
$this->authorize('access', $post);
About using via controller method or model - it's up to you. If your logic is just to abort unauthorized requests - autthorize
helper looks more clean. If you want to implement custom logic per action (like flash specific message , redirect to conditional page) you can call it via model to have more flexibility in response processing
Awarded Best Reply on Why UploadFile Function Is Not Working ?
@santoshdc as I see, your function uploadFIle
doesn't have return
statement, but you expect file_name
as the result of the function in controller to set file name
Replied to Count(): Parameter Must Be An Array Or An Object That Implements Countable On Laravel Relationship
@simonangatia can dump $forum->topics
right after grabbing from database?
Replied to Get All Films Which Has One Or More Genre
@hovo possible mistake, you call whereHas
on coutry
instead of genre
Replied to Getting Index Of Posts From Channels User Is Subscribed To
@thebigk it calls whereHas
of each relation in sequence - step by step.
Awarded Best Reply on Getting Index Of Posts From Channels User Is Subscribed To
Hi @thebigk try something like
Channel::whereHas('subscribers', function ($query) {
$query->where('user_id', auth()->user()->id);
})
->with('posts')
->get();
or if you need to sort (or paginate) by posts
Post::whereHas('channel.subscribers' => function ($query) {
$query->where('user_id', auth()->user()->id);
})
->with('channel')
->get();
Replied to Getting Index Of Posts From Channels User Is Subscribed To
Hi @thebigk try something like
Channel::whereHas('subscribers', function ($query) {
$query->where('user_id', auth()->user()->id);
})
->with('posts')
->get();
or if you need to sort (or paginate) by posts
Post::whereHas('channel.subscribers' => function ($query) {
$query->where('user_id', auth()->user()->id);
})
->with('channel')
->get();
Replied to Count(): Parameter Must Be An Array Or An Object That Implements Countable On Laravel Relationship
@simonangatia possible you have accessor like getTopicsAttribute
on your forum
model?
Awarded Best Reply on Ensure Only Rightful Owner Can Edit A Thread.
@nickywan123 it's authorization in blade, but in controller
public function edit($channel,Thread $thread)
{
$this->authorize('update', $thread);
// authorized
}
Replied to Ensure Only Rightful Owner Can Edit A Thread.
@nickywan123 it's authorization in blade, but in controller
public function edit($channel,Thread $thread)
{
$this->authorize('update', $thread);
// authorized
}
Replied to Count(): Parameter Must Be An Array Or An Object That Implements Countable On Laravel Relationship
@simonangatia hasMany
relation returns Collection
instance. why just not use
{{ $forum->topics->count() }}
If problem still appears, try to dd
what you have
// in your blade
@dd($forum->topics)
Replied to Ensure Only Rightful Owner Can Edit A Thread.
Hi @nickywan123 it's great, that you created the policy, but did you call the check? Please, look here to understand how to authorize using policies
Replied to Php Artisan Migrate
@asadali007 I mean - are you sure your mysql root user doesn't have password?
Replied to Php Artisan Migrate
@asadali007 the problem is that you can't connect to database - permission denied (as in message). Are you sure your credentials are correct? Are you able to connect to mysql via terminal using this credentials?
Replied to Why UploadFile Function Is Not Working ?
@santoshdc as I see, your function uploadFIle
doesn't have return
statement, but you expect file_name
as the result of the function in controller to set file name
Replied to How To Prevent Duplicate In Loop
Hi @david001 if you just need to output child categories without products data - @snapey is right, grabchild categories filtered usingwhereHas
for product from database
If you displays products on this page and just want to have list of child categories on the same page, you can do something like:
@foreach($products->unique(fn ($p) => $p->childcategory->name) as $product)
<p>
{{($product->childcategory->name)}}
</p>
@endforeach
it should prevent duplication
Replied to Why UploadFile Function Is Not Working ?
Hi @santoshdc your problem is here
$file->save($path."/".$file_name);
$file
is uploaded file object, not the model. To store in database you should set value for related model
Replied to Best Way To Create Some Kind Of Audit Records
Hi @boubou I use laravel auditing package to log any actions. Then you can just filter it by user to get recent activities per user.
Replied to Laravel Mass Update Using Foreach Loop
@mozziehin as I see you don't have any unique identifier of variant in data, so, you can map any existing Variant to the one in your data array.
I can see 2 ways:
id
column value, for example) with the data, so you can be able to find correct variant to updateReplied to Retrive Collection Item, Change It And Prepend It Back Into Original Collection
Hi @guntarv by default eloqent returns collection, so, what's the the problem to just append new one?
$options = Option::all();
$newOption = Option::create([
'crop' => 'value1',
'variety' => 'value2',
])
$options->push($newOption);
// or prepend
$options->prepend($newOption);
Replied to Laravel Check Box To Textbox?
Hi @davehetfield can you explain little more? Hard to understand what you need
Replied to Laravel Mass Update Using Foreach Loop
Hi @mozziehin you have a lot of things here
First one - you define Variation $products
and
$products = $products::query()->where('product_id', $prod_id)->get();
with the same name, so, on the second loop you'll get an error
second one: you get product on each loop, instead of getting it one time before
third: look at sync method instead of detach and attach
fourth: this step
$products->fill(Arr::only($product, $products->getFillable()));
make no sence, laravel automatically will skip all non-fillable attributes
By the way: possible we have wrong understanding of your relations. Product has many variants? can you show us relations between Product model, variants model, what is options for variant, and ideally how you $data
array looks like
Replied to Cron Job To Wget Page That Loads Info From Google Sheets
Hi @ftored I can recommend to you to look at this package to manage your cron
Awarded Best Reply on Redirecting After Login
Hi @ifrit try
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function authenticated(Request $request, $user)
{
if ($user->role == 'admin') {
return redirect(route('home'));
}
return redirect(route('customer'));
}
}
Replied to Redirecting After Login
Hi @ifrit try
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
protected function authenticated(Request $request, $user)
{
if ($user->role == 'admin') {
return redirect(route('home'));
}
return redirect(route('customer'));
}
}
Awarded Best Reply on How To Get Abbr From DAYNAME Function In Sql
Hi @lgendary you can use DATE_FORMAT function like so:
public function getLoggedInUser()
{
return Session::selectRaw('DATE_FORMAT(created_at, "%a") as date, COUNT(*) as total')
->where('created_at', '>', Carbon::now()->subWeek())
->groupBy('date')
->oldest()
->get();
}
Replied to How To Get Abbr From DAYNAME Function In Sql
Hi @lgendary you can use DATE_FORMAT function like so:
public function getLoggedInUser()
{
return Session::selectRaw('DATE_FORMAT(created_at, "%a") as date, COUNT(*) as total')
->where('created_at', '>', Carbon::now()->subWeek())
->groupBy('date')
->oldest()
->get();
}
Replied to Blade Double Underscore
@froedrick double underscore is translation of the text. So, if you want to use multiple languages - you'll be able to easily define translations for this text
By default (if no translation defined for the current language) it will displays text as it is
Replied to How To Go Back To Active Accordion On Page Refresh
Hi @noblemfd bootstrap uses class show
to detect which panel should be opened. So, you just need to pass the identifier which panel you want to show by default from your controller. For example, let's name it $panel
in your controller
return view('your_view', [
'panel' => 'second',
]);
and if your blade
<div id="collapseOne" class="panel-collapse collapse {{ $panel == 'first' ? 'show' : '' }}">
...
<div id="collapseTwo" class="panel-collapse collapse {{ $panel == 'second' ? 'show' : '' }}">
Awarded Best Reply on How Install Laravel-datatables-oracle Without Jquery ?
@mstdmstd your question sounds strange. yajra/laravel-datatables
- backend php package to handle server-side work of DataTables
plugin on frontend. alpine/tailwind are frontend js/css packages. So, what you are looking for? Replacement of Datatable plugin with alpine/tailwind?
Just try to google something like datatables alpine tailwind
and you will find suggestions like this one and much more