lundi 29 octobre 2018

How to fix **Swift_TransportException** in Laravel 5.6

I am going to send contact form details via the gmail. My .env file is look like this,

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myname@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=null

but when I send submit buttons following error is coming,

 (1/1) Swift_TransportException

Expected response code 250 but got code "530", with message "530 5.7.0 Must issue a STARTTLS command first. u5-v6sm3569015pgk.46 - gsmtp
"

then How Can I fix this problem?



via Chebli Mohamed

Does hasManyThrough ignore global scopes on the intermediate table?

I have 3 tables: companies, games and tests.

  • Companies have many games
  • Games have many tests

The Game model has a global scope, which I can confirm is working:

public function apply(Builder $builder, Model $model)
{
    $builder->where('type', 'live');
}

Any direct queries I do using the Game model will only return results where the game type is set to "live".

I am using return $this->hasManyThrough('App\Test', 'App\Games') in my Company model to get all tests for a particular company.

However, this is returning results for all games, regardless of their type.

So I am wondering if using hasManyThrough bypasses the global scope that I've set in the Game model?

If so, is there any way around this? I want to make sure that all queries I'm doing are filtering out any games that aren't set to "live".

Cheers



via Chebli Mohamed

Eloquent manytomany polymorphic nested query withCount

I have the following manytomany polymorphic relationship

Asset

public function countries()
{
    return $this->morphToMany('App\Country', 'locationable');
}

Country

public function assets()
{
    return $this->morphedByMany('App\Asset', 'locationable');
}

Also Category has manytomany with Assets

public function assets()
    {
        return $this->belongsToMany('App\Asset', 'category_asset');
    }

I need to query a category and eager load assets that have country assigned.

Here is my eloquent query

$category = Category::with(['children.assets' => function ($query) {
            $query->whereHas('countries', function($q) {
                $q->where('code', '=', 'FR');
            });
        }])
            ->where('id', 1)
            ->first();

This seems to work, but then when I use the Category model with $this->assets It loads all of them, even if only one is returned in the query.

I am using API resources like so

AssetResource::collection($this->whenLoaded('assets'))

Where can I put a condition to only use assets that passed the condition where('code', '=', 'FR')



via Chebli Mohamed

Duplicate Queries in Laravel- Debugger

I am trying to update the columns of the records based on the other column records but it results in the duplicate queries since its being executed for each record in the table.I s there a way to avoid it and improve this code.

 public function updateReviewColumn(){
        $reviewData = $this->pluck('indicator')->toArray();
        foreach ($reviewData as $datum) {
            if ($datum == 'Y')
                $this->where('indicator', $datum)
                    ->update(['reviewindicator' => 'Yes']);
            else
                $this->where('indicator', $datum)
                    ->update(['reviewindicator' => 'No']);
        }
    }



via Chebli Mohamed

Is it possible to get data of related model inside current model in Laravel?

I'm building the Laravel app which using recursive url constructing. And I want to know is it possible to access data of the hasone related model within model to return constructed url direct to the view without interacting by controller.\

public function link(){
    var_dump($this->category());
    $url = ['news'];
    $url[] = $this->category()->url;
    $url[] = $this->url;
    return implode('/',$url);
}

Simple code example like this return this one

Undefined property: Illuminate\Database\Eloquent\Relations\HasOne::$url (View: /???/resources/views/common/news/full_preview.blade.php) (View: /???/resources/views/common/news/full_preview.blade.php) (View: /???/resources/views/common/news/full_preview.blade.php)

So is there any good way solve it by using just eloquent models, or it's only possible by using controllers and so on?



via Chebli Mohamed

CKEditor won't show the iFrame icon in toolbar

I'm using CkEditor in a Laravel 5 project.

In the config.js under bower_component/ckeditor/ I used the following code:

