lundi 29 octobre 2018

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?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2OfnVIW
via IFTTT

CKEditor 4 in Laravel 5 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?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2yzPXdd
via IFTTT

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!



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2OcIPIs
via IFTTT

public/index.php to remove / redirect to domain in laravel

I have developed one laravel application. Initially we have executing http://127.0.0.1/laravel_app/public this url structure.

Ater completing application i have moved to server with domain.

Example: http://www.example.com it working fine. I have integrated .htaccess to remove public. its working fine.

But i have try to http://www.example.com/public/index.php above url manually typed site will display.

How can remove if users enter manually enter this type of URL http://www.example.com/public/index.php

Kindly guide me.

Thank you.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2CNNYEX
via IFTTT

Update pivot table to create dynamic menu(menu and sub items) - Laravel

The case is : I want to make a dynamic sidebar when the user choose a book from the list -I used checkboxes -it should be saved in a pivot table with the category id so this will let me to to generate a dynamic sidebar for each user for example:

user 1 sidebar

- category 1

book 1 book 2

- category 2

book1 book2

My tables :

 user_book
-----------------------------------------
 user_id| book_id|category _id
-----------------------------------------


user_categories
-----------------------------------------
 user_id| book_id|category _id
-----------------------------------------


 books
-----------------------------------------
id| book_name|category_id|
-----------------------------------------

categories
-----------------------------------------
id| category|
-----------------------------------------

my code:

$user->books()->sync($request->input('books_ids'));// to save books ids -array of ids- but don't know how to insert the related category for each book in the pivot table.

and to generate my sidebar:

 $user->categories()->with('books')->get();

so my questions are : 1. is this the right way to make a dynamic sidebar and what is the best way? 2. how can I insert the related category in the pivot table ?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2qgi4JR
via IFTTT

Laravel, Vuejs redirect with params

I have an option to search for users, what i would like to achieve is for the search to return an array with users to a new page. I made this work with window.location after my request but that crashes when there is to many users and not a way i would like to achieve this.

If you know any other way to tackle this please let me know.

Here is what i am currently doing:

Search.vue:

<template>
 <input type="text" v-model="query" @keyup.enter="search()"/>
 <button @click="search()"  type="button">
</template>


<script>
export default {
    data() {
        return {
            users: [],
            query: ''
        }
    },
    methods: {
        search: function() {
            this.users = [];
            this.loading = true;
            axios.get('/api/search?q=' + this.query).then((response) => {
             this.users = response.data;
               this.loading = false;
               this.query = '';
               window.location = '/search/?users=' + JSON.stringify(this.users);
            });
        }
    }
};
</script>

SearchController.php:

class SearchController extends Controller
{
    public function search(Request $request)
    {
        $error = ['error' => 'No results found, please try with different keywords.'];

        if ($request->has('q')) {
            $users = User::search($request->get('q'))->get()->load('stats');
            return $users->count() ? $users : $error;
        }
        return $error;
    }
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2OaIE0p
via IFTTT

mapWithKeys in laravel ,i dont understand how do it work?

I saw the example of laravel, but i dont understand how do it work.

for this example:

$collection = collect([
    [
        'name' => 'John',
        'department' => 'Sales',
        'email' => 'john@example.com'
    ],
    [
        'name' => 'Jane',
        'department' => 'Marketing',
        'email' => 'jane@example.com'
    ]
]);

$keyed = $collection->mapWithKeys(function ($item) {
    return [$item['email'] => $item['name']];
});

$keyed->all();

someone can explain detail of it?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2qiqeBu
via IFTTT