lanatel

Experience

3,070

5 Best Reply Awards

  • Member Since 2 Weeks Ago
  • 0 Lessons Completed
  • 0 Favorites

18th May, 2018

lanatel left a reply on Looping Through Collection And Summing Values With Same Parent_id • 1 week 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 • 1 week 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 • 1 week 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? • 1 week 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 • 1 week 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 • 1 week 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 • 2 weeks 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 • 2 weeks ago

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

lanatel left a reply on SetAttribute In Model • 2 weeks 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 • 2 weeks 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 • 2 weeks 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 • 2 weeks 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. • 2 weeks ago

return view('auth.login');

lanatel left a reply on WhereIn Only Matching Values With Two Units, Not Three • 2 weeks 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 • 2 weeks 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 • 2 weeks 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 • 2 weeks 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 • 2 weeks 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' • 2 weeks ago

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

lanatel left a reply on Attributes For Products In Given Category • 2 weeks 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 • 2 weeks 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 • 2 weeks 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 • 2 weeks 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 • 2 weeks 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 • 2 weeks 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.