Member Since 4 Years Ago
2,770 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.
Awarded Best Reply on Getting Count Of HasManyThrough Relation With Middle-model Condition
I got it.
I should change the $regions.
protected function get_sub_regions($post_cat_id ){
if( $post_cat_id) {
return Region::query()
->with(['managers'=> function( $q) use( $post_cat_id) {
$q->where('posts.post_cat_id', $post_cat_id);
}])
->get();
}
return Region::query()
->with('managers')
->get();
}
Replied to Getting Count Of HasManyThrough Relation With Middle-model Condition
I got it.
I should change the $regions.
protected function get_sub_regions($post_cat_id ){
if( $post_cat_id) {
return Region::query()
->with(['managers'=> function( $q) use( $post_cat_id) {
$q->where('posts.post_cat_id', $post_cat_id);
}])
->get();
}
return Region::query()
->with('managers')
->get();
}
Replied to LiveWire Live Actions Question
I will give you one more tip..
<div class="flex justify-around">
<div class="">{{ $user->id }}</div>
<div
onclick="confirm('Do you really want to change?') || event.stopImmediatePropagation()"
wire:click='toggle({{ $user->id }})'
class="{{ $user->status ? 'text-green-700': 'text-red-700' }} cursor-pointer w-20"
>
{{ $user->status ? 'success': 'fail' }}
</div>
<div class="">{{ $user->name }}</div>
</div>
Try. You would want to keep it.
Replied to LiveWire Live Actions Question
If you use method for looping all rows, it go too much complicated.
Use followings. It is working and simple.
Route::get('/', function () {
$users= User::paginate();
return view('welcome', ['users'=>$users ]);
});
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
@livewireStyles
<link href="https://unpkg.com/[email protected]^2/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="">
@foreach ($users as $user)
@livewire('row', ['user' => $user], key($user->id))
@endforeach
@livewireScripts
</body>
</html>
namespace App\Http\Livewire;
use App\Models\User;
use Livewire\Component;
class Row extends Component
{
protected $user;
public function render()
{
return view('livewire.row',[
'user'=>$this->user
] );
}
function mount( $user) {
$this->user = $user;
}
function toggle( $user_id){
$this->user = User::find( $user_id);
$this->user->status = ! $this->user->status;
$this->user->save();
}
}
<div class="flex justify-around">
<div class="">{{ $user->id }}</div>
<div
wire:click='toggle({{ $user->id }})'
class="{{ $user->status ? 'text-green-700': 'text-red-700' }} cursor-pointer bg-gray-300"
>
{{ $user->status ? 'success': 'fail' }}
</div>
<div class="">{{ $user->name }}</div>
</div>
Replied to LiveWire Live Actions Question
you are going around.
Use nested component. That's more simple.
https://laravel-livewire.com/docs/2.x/nesting-components
make two compoent - > Outer / RowComponent
in Outer blade
<div>
@foreach ($users as $user)
@livewire('row-compont', ['user' => $user], key($user->id))
@endforeach
</div>
in RowComponent.php
function mount( $user ){
$this->user = $user ;
}
function success(){
$this->user->status = ! $this->user->status;
$this->user->save();
}
in row-component.blade.php
<td
wire:click='success()'
class="{{ $user->status == 1 ? 'text-green-800' : 'text-red-800' }}"
>
{{ $user->status == 1 ? 'success' : 'not success' }}
</td>
Above code is sudo codes but you can catch the point.
Started a new Conversation Getting Count Of HasManyThrough Relation With Middle-model Condition
Region hasMany Posts. / Posts hasMany Managers.
So I can use 'hasManyThrough' relationship.
Region has managers with ''hasManyThrough' .
class Region extends Model
{
function posts(){
return $this->hasMany( Post::class );
}
function managers(){
return $this->hasManyThrough( Manager::class );
}
}
class Post extends Model
{
function region(){
return $this->belongsTo( Region::class );
}
function managers(){
return $this->hasMany( Manager::class );
}
}
class Manager extends Model
{
function post(){
return $this->belongsTo( Post::class );
}
}
I guess I can get managers list with region id and post id .
$managers = Manager::query()
->when( $post_id , function( $q) use( $post_id ) {
return $q->whereHas( 'post', function( $qq) use( $post_id) {
$qq->where('posts.id', $post_id );
});
})
->when( $region_id , function( $q) use( $region_id ) {
return $q->whereHas( 'post.region', function( $qq) use( $region_id) {
$qq->where('regions.id', $region_id );
});
})->get();
But the problem is how to get managers count according to the region / post together..
When the post id is fixed, I want to loop regions and get the number of managers which belongs To Post and Region together..
Following codes does not work as I want.
@foreach( $regions as $region)
{{ $region->managers->count() }}
@endforeach
How can I get count for hasManyThrough relationship with through-model (?) condition ?
Replied to Where Do You Save Auth Users Data ?
So maybe I have to merge Auth user data into Request Object ?
I guess so. And no more another way..
Thanks.
Replied to Where Do You Save Auth Users Data ?
If app use controllers only, this way could be fine.
But in a page, there are several blade / livewire components, it uses Auth user and relationships.
Like button (fb), comments button, stars something button, there should be notice if Auth users subscribed or not.
Yes I make all buttons with livewire/ blade components each .
User relations can be called a lot of times in just one page.
That's why I want to use cached Auth user.
Replied to Where Do You Save Auth Users Data ?
hi.
I know about view composer.
But it is for views, not for controller / livewire component / blade components.
And I really need it cached. Too many pages / components use Auth user.
I break the cache whenever Auth user data changed. So it is fine.
Replied to Where Do You Save Auth Users Data ?
Then do you have another idea ?..
Could you give some idea?
Replied to Where Do You Save Auth Users Data ?
in console case, I use this.
if( ! App::runningInConsole()) {
}
No idea for queue job .
Thanks.
Replied to Where Do You Save Auth Users Data ?
So you mean
$currentUser = User::with('role')->with('level')->with('profile')->find( auth()->id() ) ;
it can be transferred with Request without re-query on next page ?
Replied to Where Do You Save Auth Users Data ?
Request Object is good place to get auth user, I agree.
But I need some user_profile, user_role, user_level , user_logs .. always, because of client requests.
So I want to save it with relations somewhere, specially using cached.
Any idea?
Started a new Conversation Where Do You Save Auth Users Data ?
I can get auth user in AppserviceProvider.
But I want to save the user' relations somewhere with cache.
I don't want call auth users relations every time.
Where do you guys save this ?
Started a new Conversation Filtering Of Constraining Eager Loading Of MorphTo
hi .
I have 3 models. Post / Comment / Point .
Whenever Post / Comment action happen, new Point model will be inserted.
class Post extends Model
{
use SoftDeletes;
public function points()
{
return $this->morphMany( Point::class, 'pointable');
}
}
class Comment extends Model
{
public function points()
{
return $this->morphMany( Point::class, 'pointable');
}
}
class Point extends Model
{
function pointable(){
return $this->morphTo();
}
}
But Post model has softdelete but Comment model doesn't.
Sometimes post and comments can be deleted.
I want to retrive data from points table.
Because for avoding errors, I decided gave up the deleted row, and it is ok for application administration.
I tried to use 'Constraining Eager Loading'
$points = Point::query()
->with(['pointable' => function (MorphTo $morphTo) {
$morphTo->constrain([
Post::class => function (Builder $query) {
$query->whereNull('deleted_at');
},
Comment::class => function (Builder $query) {
$query->whereNull('deleted_at'); /// --> no softdelete table, How can I handle this ?
},
]);
}])
->get();
But there is no deleted_at field on comments table.
How can I get points list with Post / Comment without error ?
Started a new Conversation Eager Loading For Authenticated User ?
hi .
I have User Model .
class User extends Authenticatable
{
function role(){
return $this->belongsTo( Role::class);
}
function points(){
return $this->hasMany( Point::class);
}
function level(){
return $this->belongsTo( Level::class);
}
function scraps (){
return $this->hasMany( Scrap::class );
}
function posts(){
return $this->hasMany( Post::class );
}
function comments(){
return $this->hasMany( Comment::class );
}
function likes(){
return $this->hasMany( Like::class );
}
function logins(){
return $this->hasMany( Login::class );
}
}
So it has a lot of relations .
in controller I must use some or all of relations.
something like these..
$user = Auth::user(); // or auth()->user() ..
foreach( $user->posts as $post ){
;;
}
foreach( $user->likes as $like ){
;;
}
$user->comments->count();
$user->comments()->parent;
So loading Auth::user() only causes N+1 query trouble.
How can I get '$user' with eager relationship? loading ?
Started a new Conversation How Can I Use Components Following From Jetstream ?
Hi.
I really like jetstream components. But not the ambiguous and complex controlling layers of jetstream package.
So I dont want to use laravel/jetstream package but I want components only.
I will use laravel/breeze.
And I want jetstream components together. it is good with livewire.
How can I use jetstream components without jetstream package ?
Should I remove Auth Part of jetstream package after install ?
Or is it from another packages ?
Replied to 'database' Notification Paginate ?
Hi.. It is wondering..
I coded without perPage. and it works well.
Maybe it is cause withPagination trait.
Replied to 'database' Notification Paginate ?
Sorry.
it should be this
$notifications = Auth::user()->notifications()->paginate();
Replied to How To Redirect To Login Page When 419 Error
You gave me the important point..
I must not redirect to 'login' route for the shake of CSRF Token.
Yep. Thank you so much.
Started a new Conversation 'database' Notification Paginate ?
I am struggling with 'notifications' .
I want to use notification pagination in livewire component.
class RightMenu extends Component
{
use WithPagination;
public function render()
{
$notifications = Auth::user()->notifications->paginate();
}
}
It cause error .
livewire Illuminate\Notifications\DatabaseNotificationCollection::paginate does not exist.
I can not find any references on document.
How can I paginate database notification collection?
Replied to Dispatch Event And Call Livewire Component Method Together.
Thanks.
Actually it is little bit confusing case for me.
I will consider your advice..
Started a new Conversation Dispatch Event And Call Livewire Component Method Together.
What I want is to show 'right-menu' and call method of Livewire Component.
I have button to show right-menu.
The button dispatch 'open-right-menu' to another alpinejs component.
and that dispatch fire 'setNotificationsZero()' event in livewire component, I wish.
// right menu part
<div
x-data="xx_right_menus()"
x-init="right_menu_init()"
x-on:open-right-menu.window="right_open = ! right_open ; $wire.setNotificationsZero();"
x-cloak
>
<div > something here...</div>
</div>
// trigger button
<div x-data
x-on:click="$dispatch('open-right-menu')"
>
Open Right Menu
</div>
<script>
function xx_right_menus(){
;;
}
</script>
but I got error
Uncaught ReferenceError: $wire is not defined
So I tried with 'this'
<div x-data
x-on:open-right-menu.window="right_open = ! right_open ; this.$wire.setNotificationsZero();" >
it cause new error
Uncaught TypeError: Cannot read property 'setNotificationsZero' of undefined
and I tried with '@this'
<div x-data
x-on:open-right-menu.window="right_open = ! right_open ; @this.$wire.setNotificationsZero();" >
Attempt to read property "id" on null
and I tried with 'Livewire.emit'
<div x-data
x-on:open-right-menu.window="right_open = ! right_open ; Livewire.emit('setNotificationsZero');" >
in this case there is no reaction. i mean setNotificationsZero is not fired.
How can I call livewire method from alpine js component ?
Replied to How To Redirect To Login Page When 419 Error
Actually 419 error happens even when user logout.
'logout' uses csrf token too..
And usual form submit requires csrf token too.
So it is everywhere, and that's why I want to customize error handler.
Thanks.
Replied to How To Redirect To Login Page When 419 Error
Thanks for reply.
But I need error handler in laravel 8 . not previous version.
Thanks.
Started a new Conversation How To Redirect To Login Page When 419 Error
Hi.
How can I redirect to another route ( login ) when 419 error happens ?
Replied to Why Livewire Create Subfolder In Livewire-tmp Folder And Can Not Find Temp File?
This is for someone who has trouble with same problem..
In this case, I found 'jpg' file is fine 'usually' not always.
Started a new Conversation Why Livewire Create Subfolder In Livewire-tmp Folder And Can Not Find Temp File?
My livewire version ^1.3 . and remote Image server is being used separately.
I can not upgrade livewire up to 2.x right now.. .....
I got this error .
File not found at path: livewire-tmp/Leq1rOyYge2biOuMgO2RnC5wbmc=-.png
I looked up logs .
I found error message in laravel log file.
[2021-03-19 00:55:35] production.ERROR: File not found at path: livewire-tmp/Leq1rOyYge2biOuMgO2RnC5wbmc=-.png
{"userId":2,"exception":"[object] (League\Flysystem\FileNotFoundException(code: 0): File not found at path: livewire-tmp/Leq1rOyYge2biOuMgO2RnC5wbmc=-.png at /projects/myproject/vendor/league/flysystem/src/Filesystem.php:389)
Leq1rOyYge2biOuMgO2RnC5wbmc=-.png was not found in tmp folder..
so I went to Image Server and run this
find ./ -name "Leq1rOyYge2biOuMgO2RnC5wbmc=-.png"
[email protected]:~/storage/image-storage/livewire-tmp$ find ./ -name "Leq1rOyYge2biOuMgO2RnC5wbmc=-.png"
./CWtX8ZrU1TCcZzc4R6lbWFS8GsaSZJ-meta67Kg66as7JWk6rW/Leq1rOyYge2biOuMgO2RnC5wbmc=-.png
./wMFgKFSpl94Ndarmxqd6EXsHMDYJ5a-meta67Kg66as7JWk6rW/Leq1rOyYge2biOuMgO2RnC5wbmc=-.png
./KpE7nOkuwKkn9c53Qqkt3kPWnMpn9S-meta67Kg66as7JWk6rW/Leq1rOyYge2biOuMgO2RnC5wbmc=-.png
./T0uYtggOMGHRTbzQzDvYeYiDSlrdZU-meta67Kg66as7JWk6rW/Leq1rOyYge2biOuMgO2RnC5wbmc=-.png
./NxNdAhOCK2YLhRdftgC7J1p9fu1HGx-meta67Kg66as7JWk6rW/Leq1rOyYge2biOuMgO2RnC5wbmc=-.png
Yes I found 5 'Leq1rOyYge2biOuMgO2RnC5wbmc=-.png' with in different folder but not 'livewire-tmp' base folder.
My question.
In livewire-tmp folder, there are temp files and 'folders'. Why are folders created 'sometimes' not always?
I have livewire 2.4 version projects. I can see same situations 2.4 version.
2.4 makes another tmp folders in 'livewire-tmp' folder .
So I guess There could be same situation happens in 2.x version.
How can I solve this problem ?
Followings are parts of config/livewire.php
'temporary_file_upload' => [
'disk' => 'image-storage', // it is specified in 'filesystems.php'
'rules' => null,
'directory' => null,
'middleware' => 'throttle:20,1' ,
],
Thanks.
Started a new Conversation Not Getting Deleted Post From Comment With "with('post')"
Hi.
I have Post / Comment Model .
Post has SoftDelete field. One of Post Record is deleted with softdelete.
But I have to get Comments like this following.
$comments = Comment::where('user_id' , $user->id)
->with('post')
->get();
When I deleted Post record the Comment Relation was not deleted together.
It is job request from client. ( it is real world.. huh. )
Anyway when I query comments with post , it gets 'deleted belongsTo Post' together.
I need paginate without the 'deleted belongsTo Post'. but I can not find references in documents.
Do I have to use Raw Sql for this not Eloquent ORM ?
Thanks for any advices.
Replied to Best Place For Modification Of Input Data?
What if I just want to change data only ?
I don't like helper autoloading.
Do you have another options for modifying data only ?
Replied to Best Place For Modification Of Input Data?
Oh my..
I didn't know about it.
Thank you very much..
Started a new Conversation Best Place For Modification Of Input Data?
Hi.
I am wondering where is the best place for modification together.
There is make:request for input validation .
But with Input validation, I can not modify the input data and let it go through to next normal steps. not to error handlers..
I have to check validation and have to modify user input for several controllers and livewire components.
Where is the best place to change input data and let it go to next process ?
Replied to Expected SSH_FXP_ATTRS Or SSH_FXP_STATUS (server Issue?)
Hi. I have same trouble.
yes sometimes works, sometimes no.
Expected SSH_FXP_ATTRS or SSH_FXP_STATUS
But I just "guess" it is from network trouble, something like unstable connection. it is just guessing .
Does somebody knows this case?
Replied to How To Use Traits With Constant For Model ?
Thanks.. I till try.. and I will post my code.
Replied to How To Use Traits With Constant For Model ?
I agree. Using config file is possible. But using config file makes me hesitated.
I guess it is really going around too much.
Thanks.
Replied to How To Use Traits With Constant For Model ?
Yes. I have a lot of models which have almost same status options not exact same options. So I want to put 2~3 option traits for all models.
Do you have any going around way in this case? .
Started a new Conversation How To Use Traits With Constant For Model ?
I know using status variables in model is happened often.
So I try to separate constants parts from Models using trait.
in model.
use App\Traits\Models\StatusTicket;
class FranchiseRequest extends Model
{
use HasFactory;
use StatusTicket;
}
in trait
namespace App\Traits\Models;
trait StatusTicket
{
const STATUS_OPEN = 1;
const STATUS_HOLD = 2;
const STATUS_CLOSED = 3;
public function isOpen()
{
return $this->status == self::STATUS_OPEN ;
}
}
But constant is not allowed in trait..
How can I separate constants parts from Model ?
Started a new Conversation Getting Different Field With WhereHasMorph
hi .
I just want to get different field data from MorphTo relationship ..
followings are simplified fields in each tables;
posts
id - integer
title - string
videos
id - integer
url - string
comments
commentable_id - integer
commentable_type - string
in controller
use Illuminate\Database\Eloquent\Builder;
$comments = Comment::whereHasMorph(
'commentable',
[Post::class, Video::class],
function (Builder $query, $type) {
$column = $type === Post::class ? 'content' : 'url';
// Here.. how to return 'content' for Post::class and 'url' for Video::class
}
)->get();
in view
@foreach( $comments as $comment )
{{ $comment->commantable->title }} ??
// or
{{ $comment->commantable->url }} ??
@endforeach
I guess it should be eager loading because all data is looped in foreach .
How can I get different fields data from morphTo relationship ?
Replied to Can Not Use Auth::user() In ServiceProvider..
Thanks , I solved this.
I made 'ViewComposerServiceProvider';
php artisan make:provider ViewComposerServiceProvider
in config/app.php
'providers' => [
;;;
App\Providers\ViewComposerServiceProvider::class, // -->added..
];
in ViewComposerServiceProvider.php
public function boot()
{
View::composer('*', function ($view) {
if( Auth::check() ){
return $view->with('currentUser', Auth::user()) ;
} else {
return $view->with('currentUser',null ) ;
}
});
}
It works.
Thanks.
Started a new Conversation Can Not Use Auth::user() In ServiceProvider..
I want to use View Composer of Auth::user() for every views.
But in AppServiceProvider.php , Auth::check() not works, and Auth::users() returns null only.
I checked if I logged in already.. yes. it is .
How can I use Auth in AppServiceProvider?
Replied to How Can I Access Model Dynamically ?
solved.
There was no id = 1 row..
was deleted.
Above codes works fine.
Replied to How Can I Access Model Dynamically ?
and Yes.. I added each connections;
in database.php
'connections' => [
'mysql' => [
'driver' => 'mysql',
;;
],
'apple' => [
'driver' => 'mysql',
;;
],
'banana' => [
'driver' => 'mysql',
;;
],
];
Started a new Conversation How Can I Access Model Dynamically ?
I have two Modules ( Apple / Banana )
Each Module has same model 'Post'
\Modules\Apple\Post
class Post extends Model
{
protected $connection='apple';
protected $table ='posts';
}
\Modules\Banana\Post
class Post extends Model
{
protected $connection='banana';
protected $table ='posts';
}
( I used Laravel Module Package )
in controller
$entity = new \Modules\Apple\Post;
$post = $entity::find(1);
dd($post); // --> it returns null
I checked that there is data in two 'posts' tables.
but it returns 'null';
Do I miss something ?
Replied to Vendor Folder Accessible ?
Thanks for reply.
Actually php files which under vendor folder can not be accessible by url.
and I installed packages again with your advice..
composer install --optimize-autoloader --no-dev
Really thank you.
But I wonder about Laravel.com / Laracasts.com
They dont show 404 page too.
Does it mean their host settings are not incorrect also ?
How should the host be set up ?
Replied to Vendor Folder Accessible ?
I found .. it is same in laravel.com / laracasts.com
with /vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php path..
it shows
No input file specified.
Should I have to accept it ok ?
I really am being confused.
p.s. sorry for testing..