last day (15 days later) » 

7:31 PM
3
A: Protect routes dynamically, based on id (laravel, pivot table)

lukasgeiterYou can't pass a route parameter to a filter. However you can access route parameters from pretty much everywhere in the app using Route::input(): $id = Route::input('id'); Optimizations public function hasCustomer($id) { if($this->customers()->find($id)){ return true; } ...

 
Thanks! Is my approach suitable? Or would you recommend a better solution?
 
Looks fine to me. Although you could for sure optimize hasCustomer a bit. I'll update my answer.
 
Thanks a lot! Maybe this task is easy but at the beginning it has been very confusing to me. Further, I'm thinking of a more general way to check the permission because if I would have later > 10 pages to protect, I'm creating nearly ten times the same filter?
 
And all the filters would work the same? (If the id is in the related models of user grant access)
 
Yes, currently this is my approach. Maybe I will extend it later.
 
7:31 PM
I added some code to my answer. Hope that helps ;)
 
Wow, here is a chat?
is this new?
 
Not really, in fact it's pretty old ;)
 
Okay, first time for me here
 
Sooo what's up?
 
anyway, thanks for helping me that quickly!
but ..
I have another question, I'm currently struggling with
 
7:34 PM
Yes?
 
<?php

class Customer extends Eloquent
{
    protected $guarded = array();

    public function objects()
    {
        return $this->hasMany('Object')->where('parent_id', null);
    }
}
ah this works, nice!
okay, very basic I think - so my Customer has some Objects, now I'm would like to limit them to those, the user has access to
return $this->hasMany('Object')->where('parent_id', null)
    ->whereIn('id', Auth::user()->objects->lists('id'));
this works fine, but looks very ugly
And I would like to ask, if there is a more elegant way to do this
public function objects()
{
return $this->belongsToMany('Object', 'user_object');
}
thats it ;)
 
Hmm
 
achso
Kommst du aus der Schweiz?
 
Bingo :)
 
ich aus Düsseldorf, Deutschland ;)
 
7:40 PM
Sehe ich gerade ;)
 
google analytics?
 
nein steht auf deinem Profil :D
 
ah ok
stimmt.
 
Also ich nehme mal an du hasst die Beziehungen auch in Object definiert?
*hast
 
// Object has children
public function objects()
{
    return $this->hasMany('Object', 'parent_id');
}
achso, ich könnte direkt da ansetzen
wie man vielleicht merkt, fange ich gerade an mit laravel / eloquent und der start ist manchmal echt verwirrend
 
7:42 PM
Nein ich meinte eigentlich users()
 
achso
public function objects()
{
    return $this->belongsToMany('Object', 'user_object');
}
 
Dachte eher an sowas wie:
public function users()
{
    return $this->belongsToMany('User', 'user_object');
}
 
Ach, im Object?
das habe ich nicht :D
hmmh, also anders herum abfragen
 
wenn du das noch reinhaust könntest du diesen query versuchen:
return $this->hasMany('Object')->whereHas('users', function($q){
$q->where('id', Auth::id());
});
 
ah! Ich verstehe es glaube ich sogar schon
 
7:46 PM
(plus natürlich ->where('parent_id', null))
Toll!
 
vielen Dank,
            $q->where('user_object.user_id', Auth::id());
meine Tablle heißt wohl anders, aber so funktioniert es
*tabelle
 
Okay
users.id sollte auch gehen aber kommt auf's gleiche raus...
 
Stimmt.
würde dir gern noch mal +5 geben :P
 
Tja... da kann ich auch nichts machen ;)
Wenn du wirklich spendabel bist kannst du mir ein bounty geben. Allerdings ist das minimum 50 rep
Aber ist schon ok. Ein Danke reicht mir eigentlich auch :)
 
8:02 PM
Hmm, ich habe ja erst 330 Punkte
Danke! :)
 
Bitte und einen schönen Abend bzw. gute Nacht!
 
Ebenso!
 

last day (15 days later) »