nc1991

Experience

1,980

0 Best Reply Awards

  • Member Since 1 Year Ago
  • 17 Lessons Completed
  • 0 Favorites

5th August, 2018

nc1991 left a reply on Eloquent Query Is Taking An Extremely Long Time • 1 month ago

Thanks Cronix, I thought privacy is indexed because I have it in my schema:

$table->index('privacy');

would I index every one like so:

  $table->index('privacy');
  $table->index('owner_id');
  $table->index('owner_type');
  $table->index('status');

Or like so:

$table->index(['privacy', 'owner_id', 'owner_type', 'status']);

nc1991 started a new conversation Eloquent Query Is Taking An Extremely Long Time • 1 month ago

So I am trying to get my friends and followers statuses with a left join, here is my query:

`/**
 * Scope a query to friends and followers statuses.
 *
 * @param \Illuminate\Database\Eloquent\Builder $query
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeFriendsAndFollowers(Builder $query): Builder
{
    $user_id = auth()->user()->id;

    return $query
        ->leftJoin('followables', function ($join) {
            $join
                ->on('timeline.owner_id', 'followables.followable_id')
                ->on('timeline.owner_type', 'followables.followable_type');
        })
        ->leftJoin('friendships', function ($join) {
            $join
                ->on('timeline.owner_id', 'friendships.recipient_id')
                ->on('timeline.owner_type', 'friendships.recipient_type');
        })
        ->where(function ($query) use($user_id) {
            $query
                ->where(function ($query) use($user_id) {
                    $query->where('followables.user_id', $user_id);
                })
                ->where('privacy', 'public')
                ->orWhere('privacy', 'private');
        })
        ->Orwhere(function ($query) use($user_id) {
            $query
                ->where(function ($query) use($user_id) {
                    $query
                        ->where('friendships.sender_id', $user_id)
                        ->orWhere('friendships.recipient_id', $user_id);
                })
                ->where('privacy', 'public')
                ->orWhere('privacy', 'private');
        })
        ->orWhere(function ($query) use($user_id) {
            $query
                ->where('timeline.owner_id', $user_id)
                ->where('timeline.owner_type', User::class);
        });
}`

Here is my schema;

Timeline:

    Schema::create('timeline', function (Blueprint $table) {
        $table->increments('id')->unsigned()->index();
        $table->text('status')->nullable();
        $table->string('location')->nullable();
        $table->string('type')->nullable();
        $table->string('privacy')->default('public');
        $table->integer('owner_id');
        $table->string('owner_type');
        $table->timestamps();
        $table->softDeletes();

        $table->index(['owner_id', 'owner_type']);
        $table->index('privacy');
        $table->index('owner_id');

    });

Friends:

    Schema::create(config('friendships.tables.fr_pivot'), function (Blueprint $table) {
        $table->increments('id')->unsigned()->index();
        $table->morphs('sender');
        $table->morphs('recipient');
        $table->tinyInteger('status')->default(0);
        $table->timestamps();

        $table->index(['sender_id', 'recipient_id']);
    });

Followers

    Schema::create(config('follow.followable_table', 'followables'), function (Blueprint $table) {
        $table->increments('id')->unsigned()->index();
        $table->unsignedInteger('user_id')->index();
        $table->unsignedInteger('followable_id');
        $table->string('followable_type');
        $table->string('relation')->default('follow')->comment('folllow/like/subscribe/favorite/');
        $table->timestamp('created_at');

        $table->foreign('user_id')
            ->references(config('follow.users_table_primary_key', 'id'))
            ->on(config('follow.users_table_name', 'users'))
            ->onUpdate('cascade')
            ->onDelete('cascade');

        $table->index(['followable_id', 'followable_type']);

    });

Then I run the query like so:

    return $this->timeline->select('timeline.*')
        ->friendsAndFollowers()
        ->orderBy('timeline.created_at', 'desc')
        ->paginate(10);

I only have 10000 rows in each table, so it's not big at all, I have indexes for the relationships, any idea on what is wrong?

7th October, 2017

nc1991 left a reply on Relationships, Get Products Where Relationship Table Belongs To The User • 11 months ago

I manage to get it to work via the hasthroughmany

/**
 * The products the user has sold
 */
public function sold()
{
    return $this->hasManyThrough('App\Models\Market\Purchases', 'App\Models\Market\Product');
}

5th October, 2017

nc1991 left a reply on Relationships, Get Products Where Relationship Table Belongs To The User • 11 months ago

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

nc1991 left a reply on Relationships, Get Products Where Relationship Table Belongs To The User • 11 months ago

I'm not trying to get the items I've purchased :(, because I could just use Auth::user()->purchases, I'm just trying to get all rows from purchases where the product_id, belongs to a product I own

for example

    select * from purchases
left join products
    on products.id = purchases.product_id
where products.user_id = loggedin user's id

nc1991 left a reply on Relationships, Get Products Where Relationship Table Belongs To The User • 11 months ago

This gets the products that are purchased, but not a product that I own (user_id, auth)

https://i.imgur.com/mSwp9H4.png

https://i.imgur.com/zFjUVLY.png

nc1991 left a reply on Relationships, Get Products Where Relationship Table Belongs To The User • 11 months ago

Illuminate \ Database \ Eloquent \ RelationNotFoundException
Call to undefined relationship [product] on model [App\Models\Market\Product].

https://i.imgur.com/zKeyfpU.png

Also, the builder in case you need it: https://i.imgur.com/wiz3Hhc.png

nc1991 left a reply on Relationships, Get Products Where Relationship Table Belongs To The User • 11 months ago

So I am still getting that error, I have the relationships like so:

user.php

public function products()
{
    return $this->hasMany('App\Models\Market\Product');    
}

public function purchases()
{
    return $this->hasMany('App\Models\Market\Product');    
}

product.php

public function user()
{
    return $this->belongsTo('App\Models\Account\User');
}

public function purchases()
{
    return $this->belongsTo('App\Models\Market\Purchases');    
}

purchases.php

public function product()
{
    return $this->belongsTo('App\Models\Market\Product');    
}

public function user()
{
    return $this->belongsTo('App\Models\Account\User');    
}  

Any idea?

nc1991 left a reply on Relationships, Get Products Where Relationship Table Belongs To The User • 11 months ago

I seem to be getting an error doing that

Call to undefined relationship [product] on model [App\Models\Market\Product].

nc1991 started a new conversation Relationships, Get Products Where Relationship Table Belongs To The User • 11 months ago

So I have 2 tables products, and purchases, I am trying to get the purchases that belong to the owner of the product. Anyone can purchases, but only the user owns the product

I have my tables like so:

Schema::create('products', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id');
    $table->string('title');
    $table->text('description');
    $table->string('category');
    $table->string('auction_type');
    $table->string('allow_offers');
    $table->string('condition');
    $table->integer('quantity');
    $table->integer('auctionlength');
    $table->decimal('buyitnow_price');
    $table->decimal('auction_price');
    $table->string('shipping');
    $table->decimal('shipping_cost');
    $table->integer('shipping_time');
    $table->string('international_shipping');
    $table->string('location');
    $table->string('allow_returns');
    $table->date('enddate');
    $table->timestamps();
    $table->softDeletes();           
});

Schema::create('purchases', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id');
    $table->integer('product_id');
    $table->decimal('amount');
    $table->string('name');
    $table->string('address');
    $table->string('city');
    $table->string('state');
    $table->string('country');
    $table->string('postal');
    $table->string('email');
    $table->string('status');
    $table->timestamps();
    $table->softDeletes();
});

My two models are Purchases and Products, without raw query, how can I get the purcahse of products that belongs to user_id

15th February, 2017

nc1991 started a new conversation Is It Possible To Append A Package Resource To A Core Template • 1 year ago

For example, I have modules like so: polls, albums (pictures), now on my timeline I have hardcoded icons and javascript in my timeline:

I plan on creating modules for my scripts, so those modules won't installed by default, I can manually install the module via adding the views in the main layouts, but when I release my script, people won't know how to do it,

I've looked throughlaravel documentation to achieve this, but haven't found anything

For example, this is my status partials in a timeline modules:

<div class="box profile-info n-border-top">
    <form action="{{ route('CreateStatus') }}" method="post">
        {{csrf_field()}}
        <textarea class="form-control input-lg p-text-area" name="status" rows="2" placeholder="Whats in your mind today?"></textarea>
        <div class="box-footer box-form">
            <input type="submit" value="submit" class="btn btn-azure pull-right">
            <ul class="nav nav-pills">
                <li><a href="#"><i class="fa fa-map-marker"></i></a></li>
                <li><a href="#"><i class="fa fa-camera"></i></a></li>
                <li><a href="#"><i class=" fa fa-film"></i></a></li>
                <li><a href="#"><i class="fa fa-microphone"></i></a></li>
            </ul>
        </div>
    </form>
</div>

So, is there a way with packages to add partials from another module to another module automatically?

5th February, 2017

nc1991 started a new conversation My Vue Component Is Not Displaying • 1 year ago

I have a vue component, the /people is a json of all the people on the website like so:

        <div class="c-info">
            <strong>@{{ people.name }}</strong>
            <small></small>
        </div>

        <div class="c-footer">
            <button class="waves-effect"><i class="zmdi zmdi-person-add"></i> Add
            </button>
        </div>
    </div>
</div>

export default {
    data: function(){
        return {
            persons: []
        }
    },

    mounted: function()
    {
        this.getPeople();
    },

    methods:
    {
        getPeople: function() {
            axios.get("/people").then(function(response) {
                this.persons = response.data;
            }.bind(this));
        }
    }
}

That code is in Example.vue, and in my app.js I have the following code:

Vue.component('all-people', require('./components/Example.vue'));

const app = new Vue({ el: '#app', });

Now In my blade file I have the following HTML

@extends('layouts.main') @section('content')

    <div id="app">
        <all-people></all-people>
    </div>
</div>

@stop

When I load my page I'm getting no errors, but the template ins't displaying, here is the source:

How come it isn't working?

Edit Your Profile
Update

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