eloquent

( (Where and Where) OR (Where and Where) ) Laravel 5.2

柔情痞子 提交于 2020-08-27 05:57:28
问题 I am trying to create ( (Where and Where) OR (Where and Where) ) And after a lot of searching I found this $sender = \App\User::where('username','=',$username)->firstOrFail(); $receiver = Auth::user(); $messages = \App\Message::Where(function($query) { $query->where("sender",$sender->id) ->where("receiver",$receiver->id); }) ->orWhere(function($query) { $query->Where("sender",$receiver->id) ->Where("receiver",$sender->id); }) ->get(); But it's showing me that the $sender and the $receiver

( (Where and Where) OR (Where and Where) ) Laravel 5.2

别来无恙 提交于 2020-08-27 05:57:09
问题 I am trying to create ( (Where and Where) OR (Where and Where) ) And after a lot of searching I found this $sender = \App\User::where('username','=',$username)->firstOrFail(); $receiver = Auth::user(); $messages = \App\Message::Where(function($query) { $query->where("sender",$sender->id) ->where("receiver",$receiver->id); }) ->orWhere(function($query) { $query->Where("sender",$receiver->id) ->Where("receiver",$sender->id); }) ->get(); But it's showing me that the $sender and the $receiver

Laravel 5 eloquent hasManyThrough / belongsToManyThrough relationships

非 Y 不嫁゛ 提交于 2020-08-24 09:27:55
问题 In a Laravel 5.2 application I have three models: User , Role and Task . A User is associated with multiple Roles , and a Role is associated with multiple Tasks . Therefore each user is associated to multiple tasks, through their roles. I am trying to access all Tasks associated with a User , through their Roles. The relevant parts of my models look like: class User extends Authenticatable { public function roles() { return $this->belongsToMany('App\Role'); } public function tasks() { return

Laravel 5 eloquent hasManyThrough / belongsToManyThrough relationships

半世苍凉 提交于 2020-08-24 09:27:22
问题 In a Laravel 5.2 application I have three models: User , Role and Task . A User is associated with multiple Roles , and a Role is associated with multiple Tasks . Therefore each user is associated to multiple tasks, through their roles. I am trying to access all Tasks associated with a User , through their Roles. The relevant parts of my models look like: class User extends Authenticatable { public function roles() { return $this->belongsToMany('App\Role'); } public function tasks() { return

Perform order by relationship field in Eloquent

 ̄綄美尐妖づ 提交于 2020-08-23 03:35:33
问题 I want to create product filter with Eloquent. I start like this $query = Product::whereHas('variants') ->with('variants') ->with('reviews') $query = $this->addOrderConstraints($request, $query); $products = $query->paginate(20); Where private function addOrderConstraints($request, $query) { $order = $request->input('sort'); if ($order === 'new') { $query->orderBy('products.created_at', 'DESC'); } if ($order === 'price') { $query->orderBy('variants.price', 'ASC'); } return $query; } However,

Perform order by relationship field in Eloquent

给你一囗甜甜゛ 提交于 2020-08-23 03:34:04
问题 I want to create product filter with Eloquent. I start like this $query = Product::whereHas('variants') ->with('variants') ->with('reviews') $query = $this->addOrderConstraints($request, $query); $products = $query->paginate(20); Where private function addOrderConstraints($request, $query) { $order = $request->input('sort'); if ($order === 'new') { $query->orderBy('products.created_at', 'DESC'); } if ($order === 'price') { $query->orderBy('variants.price', 'ASC'); } return $query; } However,

Laravel - Querybuilder with join and concat

女生的网名这么多〃 提交于 2020-08-22 12:09:41
问题 Im trying to pull all users from the users table who match a certain group in the users_groups pivot table. Im using Sentry 2 from Cartalyst btw. This works to get all users with first and last name concatenated. User::select(DB::raw('CONCAT(last_name, ", ", first_name) AS full_name'), 'id') ->where('activated', '=', '1') ->orderBy('last_name') ->lists('full_name', 'id'); when i try to change it to also filter users who do not belong to a certain group I get a syntax error. User::select(DB: