lanatel

Experience

3,710

6 Best Reply Awards

  • Member Since 3 Months Ago
  • 0 Lessons Completed
  • 0 Favorites

2nd August, 2018

lanatel left a reply on Laravel Updating Relationship Column In WhereHas • 2 weeks ago

If I got you right you want to update your salesdetails table. In this case your query cant be used because now you are trying to update Sales table.

You can try smth like that:

salesdetailsModel::whereHas('sale',  function ($query) use ($now) {
            $query->where(DB::raw("(DATE_FORMAT(transaction_date,'%Y-%m-%d'))"), '<', $now)
    ->where('product_id', '<=', 24)
    ->where('switch', 1)
    ->update(['switch' => 0]);

Or if you already have sale instance and want to update its details you can do

$sale->salesdetails()
    ->where('switch', 1)
    ->update(['switch' => 0])

20th June, 2018

lanatel started a new conversation Laravel Mix: Fonts Path • 2 months ago

Hi, Laravel-Mix copies my fonts from assets/dashboard/fonts/fontName to public/fonts/fontName. Is there any way to change it? I need to have in public/dashboard/fonts/fontName.

4th June, 2018

lanatel left a reply on Sum Over Multiple Column In HasMany Relation Table • 2 months ago

@floflo where do you want to call it? If you need to use it with your sum result than you can do it in the same query

Number::leftJoin(DB::raw('(SELECT number_id, SUM(total_bids) as binds_sum, SUM(total_calls) as calls_sum FROM AggregateStatisticTableName GROUP BY number_id) AS statistics'), function ($join) {
            $join->on('statistics.number_id', '=', 'numbers.id');
        })
    ->select('binds_sum', 'calls_sum ', DB::raw('calls_sum / binds_sum as percent'), 'numbers.id')
    ->get();

1st June, 2018

lanatel left a reply on Routing Active • 2 months ago

when you want to check your route name you can use Route::currentRouteName() . But it will give you a full name so you have to check it on every link or use extra functions.

I think in your case it is better to use Request::is($path). Cause here you can pass part of url (for ex. mylink/* ) and apply it to group of routes.

31st May, 2018

lanatel left a reply on Sum Over Multiple Column In HasMany Relation Table • 2 months ago

Than you can do something like that:

Number::leftJoin(DB::raw('(SELECT number_id, SUM(total_bids) as binds_sum, SUM(total_calls) as calls_sum FROM AggregateStatisticTableName GROUP BY number_id) AS statistics'), function ($join) {
            $join->on('statistics.number_id', '=', 'numbers.id');
        })->get();

It does not use your relationship but make just one query

lanatel left a reply on Sum Over Multiple Column In HasMany Relation Table • 2 months ago

Which column is a foreign key on the AggregateStatistic model?

18th May, 2018

lanatel left a reply on Looping Through Collection And Summing Values With Same Parent_id • 3 months ago

Model::groupBy('parent_id')
            ->select('parent_id', DB::raw('SUM(value) as sum'))
            ->get();

17th May, 2018

lanatel left a reply on Laravel Queues • 3 months ago

@Borisu I understand. But I need it because site moderators will create templates for mass mailing and it is possible that sometimes they will do it bad (for example send it to wrong type of users or just type a bad text). So in this case I want to be able to stop execution jobs that responsible for this emails

lanatel left a reply on Laravel Queues • 3 months ago

@Borisu thanks for reply. But my question is, what if I have for example 1000 jobs in one queue. Half of them have been executed and other half is still in the queue. Can I just stop executing and delete that jobs?

lanatel left a reply on How To Use The Correct UpdateOrcreate In Laravel? • 3 months ago

It is not the best idea to use updateOrCreate() in this case. First of all, your job_educations table doesn't need id column. Secondly, you can define a many to many relation https://laravel.com/docs/5.6/eloquent-relationships#many-to-many between your jobs and educations tables. Thus, you don't a JobEducation model at all. And then, with this relation you can use sync() or if you don't want to delete existing records syncWithoutDetaching() method. https://laravel.com/docs/5.6/eloquent-relationships#updating-many-to-many-relationships

16th May, 2018

lanatel left a reply on Get Count In Joining Multiple Table • 3 months ago

You can do it another way. If user can belong to many groups and group can have a lot of users you can define a many to many relationship https://laravel.com/docs/5.6/eloquent-relationships#many-to-many and then just call withCount() method

$group = Group::withCount('users')->first();
$count = $group->users_count;

lanatel started a new conversation Laravel Queues • 3 months ago

Hello guys. On my site, users will receive different types of emails. Some of them are related to registration on the site; others will be sent when the user asks questions, and so on. One type of emails is mass mailing. All of them will be queued with a different priority.

The queue worker will be running on a separate server (is it appropriate?), where the queues will be processed according to the priority. Bulk mailing will be in the same queue, but it should have some extra parameter that I can separate them later (for example, one day I have to send to all users a notification about the promotion on the site and these messages should be in one group, and after some time I will send messages on a completely different topic and they also should be separated). I need this to be able to delete all jobs that have not been executed yet and which handle sending emails from specific group or pause the job executions and continue it after a certain time.

So the main question if it is possible to implement. I have read about Laravel Horizon (I use Redis) and know about the tag existence. Can I use them to achieve my goal or they are used only for monitoring jobs?

I will be glad to hear any thoughts, ideas and advices. Thank you

11th May, 2018

lanatel left a reply on Attributes For Products In Given Category • 3 months ago

@GoldeNx3 maybe smth like this

Product::whereHas('attributes', function ($query) {
            $query->where('attribute_id', $idOfAttributeYouInterestedIn)
            ->where('value', $specificValue);
        })->get());

lanatel left a reply on SetAttribute In Model • 3 months ago

@tykus yes, you are right. Why didn't I think of it? Thanks

lanatel left a reply on SetAttribute In Model • 3 months ago

You can do it when you a create migration:

$table->string('school_code')->default(str_random(8));

or in your Model

protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->school_code= str_random(8);
        });
    }

lanatel left a reply on Class Doesn't Exist • 3 months ago

Did you add a namespace in your Vivense class?

10th May, 2018

lanatel left a reply on Select JSON Data From DB And Display • 3 months ago

@trihead in this case $value is your json from db.

But m-rk's way is better and easier. Just try to work with arrays and you will see how easy it is.

lanatel left a reply on Select JSON Data From DB And Display • 3 months ago

You can have accessor in your model class

public function getMetaAttribute($value)
{
    return json_decode($value);
}

Than you can use it like a regular array

lanatel left a reply on Route [login] Not Defined. • 3 months ago

return view('auth.login');

lanatel left a reply on WhereIn Only Matching Values With Two Units, Not Three • 3 months ago

If it works with your test array it should work

$first = Users::select('id')->where('column', condition1)->where('column2', condition2)->pluck('id')->toArray();

lanatel left a reply on Query Result From Database • 3 months ago

first of all rename you classrooms relation to classroom. And maybe try just get $student->classroom. Are there any results?

lanatel left a reply on Query Result From Database • 3 months ago

@bincy123 if you don't get any errors, be sure to fill your db tables with data. Maybe yo just get empty result

lanatel left a reply on Query Result From Database • 3 months ago

@bincy123 can you dump your result? You should get results from both tables by using $student->galleries and $student->classroom->galleries.

lanatel left a reply on Query Result From Database • 3 months ago

@bincy123

Student::where('id', $student_id)->with(['galeries', 'classroom.galleries'])->get();

lanatel left a reply on SQLSTATE[42000]: Syntax Error Or Access Violation: 1067 Invalid Default Value For 'dates' • 3 months ago

$table->time('dates')->default(date('H:i:s'));

lanatel left a reply on Attributes For Products In Given Category • 3 months ago

Products, Categories and Attributes table can have everything you need. Then attribute_category table should have 2 columns attribute_id and category_id. And attribute_product should have 3 columns: attribute_id, product_id, value

https://laravel.com/docs/5.6/eloquent-relationships#many-to-many here is a great explanation of Many To Many relation

lanatel left a reply on Query Result From Database • 3 months ago

In your Student model

public function galleries()
    {
        return $this->hasMany(Gallery::class);
    }

    public function classroom()
    {
        return $this->belongsTo(Classroom::class);
    }

In Classroom model:

public function galleries()
    {
        return $this->hasMany(SharedGallery::class);
    }

And than you can do:

Student::with(['galeries', 'classroom.galleries'])->get();

lanatel left a reply on Attributes For Products In Given Category • 3 months ago

For example, you can have the folowing db structure: - products table (with category_id column) - categories table - attributes table (with all possible attributes) - attribute_category table (with belongsToMany relation) - attribute_product (also with belongsToMany relation, but beside of attribute_id and product_id you can have there value column where you can store attribute value of a given product)

So when you want to add new product, you select a category, then you get all attributes that have to be presented for a given category and just insert it to attribute_product table.

I don't know if it is a good solution but it should work :)

8th May, 2018

lanatel left a reply on Search Method Which Queries By Nested Relationship Fields • 3 months ago

If I got you right, you can try something like that

$investors = User::with([
    'investments',
     'investor' => function ($query) use ($request) {
        $query->when($request->firstname, function($query) use ($request){
                return $query->where('investor.firstname', 'like', "%{$request->firstname}%");
                return $query->where('investor.firstname', 'LIKE', "%$request->firstname%");
            })      
            ->when($request->lastname, function($query) use ($request){
                return $query->where('investor.lastname', 'like', "%{$request->lastname}%");
                return $query->where('investor.lastname', 'LIKE', "%$request->lastname%");
            })
       ->when($request->phone, function($query) use ($request){
                return $query->where('investor.phone', 'like', "%{$request->phone}%");
                return $query->where('investor.phone', 'LIKE', "%$request->phone%");
            })             
}])
->get()

Here you can get all users with filtered investors relation

7th May, 2018

lanatel left a reply on Group By • 3 months ago

you can do something like that

$qualityClaims = QualityClaim::SelectRaw(
            'defect,
            MONTHNAME(plm.quality_claims.created_on) month,
            COUNT(*) as count'
        )
        ->join('plm.defects', 'plm.quality_claims.defect_id', '=', 'plm.defects.id')
        ->where('plm.quality_claims.created_on', '>=', Carbon::now()->subMonth(12))
        ->where('plm.quality_claims.organization_id', '=', $id)
    ->get();

$result = $qualityClaims->groupBy('defect')->transform(function($item, $k) {
            return $item->groupBy('month');
        });

lanatel started a new conversation ShouldBeSearchable() Seems To Not Be Working • 3 months ago

Hello! I am using ElasticSearch in my project and I want to index conditionally, so I have the following method in my model class

public function shouldBeSearchable()
{
      return $this->paid;
}

When it return true everything works as expected, but if result is false I get this error

{"_index":"question","_type":"questions","_id":"72","_version":1,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":5,"_primary_term":1}

And that's obvious because I found method, which handles the saved event for the model. Here it is

public function saved($model)
{
        if (static::syncingDisabledFor($model)) {
            return;
        }

        if (! $model->shouldBeSearchable()) {
            $model->unsearchable();
            return;
        }

        $model->searchable();
    }

So, here it call unsearchable() method for model which doesnt exist in the search index and maybe because of that I have this not found error?

Edit Your Profile
Update

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