Showing posts with label Eloquent. Show all posts
Showing posts with label Eloquent. Show all posts

Monday, April 3, 2017

[ANSWERED]Eloquent relataionship 3 tables

im new to Stack Overflow and Laravel.
I try to develop an variable profilesystem in laravel and i got 3 tables (User,UserFields,Fields).
The strutucre of the fields table shows like this:
The structure of the user_fields table shows like this:
The user table is the standard table witch came with laravel 5
now i want to get all fields with user_fields depending on witch user is selected or logged in. If the user_field doesnt exists the model should return null or create a new record for the user_fields.
I try a lot of "solutions" witch came with eloquent like (hasManyThrough, belongsToMany) but i dont get the result witch i wanted.
Is there any solution with relationship methods?
Thanks for ur help, and sorry for my bad english :/


via Florian Beck-Klaus

Tuesday, February 28, 2017

Get Model where relationship = id

Would love to know how other people are achieving the following?
Tables:
teams 
teams_users (pivot many teams, many users)
users

What i am trying to achieve
$teams->user->where('user_id', $id)->get();

however i am having to run a loop, and create another method on the team model to pluck(id, name)
    // foreach ($teams as $team) {
    //  # code...
    // dump($team->getUserIdsAttribute());
    // }

Do you know a better way?



via Harry Bosh

Monday, February 27, 2017

[SOLVED]Laravel Eloquent Join Querying can't access all variables

I have two models defined (Laravel application): AdviceProtocol and Question which you the tables advice_protocols and questions. The models have been linked together by:
public function userGoal() {
        return $this->hasOne('App\Question', 'id', 'user_goal_id');
    }

I would like to get the following variabels from my query: the advice protocol name, the advice protocol category and questions name. The two tables are linked through a set of ids. The query which I have now is:
public function data(){
        $advicePreparationsQuery = AdviceProtocol::select(['advice_protocols.name', 'advice_protocols.category', 'questions.id'])
        ->join('questions', 'advice_protocols.user_goal_id', '=', 'questions.id')
        ->get();

The questions.id can be retrieved, but when I change the variable to questions.name: nothing is retrieved. My output doesn't give an error, but no values are returned. Could someone please help me to get the value of questions.name?



via Anna Jeanine

[SOLVED]Laravel Eloquent Relationships -- equivalent of MYSQL "join"

It seems that in all that Laravel Eloquent relationships can do, it isn't possible to have it add an INNER JOIN clause. Am I correct in that? whereHas clauses add a WHERE EXISTS subquery, and with clauses preform "eager loading" that just loads the relationship in separate queries.
Obviously, there are a lot of cases where an INNER JOIN is going to be way more efficient than running a bunch of queries, so I have written a join clause for these queries.
  1. Is it possible to use an eloquent relationship to define a join clause so that I don't have to rewrite the same join query every time I use the 2 tables together?
  2. If not, why? It seems like joining 2 tables that are related would be something that should be standardized. I can't really see how adding a subquery like WHERE EXISTS would be a suitable substitute. (according to my tests, it generates much slower queries....)


from Latest question asked on Laravel tag.


via Skeets O'Reilly

Advertisement