eloquent

Counting Models related through manyToMany only if a pivot attribute null - Laravel

那年仲夏 提交于 2020-01-17 05:13:12
问题 I am elaborating code acquired here ManyToMany relation - how update attribute in pivot table what I want: I want to get a collection of Activities related to any weekly Routine . The pivot table's atribute done_at tells me when any activity (task) was completed. I can list and later ->count() activities related to parent model in manyToMany relation: public function activities() { return $this->belongsToMany('App\Models\Activity', 'activity_routine', 'routine_id', 'activity_id')->withPivot(

How to create follower relationships using Polymorphic Many to Many relationships with Laravel?

▼魔方 西西 提交于 2020-01-17 04:12:25
问题 Okay, so I'm trying to create a relationship where users can follow other users or follow categories. I got a recommendation to use Polomorpic Many to Many relationships. Right now I'm trying to create a follower - followee relationship but I can't wrap my head around how to set up the models and the controller. TABLES Users Schema::create('users', function (Blueprint $table) { $table->increments('id'); $table->string('email'); $table->string('password'); $table->string('first_name'); });

Convert SQL to Eloquent to Join multiple table and Count

為{幸葍}努か 提交于 2020-01-16 18:43:27
问题 I have been this SQL working fine, but trying to convert it to Eloquent format, it keeps returning a wrong, SQL entirely which flags error. Coding with Laravel 5.5 Select arm_articles.article_topic, arm_articles.id, arm_articles.article_id, COUNT(arm_article_views.view_article_id) AS TotalViews, COUNT( arm_article_likes.liked_article_id) AS TotalLikes, COUNT( arm_article_comments.comment_article_id) AS TotalComments FROM arm_articles LEFT JOIN arm_article_views ON arm_articles.article_id =

Laravel query builder with subquery and dynamic filters

假装没事ソ 提交于 2020-01-16 08:42:31
问题 I need to use the laravel query builder for a query with sub query with multiple dynamic filters. The code is something like this: $totalRechargesPerUserQuery = DB::table('transactions as T2') ->select([DB::raw('COUNT(T2.user_id)')]) ->join('users as U2', 'U2.id', '=', 'T2.user_id') ->whereRaw('U2.id = U1.id') ->groupBy('T2.user_id'); $this->applyPromotionFilter($totalRechargesPerUserQuery, $selectedWeeks, 'T2'); $query = DB::table('transactions as T1') ->select([ 'U1.name', 'U1.email', 'U1

Unknown aggregate column in having clause

半城伤御伤魂 提交于 2020-01-16 05:13:26
问题 im trying to build a location based event search in PHP + MySQL (Using Laravel and its Eloquent ORM) This is the query I am using: select events.*, ( 3959 * acos( cos( radians(50.5) ) * cos( radians( addresses.latitude ) ) * cos( radians( addresses.longitude ) - radians(9.50) ) + sin( radians(50.5) ) * sin( radians( addresses.latitude ) ) ) ) AS distance from `events` inner join `addresses` on `events`.`address_id` = `addresses`.`id` having `distance` <= 10 order by `id` desc limit 15 offset

How to split up an Eloquent model query

独自空忆成欢 提交于 2020-01-16 04:47:25
问题 I'm trying to break up an Eloquent query like this $query = new Product; if (!empty($req->query['id'])) { $query->where('id', '=', '1'); } $products = $query->get(); The result above gives me all products in the database. This however, does in fact work. $products = Product::where('id', '=', '1')->get(); Is there a way to do this? 回答1: In Laravel 4 you need to append your query parameters to your query variable. In Laravel 3, it would work like you're doing. This is what you need to do (I'm

Laravel Eloquent distinct values

为君一笑 提交于 2020-01-16 04:32:09
问题 PROBLEM SOLVED. I'm trying to get an person's books, but there are more than one book in my book table since there are different editions of a book. When I list all books of a person, I shouldn't list duplicates. Here's what I've done so far Person Model public function books() { return $this->belongsToMany('\App\Models\Thing', 'bookxauthor', 'person_id', 'thing_id'); } PersonController.php $allbooks = Person::find($id)->books; This is great, it lists all the books of an author, but I don't

Define Laravel Eloquent relations between 3 models in a messaging system

我只是一个虾纸丫 提交于 2020-01-16 00:47:31
问题 I'm trying to create multi-participant messaging system. Here's the database design I'v found here: Messaging system database schema Conversation ------------ id Messages -------- id conversation_id from_id subject message from_timestamp Participants ------------ conversation_id user_id last_read_timestamp Could you help me to describe Eloquent relations between these models? I mean this: http://laravel.com/docs/eloquent#relationships 回答1: class Conversation extends Eloquent { public function

How to save multiple Checkboxes value into Pivot Table?

我们两清 提交于 2020-01-15 05:02:53
问题 I have 2 models with Many-to-Many Relation when I create new Device and check multiple Checkboxes I want to save multiple voices_id with Device_id into the Pivot Table device_voice Device Controller ( I am facing this error Undefined variable: selectedVoice in Edit function ) public function create() { $this->authorize("create", Device::class); $voice_list = Voice::all(); return view('devices.create') ->with('voice_list', $voice_list); } public function store(CreateDeviceRequest $request) {

Laravel relationships on a table with two types of flags

假如想象 提交于 2020-01-15 03:55:13
问题 I have two tables products and users Both of this objects has images associated with it in a table images The schema for the images table is id | image_id | resource_id | flag 1 | 567575 | 1 | user 2 | 423423 | 1 | product Based on this flag i am identifying whether its a users image or whether its a products image. If I need to eager load a users image how do it do it? User model <?php namespace App\Entities; use Illuminate\Database\Eloquent\Model; class User extends Model implements