splendidkeen

Experience

8,760

2 Best Reply Awards

  • Member Since 4 Months Ago
  • 30 Lessons Completed
  • 0 Favorites

21st February, 2018

splendidkeen left a reply on Merging A Collection Of Two Models? • 4 days ago

You are right, I missed on them. @Snapey

$model1s = Business::inRandomOrder()->take(4)->get();
$model2s = User::inRandomOrder()->take(4)->get();

$collection = collect(array_merge($model1s->all(), $model2s->all())->shuffle()->take(4);

For this suggestion, I get an error of

Call to a member function shuffle() on array

splendidkeen started a new conversation Converting Models To Base Collections Using The ToBase() Method • 4 days ago

How can I convert the two models into a base collection, before triggering them?

$model1s = Business::inRandomOrder()->take(4)->get();
$model2s = User::inRandomOrder()->take(4)->get();

$collection = $model1s->merge($model2s)->shuffle()->take(4);

splendidkeen left a reply on Merging A Collection Of Two Models? • 4 days ago

How can I convert them to Base Collections using the toBase() method @tykus ?

splendidkeen left a reply on Merging A Collection Of Two Models? • 4 days ago

Thank you to @tykus @Snapey

This works, as I am testing with very few businesses and users, it's true that it will only display businesses and users of !equal id.

I will try @tykus 's advice to provide also same id'd models.

20th February, 2018

splendidkeen left a reply on Merging A Collection Of Two Models? • 4 days ago

I might need to define the $user variable as @else won't trigger the User Model

Tried it this way

@else($model instanceof App\User)

but it won't work, still results in undefined variable: user

splendidkeen left a reply on Merging A Collection Of Two Models? • 4 days ago

home.blade.php

<div class="row"> 
        @include('home.partials.homeblock')        
</div>

HomeController.php

public function index()
{
$model1s = Business::inRandomOrder()->take(4)->get();
$model2s = User::inRandomOrder()->take(4)->get();

$collection = $model1s->merge($model2s)->shuffle()->take(4);

return view ('home', compact('collection'));
} 

homeblock.blade.php

@foreach($collection as $model) 
    @if($model instanceof App\Business::class) (Line 4)

// Parse error: syntax error, unexpected 'class' (T_CLASS), expecting variable (T_VARIABLE) or '$' (Line 4)


          <div class="col-md-3 col-sm-6 col-xs-12">
        <div>
             <h4>{{$business->genre}} at {{$business->company_name}}</h4></a>
             <p >{{$business->city}}, <span>{{$business->street}}</span></p></a>

        </div>
    </div> 

     @else 
     
           <div class="col-md-3 col-sm-6 col-xs-12">
        <div>
             <h4>{{$user->genre}} at {{$user->company_name}}</h4></a>
             <p >{{$user->city}}, <span>{{$user->street}}</span></p></a>

        </div>
    </div> 

     @endif 
@endforeach

splendidkeen left a reply on Merging A Collection Of Two Models? • 4 days ago

Thank you both @tykus & @Snapey How could I tell my partial to use the correct div for the expected Model?

homeblock.blade.php

@if($model1s = $business)
<div class="col-md-3 col-sm-6 col-xs-12">
<div>
    <h4>{{$business->genre}} at {{$business->company_name}}</h4></a>
    <p >{{$business->city}}, <span>{{$business->street}}</span></p></a>

</div>
</div> 

@else
<div class="col-md-3 col-sm-6 col-xs-12">
<div>
    <h4>{{$user->username}} at {{$user->first_name}}</h4></a>
    <p >{{$user->city}}, <span>{{$user->street}}</span></p></a>

</div>
</div> 

@endif 

splendidkeen left a reply on Merging A Collection Of Two Models? • 4 days ago

Hey, thank you @Snapey

I am looking for 4 instances of e.g. a Business and a User Model, inRandomOrder.

My common partial for a Business Instance:

<div class="col-md-4 col-sm-6 col-xs-12">
<div>
    <h4>{{$business->genre}} at {{$business->company_name}}</h4></a>
    <p >{{$business->city}}, <span>{{$business->street}}</span></p></a>

</div>
</div> 

This is my Users partial

<div class="col-md-4 col-sm-6 col-xs-12">
<div>
    <h4>{{$user->username}} at {{$user->first_name}}</h4></a>
    <p >{{$business->city}}, <span>{{$business->street}}</span></p></a>

</div>
</div> 

Then the collection could use a partial based on its Model

splendidkeen started a new conversation Merging A Collection Of Two Models? • 4 days ago

Is their a way to merge a collection of two Models and take 4 of them inRandomOrder?

Such as:

$collection = (Model1::inRandomOrder()&&Model2::inRandomOrder())->take(4)->get();

return view ('home', compact('collection');

and the use a partial of

if($collection instance === Model 1)

// show div of Model 1

else ($collection instance === Model 2)

// show div of Model 2

endif 

in order to use the right block for the correct random Model?

19th February, 2018

splendidkeen started a new conversation How To Get A Closer Look Inside An Image Upload-process? • 5 days ago

I am using the Intervention Image Package for uploading images.

E.g.:

$this->validate($request, [
    'image' => 'max:800',
]);

if($request->hasFile('image')){

    $user = Auth::user();

    $image   = $request->file('image');
    $filename = time() . '.' . $image->getClientOriginalExtension();
    $location = public_path('uploads/'. $filename );
    Image::make($image)->resize(300,300)->save($location);
            
    $user->image = $filename;
    $user->save();
}

This is only touching the surface. How can I get a closer look inside the process, in terms of (1) validation, (2) upload communication and (3) error display, but also with respect to maybe (4) previewing the chosen images, before uploading them?

Really looking forward for your advice, thank you

17th February, 2018

splendidkeen left a reply on Google Maps Database Link • 1 week ago

This makes sense. Thank you. @mikefolsom

Then I could probably link the Map with my Search Controller, and trigger the Map dots based on the Users search query, right?

splendidkeen left a reply on Google Maps Database Link • 1 week ago

First, thank you @mikefolsom

I think geocoding the address and passing those to the Google Maps JavaScript API is what I am looking for. Is there a way to dynamically link the two sides, so whenever a new user was created, a new location within the map will be marked with a dot/ or the users avatar as a dot?

splendidkeen started a new conversation Google Maps Database Link • 1 week ago

Does anyone know how to link Google Maps with e.g my User.php Models content such as the location of my applications users, which would then be displayed within the Map.

Or for example integrate a location model which can be used for a user to search several locations within Google Maps based on the Models locations?

Looking forward, thank you

splendidkeen left a reply on Stuck In Uploading Image By Intervention • 1 week ago

You could try:

            $file =$request->file('file');      
    
            $filename = time() . '.' . $file->getClientOriginalExtension();
                $location = public_path('images/'. $filename);
                Image::make($file)->resize(128,128)->save($location);

                $file->file = $filename;
                $file->save();  

This would store your file within a folder (/images) in your public directory @Webiondev123

16th February, 2018

splendidkeen left a reply on InRandomOrder()->take(4) Won't Work • 1 week ago

Could I even merge content of two Models within one collection? @bobbybouwmann

Such as

$collection = (Model1::inRandomOrder()&&Model2::inRandomOrder())->take(4)->get();

return view ('home', compact('collection');

homeblock

if($collection instance === Model 1)

// show div of Model 1

else ($collection instance === Model 2)

// show div of Model 2

endif 

splendidkeen left a reply on InRandomOrder()->take(4) Won't Work • 1 week ago

Cool, that works fine. Thank you @bobbybouwmann

splendidkeen left a reply on InRandomOrder()->take(4) Won't Work • 1 week ago

Great this works, thank you @bobbybouwmann

How can I make it more dynamic such as e.g displaying only businesses from a city x?

splendidkeen started a new conversation Trying To Display Content InRandomOrder Won't Work • 1 week ago

I'm currently trying to make my application more dynamic and intelligent. Therefore I wanted to parse partials of content (1) inRandomOrder()->take(x) & (2) inRandomOrder->take(x) + based on specific content (e.g. a city or street) on my home view

This is my setup for case (1)

HomeController

public function home()
    {
        $businesses = Business::inRandomOrder()->take(4); 
        return view ('home', compact('businesses'));
    }   

homeblock.blade.php


<div class="col-md-3 col-sm-6 col-xs-12">
<div>
    <a href="#"><h4>{{$business->company_name}}</h4></a>
    <a href="#"><p>{{$business->city}}, {{$business->street}}</p></a>

</div>
</div> 
 

home.blade.php

<div class="row" id="genre-display-2"> 
    @each(partials.homeblock', $businesses, 'business')   
</div>

But it won't show any businesses, am I missing something?

Thank you

12th February, 2018

splendidkeen left a reply on Foreach Loop Doesn't Work Within Newshub • 1 week ago

This is for the UserController right?

public function newshub(Newsline $newsline, Business $business){

        $newslines = Newsline::whereHas('business', function ($query) {
            $query->whereHas('user', function ($q) {
                $q->where('id', auth()->user()->id();
            });
        })->latest()->get();

        return view ('partner.user.news.newsline', compact('newsline'));
         
    }

splendidkeen left a reply on How To Loop Through Product-related Pictures? • 1 week ago

Thank you very much @bashy

splendidkeen started a new conversation How To Loop Through Product-related Pictures? • 1 week ago

A product (Product.php) hasMany(Picture.php).

Now I want to loop through all product related pictures, within my view. I am using dots for navigation. How can I create a foreach loop for the pictures amount and dynamically adjust the amount of dots required to navigate through the pictures?

Product.php

 public function pictures()
 {
          return $this->hasMany(Picture::class);
 }

Picture.php (table: pictures, row: product_image)

public function product()
    {
        return $this->belongsTo(Product::class);
    }

Would be great, if you could help

Current slider:


          <div class="mySlides">
                 <img src="/imgs/{{ $product->pictures()->first()->product_image }}" width="100%">
               
              </div>

              <div class="mySlides">
                <img src="/imgs/{{ $product->pictures()->first()->product_image }}" width="100%">
              
              </div>

              <div class="mySlides">
                <img src="/imgs/{{ $product->pictures()->first()->product_image }}" width="100%">
                
              </div>

              <!-- Next and previous buttons -->
              <a class="prev" onclick="plusSlides(-1)"></a>
              <a class="next" onclick="plusSlides(1)"></a>
            </div>
             <br>
            <!-- The dots/circles -->
            <div>
              <span class="dot" onclick="currentSlide(1)"></span> 
              <span class="dot" onclick="currentSlide(2)"></span> 
              <span class="dot" onclick="currentSlide(3)"></span> 
            </div>

splendidkeen left a reply on Foreach Loop Doesn't Work Within Newshub • 1 week ago

So I should loop through all newslines and only show a User those where the newslines business_id equals a business which is related to the User? @tykus

splendidkeen left a reply on Foreach Loop Doesn't Work Within Newshub • 1 week ago

How can I adjust the loop, that it will go through all the newsiness related to the business I follow, and then list them ordered by when they was created? @tykus

This one works

@foreach($business->newslines()->orderBy('created_at', 'desc')->get() as $newsline)

but it shows every newsline of business 1, and then every newsline of business 2.

With the hasManyThrough function it could work that it would show like, latest newsline of business 1, then 2, then 1 etc.

splendidkeen left a reply on Foreach Loop Doesn't Work Within Newshub • 1 week ago

@tykus I tested hasManyThrough:

@foreach(Auth::user()->newslines as $newsline)

@endforeach

which results in

Column not found: 1054 Unknown column 'business.user_id' in 'field list' (SQL: select `newslines`.*, `business`.`user_id` from `newslines` inner join `business` on `business`.`id` = `newslines`.`business_id` where `business`.`user_id` = 1)

splendidkeen left a reply on Foreach Loop Doesn't Work Within Newshub • 1 week ago

That works fine, thank you @tykus How would hasManyThrough look like at User.php?

splendidkeen started a new conversation Foreach Loop Doesn't Work Within Newshub • 1 week ago

Hey, I am logged in as a User (User.php hasMany(Newsline::class) and belongsToMany(Business::class) trying to loop through Newsline.php which ````belongsTo(Business::class)``` within a 'Newshub'.

UserController

public function newshub(Newsline $newsline, Business $business){
        
        $newsline = Newsline::where('id', $newsline->id)->first();
        return view ('partner.user.news.newsline', compact('newsline'));
}

User.php

public function stores(){
       return $this->belongsToMany(Business::class)
} // Follow trait

public function newslines(){
       return $this->hasMany(Newsline::class)
}

Business.php

 public function newslines(){
        return $this->hasMany(Newsline::class, 'business_id');
    }

Newsline.php

public function business(){
        return $this->belongsTo(Business::class, 'business_id');
}

public function user(){
        return $this->hasMany(User::class);
}

Loop:


@foreach(Auth::user()->stores->newslines as $newsline)
<div class="row">
    <div class="col-md-1">
         <img> 
    </div>
    <div class="col-md-8">
        <h4>{{$newsline->business->first()->company_name}}</h4>
        <p>{{$newsline->body}}</p>
    </div>
</div>
@endforeach

Results in:

Property [newslines] does not exist on this collection instance.

1st February, 2018

splendidkeen left a reply on How To Return Redirect With {username}? • 3 weeks ago

Thanks, had this in mind, but made a mistake with a ',' instead of '=>'. It works fine, now.

splendidkeen started a new conversation How To Return Redirect With {username}? • 3 weeks ago

Route:

/user/{username}

Where do I need to add ['username' => $user->username] within my Controller:

return redirect()
    ->route('user.profile.show', $user);

23rd January, 2018

splendidkeen started a new conversation How To Divide A Value? • 1 month ago

Hello. How can I divide a e.g.

{{$product->price}}

by 10 to dynamically show the result instead the product's price?

14th January, 2018

splendidkeen started a new conversation How To Prepare A Production Site Efficient And Scalable? • 1 month ago

One quick question, as I am planning to test a small production landing page with a form for the User to post: How can I ensure that the site will be kind of scalable and efficient in terms of bugs and traffic?

(No production experience, yet)

12th January, 2018

splendidkeen left a reply on Event Listener To Store Points For A User, DB Issue • 1 month ago

Where do I define the property of $event->reply (Listener). I tested it for another Event and now it spits an error e.g.

undefined variable: reply

or


undefined property: $reply

splendidkeen left a reply on Upload Validation Issue (Error Communication) • 1 month ago

Updated my Version above, now it results in

Type error: Argument 1 passed to Illuminate\Validation\Factory::make() must be of the type array, object given

splendidkeen left a reply on Upload Validation Issue (Error Communication) • 1 month ago

Thank you @adnan483

This makes sense, I tried it with a very large sized picture (size:2044854) but it won't stop it from being uploaded.

splendidkeen started a new conversation Upload Validation Issue (Error Communication) • 1 month ago

I implemented a new validation to my Users Avatar upload, but it won't validate the files correctly, just uploads everything.

ProfileController.php

public function uploadAvatar(Request $request){


        if($request->hasFile('avatar')){

            $user = Auth::user();

            $input_data = $request->all();

            $validator = Validator::make(
                $input_data, [
                'avatar.*' => 'required|mimes:jpg,jpeg,png,bmp|max:800'
                ],[
                    'avatar.*.required' => 'Please upload an image',
                    'avatar.*.mimes' => 'Only jpeg,png and bmp images are allowed',
                    'avatar.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
                ]
            );

            if ($validator->fails()) {
                return response()->json(array(
                   'success' => false,
                   'errors' => $validator->errors()->toArray()

                   ), 400);   
            }

            $image = $request->file('avatar');


            $filename = time() . '.' . $image->getClientOriginalExtension();
            $location = public_path('uploads/avatars/'. $filename);
            Image::make($image)->resize(300,300)->save($location);
            $user->avatar = $filename;
            $user->save();  
        }

        return redirect()
            ->route('profile.editavatar')
            ->with('info', 'Your avatar has been uploaded.');
    }

Am I doing something wrong in terms of order?

11th January, 2018

splendidkeen left a reply on Auth Middleware In Route And/or Controller • 1 month ago

As the route uses the Controller, it doesn't necessarily needs to be used within the Controller. It depends, difficult to say without an example @makapaka

splendidkeen left a reply on Auth Guard In Route And/or Controller • 1 month ago

You are setting up guards with providers within auth.php.

Then you can use middleware within your route:

Route::get('home', [
    'uses' => '[email protected]', 
    'as' => 'home',
    'middleware' => ['auth'], // your guard
    ]);

splendidkeen left a reply on Is There A Way To Kind Of Dd() On An Event Listener? • 1 month ago

That worked, thanks @Cronix

I tried to log the FollowedPartner Event:

public function follow(Partner $partner, User $user, FollowedPartner $event){

        Log::info('Something happened');
        Log::info($event);

        Auth::user()->followPartner($partner);
        
        return redirect()->route('partner.show', $partner)->with('info', 'Followed.');
}

How can I log this event, to see if the Event Listener works, because at the moment it won't store any records within my points_adjustments table. The process is that a User gets awarded Points for following a Partner.

The follow functionality works, but nothing will be stored within the points table

        public function awardPoints($event, $points, User $user){   
        $adjustment = new PointAdjustment([
                'event' => get_class($event),
                'points' => $points,
            ]);
    
            $adjustment->user()->associate($user);
    
            return $this-> pointsAdjustments()->save($adjustment);
        }

splendidkeen left a reply on Is There A Way To Kind Of Dd() On An Event Listener? • 1 month ago

How could I see in this case why my Event Listener doesn't work? @Snapey

splendidkeen left a reply on Is There A Way To Kind Of Dd() On An Event Listener? • 1 month ago

Still results in

Object of class App\Events\FollowedPartner could not be converted to string
in Logger.php

splendidkeen left a reply on Is There A Way To Kind Of Dd() On An Event Listener? • 1 month ago

I tried

public function follow(Partner $partner, User $user, FollowedPartner $event){

        Log::info('hey something just happened');
                Log::info($event);

        Auth::user()->followPartner($partner);
        
        return redirect()->route('partner.show', $partner)->with('info', 'Followed.');

which resulted in :

Object of class App\Events\FollowedPartner could not be converted to string
in Logger.php

@Snapey

Maybe that is why it won't work, because within the setup I need the event class as a string


        public function awardPoints($event, $points, User $user){   
        $adjustment = new PointAdjustment([
                'event' => get_class($event),
                'points' => $points,
            ]);
    
            $adjustment->user()->associate($user);
    
            return $this-> pointsAdjustments()->save($adjustment);
        }

splendidkeen left a reply on Is There A Way To Kind Of Dd() On An Event Listener? • 1 month ago

Delete laravel.log? @snapey

splendidkeen left a reply on Is There A Way To Kind Of Dd() On An Event Listener? • 1 month ago

That would be within my AwardPointsForFollowing (Listener)

use Log;
....


 public function handle(FollowedPartner $event)
{
      
        $event->follow->user->awardPoints($event, 5, $event->follow);
    Log::info('hey something just happened');
        Log::info($event);
}

@snapey Wow this file is huge, but I am not seeing a related log to this Listener

splendidkeen left a reply on Is There A Way To Kind Of Dd() On An Event Listener? • 1 month ago

Thank you @Snapey

With class you mean within the Listeners file?

splendidkeen left a reply on Is There A Way To Kind Of Dd() On An Event Listener? • 1 month ago

How would that look like? I have not done this before. I am struggling with a Event Listener incl. a polymorphic relation, but how could I test if something is happening?

splendidkeen left a reply on Event Listener To Store Points For A User, DB Issue • 1 month ago

Thank you. Yes. @martinbean

 'App\Events\FollowedPartner' => [
            'App\Listeners\AwardPointsForFollowing',
        ],

splendidkeen left a reply on Redirecting Back To Intended Laravel 5.5 • 1 month ago

Ok, you could may integrate something like

session(['link' => url()->previous()]);

splendidkeen left a reply on Event Listener To Store Points For A User, DB Issue • 1 month ago

It doesn't work with my setup. It stores no records within my points_adjustments table. After I generate the Event and Listener, and set it up, the only thing changes is that after I push the follow button (follow() method) it will not show the Followed 'info' anymore. @shez1983 I am struggling with this problem, what do you recommend?

splendidkeen left a reply on Redirecting Back To Intended Laravel 5.5 • 1 month ago

This would be another way // return to previous page

return redirect()->intended(Session::pull('referrer'));

splendidkeen left a reply on Redirecting Back To Intended Laravel 5.5 • 1 month ago

I see, you could try

return redirect()->intended($this->redirectPath());
Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.