CKEDITOR.editorConfig = function (config) {
    // Define changes to default configuration here.
    // For complete reference see:
    // http://docs.ckeditor.com/#!/api/CKEDITOR.config

    // The toolbar groups arrangement, optimized for two toolbar rows.
    config.toolbarGroups = [
        {name: 'clipboard', groups: ['clipboard', 'undo']},
        {name: 'editing', groups: ['find', 'selection', 'spellchecker']},
        {name: 'links'},
        {name: 'insert'},
        {name: 'forms'},
        {name: 'tools'},
        {name: 'document', groups: ['mode', 'document', 'doctools']},
        {name: 'others'},
        '/',
        {name: 'basicstyles', groups: ['basicstyles', 'cleanup']},
        {name: 'paragraph', groups: ['list', 'indent', 'blocks', 'align', 'bidi']},
        {name: 'styles'},
        {name: 'colors'},
        {name: 'about'}
    ];

    // Remove some buttons provided by the standard plugins, which are
    // not needed in the Standard(s) toolbar.
    config.removeButtons = 'Underline,Subscript,Superscript';

    // Set the most common block elements.
    config.format_tags = 'p;h1;h2;h3;pre';

    // Simplify the dialog windows.
    config.removeDialogTabs = 'image:advanced;link:advanced';

    config.FormatOutput = false;

    config.allowedContent = true;
};

Also, in the css in the related page, I used the below code:

$(function () {

    CKEDITOR.replace('editor2', {
        allowedContent: true,
    });
});

In the HTML, I used the following code:

<textarea name="content" rows="10" cols="80" id="editor2"></textarea>

In the plugins folder under the bower_component/ckeditor/plugins, I see the "iframe" folder is exist. However, I can't see the iframe icon in the ckeditot toolbar. I configured "allowedContent" as true as mentioned above. Here is the screen grab:

enter image description here

What is the issue?



via Chebli Mohamed

Why deleting row in related row boot( is not trigegred?

In my Laravel 5.7 app I have 2 tables Tag, TagDetail(One to One relation) and the second table has image uploaded to storage and image field. I want using boot method for automatic deletion of related rows and image. As result deleting Tag row related TagDetail is deleted, but image of TagDetail is not deleted. I have 2 models and new Tag())->d( is just debugging function app/Tag.php :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use DB;
use App\MyAppModel;
use App\TagDetail;
use App\Http\Traits\funcsTrait;
use Illuminate\Validation\Rule;
use App\Rules\TagUniqueness;


class Tag extends MyAppModel
{
    use funcsTrait;

    protected $table = 'tags';

    protected $primaryKey = 'id';
    public $timestamps = false;
    private $votes_tag_type= 'votesTagType';

    public function getTableName() : string
    {
        return $this->table;
    }

    public function getPrimaryKey() : string
    {
        return $this->primaryKey;
    }

    public function tagDetail()
    {
        return $this->hasOne('App\TagDetail', 'tag_id', 'id');
    }

    protected static function boot() {
        parent::boot();
        static::deleting(function($tag) {
            with (new Tag())->d( '<pre>Tag BOOT $tag::' . $tag->id);
            $relatedTagDetail= $tag->tagDetail();
            if ( !empty($relatedTagDetail) ) {
                $relatedTagDetail->delete();  // I see this is triggered and  relatedTagDetail is deleted 
            }
        });
    }

and app/TagDetail.php :

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use DB;
use App\MyAppModel;
use App\library\ImagePreviewSize;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use App\Http\Traits\funcsTrait;

class TagDetail extends MyAppModel
{
    use Notifiable;
    use funcsTrait;

    protected $table = 'tag_details';
    protected $primaryKey = 'id';
    public $timestamps = false;

    protected $fillable = [
        'tag_id',
        'image',
        'description',
    ];

    public function getTableName() : string
    {
        return $this->table;
    }

    public function getPrimaryKey() : string
    {
        return $this->primaryKey;
    }

    public function Tag()
    {
        return $this->belongsTo('App\Tag', 'tag_id');
    }


    protected static function boot() {
        parent::boot();
        static::deleting(function($tagDetail) { // THIS METHOD IS NOT TRIGGERED AT ALL!
            with (new TagDetail())->d( '<pre>TagDetail BOOT $tagDetail::' . $tagDetail->id);

            $tag_detail_image_path= TagDetail::getTagDetailImagePath($tagDetail->id, $tagDetail->image, true);
            with (new TagDetail())->d( '<pre>TagDetail BOOT $tag_detail_image_path::' . $tag_detail_image_path);
            TagDetail::deleteFileByPath($tag_detail_image_path, true);
        });
    }

Is something wrong in my models declarations ?

Thanks!



via Chebli Mohamed