Alun

Alun

Member Since 1 Year Ago

Experience Points
1,790
Total
Experience

3,210 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
13
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
1,790 XP
Jun
30
3 months ago
Activity icon

Replied to Polymorphic Relation Working The Other Way Around

@SNAPEY - After a while reviewing it, I think I can get to see your point: there's a 1-M relation between InventoryItem and User, so what I should do in this case is either carry on with that relation and do the polymorfic M-M between InventoryItem and the possible items, or get rid of that model so I can relate those items with the User (in which case I'll have to create a method per possible polymorphic relation in the User model), am I right?

Activity icon

Replied to Polymorphic Relation Working The Other Way Around

@SNAPEY - Maybe I'm completely wrong here, but inventory is the pivot table to relate Users and Items, therefore it's a M-M between Items and Users, using user_inventories as pivot table? I'm starting to get a bit confused here

Activity icon

Replied to Polymorphic Relation Working The Other Way Around

@SNAPEY - And it is there in the migration: 3rd line " $table->unsignedBigInteger('user_id'); "

Activity icon

Replied to Polymorphic Relation Working The Other Way Around

@SNAPEY - Not quite, the different items already exist in the database, they're not created by users, they can select those items, therefore I think it sould be a M-M relation (a user can have many items, a item can belong to many users)

Activity icon

Replied to Polymorphic Relation Working The Other Way Around

@SNAPEY - also tried that, but same error

Activity icon

Replied to Polymorphic Relation Working The Other Way Around

@SNAPEY - Yeah, as I said in the code it looks fine, I just rushed to write it here, but those typos don't really exist, my bad.

Activity icon

Replied to Polymorphic Relation Working The Other Way Around

@SNAPEY - The User model is above, here's my repository where I try to retrieve it:

{
        return User::with([
            'profile',
            'other_relations_that_work_fine',
            'inventory'
        ])->find($id);
}
    
Activity icon

Replied to Polymorphic Relation Working The Other Way Around

@SNAPEY - Yep, I get to hit them, and there's where the SQL error I pasted above appears. I just realized there's a typo in return $this->mophedByMany(...), that's only where, the actual code is written properly with return $this->morphedByMany(...). Thanks for pointing that

Activity icon

Started a new conversation Polymorphic Relation Working The Other Way Around

I am trying to build an inventory for users in Laravel 5.8, however the items have their own properties, therefore I needed to set up a polymorphic relationship. When attaching items to users, it tries to add the model User to the table on itemable_type and the user's ID to itemable_id aswell as add the User's ID to user_id instead of the actual Item class, something I could workaround by passing the models I need to the attach() method, but when I try to retrieve them it tries to find item with itemable_type = 'App\Models\User', which makes me think something's completely wrong here. Can I have some orientation on how to solve it?

class User extends Model
{
    public function inventory()
    {
        return $this->morhpToMany(InventoryItem::class, 'itemable', 'user_inventories', null, 'itemable_id')
            ->withPivot('amount', 'notes');
    }
}
class InventoryItem extends Model
{
    public $timestamps = false;
    
    protected $table = 'character_inventories';
    
    protected $fillable = [
        'character_id', 'itemable_type', 'amount', 'parent_id', 'notes'
    ];

    public function cloth()
    {
        return $this->mophedByMany(Cloth::class, 'itemable');
    }
    
    public function food()
    {
        return $this->morphedByMany(Food::class, 'itemable');
    }

    // Other similar relations
}
// The Inventory migration:
 Schema::create('user_inventories', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id');
    $table->unsignedInteger('itemable_id');
    $table->string('itemable_type');
    $table->unsignedInteger('amount')->default(0);
    $table->text('notes', 65535)->nullable();

    $table->foreign('character_id')->references('id')->on('characters');
});

The expected result is the User model to have different items in his inventory, but the relation is trying to query by joinning to itself and filtering by user type instead of actual items.

The error:

Syntax error or access violation: 1066 Not unique table/alias: 'user_inventories' (SQL: 
select `user_inventories`.*, 
    `user_inventories`.`itemable_id` as `pivot_itemable_id`, 
    `user_inventories`.`itemable_type` as `pivot_itemable_type`, 
    `user_inventories`.`amount` as `pivot_amount`, 
    `user_inventories`.`parent_id` as `pivot_parent_id`, 
    `user_inventories`.`notes` as `pivot_notes` 
from `user_inventories` 
inner join `user_inventories` on `user_inventories`.`id` = `user_inventories`.`itemable_id` 
where `user_inventories`.`itemable_id` in (4) 
and `user_inventories`.`itemable_type` = App\Models\User)
Feb
17
7 months ago
Activity icon

Replied to Extending Vue SFC, Methods From Child Component Not Working

@EJDELMONICO - The thing about extending it is this way instead of using mixins is the capability of overriding the parent's methods, which don't happen whith the mixins. However, I think I know why is this happening: I'm extending from a component and then using the same component within the child, therefore the events on the parent always call to the parent's methods, nothing to do with the child. I changed the way to extending it and writing the template in the children (again) so I can use both, parent's and children's methods. The ugly thing here is the fact that I have to write the same template in the 3 components, but it's a minor issue if I can reuse the loginc

Feb
11
8 months ago
Activity icon

Started a new conversation Extending Vue SFC, Methods From Child Component Not Working

Hi there,

I have a base component which sometimes I need to extend with some extra functionality, they look like this:

// ParentComponent.vue
<template>
    <some-html>
        <more-html-stuff />
        <button type="button" class="btn" @click="debug()">Debug</button>
    </some-html>
</template>
<script>
export default {
    name: 'ParentComponent',
    props: {
        // some props
    },
    computed:{
        returnSomething(){
            return this.prop_X;
        }
    },
    methods: {
        // methods
        debug(){
            console.log('this is parent');
        }
    }
};
</script>

// Child_1.vue
<template>
    <parent-component v-bind="$props" />
</template>
<script>
import ParentComponent from './ParentComponent';
export default {
    name: 'ChildComponent',
    components: {ParentComponent},
    extends: ParentComponent,
    props: {
        another_property: {}
    },
    computed: {
        returnSomething(){
            return 'anotherThing';
        }
    },
    methods: {
        debug(){
            console.log('this is child');
        }
    }
};
</script>

By using ParentComponent as a component in the child, I can reutilize the template, however I think that's leading to an issue when I click the debug button and no matter what component I'm using, only the parent's method is being called.

Any orientation here please? Thank you all

Dec
08
10 months ago
Activity icon

Started a new conversation Passport Raising An Exception When CreateToken Is Called

Hi,

I've come to the situation in where I need both, a traditional login and an API login, so I decided to have a look to Laravel Passport. I had already the Laravel's Auth installed in my app, but to test the two of them coexisting I created a fresh project. First I installed the traditional login, and after I went for the Passport's. The idea was to generate a token when a user logs in, so I went with this code:

// HomeController.php
// Having the user logged in already:
public function index(){
    $user = Auth::user();
    $token = $user->createToken('Test token')->accessToken;
}

The method createToken() is raising an exception "Trying to get property of non-object", I debugged the process, and have found that, in the Eloquent's Builder, the where() method is getting passed "client-id" as second parameter, I'm not really sure, but the parameter's name is "$operator". "client-id" is what I defined in my AuthServiceProvider like so:

public function boot(){
    $this->registerPolicies();

    Passport::routes();
    Passport::personalAccessClientId('client-id');
}

I ran the install command right after installing the package and ran the migrations:

php artisan passport:install

Can it be considered a bug in the package because it's sending the wrong parameters, or am I doing something wrong?

Thank you

Nov
17
10 months ago
Activity icon

Replied to Dynamically Add Content To Bootstrap-vue Popover

In case someone wants to know, it was a nooby error: I forgot to change " content="body" " for " v-if="my.var.popover" " in the <b-popover> tag. It's solved and working properly now

Nov
16
11 months ago
Activity icon

Started a new conversation Dynamically Add Content To Bootstrap-vue Popover

Hi, I'm migrating my project from jquery + bootstrap + vuejs to only vuejs + bootstrap-vue. I used to have a few popovers within the main app and in some components. The components' have been updated properly by changing the markdown to <b-btn></b-btn><b-popover></b-popover>, however the ones in the main app are experiencing struggles with it: they used to be shown depending on a variable and the title and content is loaded via Ajax. Now with the new markdown they never get shown. Here are the old and new codes:

old file.blade.php:

<button type="button" 
    id="my_popover"
    class="btn btn-lg"
    data-toggle="popover"
    data-container="body"
    :title="my.var.name"
    :data-content="my.var.description"
    v-if="my.var.popover">
    <i class="fa fa-question-circle"></i>
</button>

old app.js

new Vue({
    data: {
        my: {
            var: {
                name: null,
                description: null,
                popover: false
            }
        }
    },
    methods(){
        myFunction(){
            let url = '/my_url';
            axios.get(url).then(response => {
                let data = response.data;
                this.my.var.title = data.title;
                this.my.var.description = data.description;
                this.setPopup('var');
            }).catch(error => {
                console.error(error);
                console.error(error.message);
            });
        },
        setPopup(v){
            this.my[v].popover = true;
            let pop = '#'+v+'_popover';
            this.$nextTick(() => {
                $(pop).popover({
                    content: 'body',
                    trigger: 'hover',
                    placement: 'right'
                });
            });
        }
    }
});

new file.blade.php:

<b-popover target="my_popover" placement="right" 
    :title="my.var.name" 
    triggers="hover" 
    ref="popover"
    content="body"
    placement="right"
    :content="my.var.description">
</b-popover>
<b-btn id="my_popover" class="btn-default" variant="default" v-if="my.var.popover">
    <i class="fa fa-question-circle"></i>
</b-btn>

new app.js

new Vue({
    data: {
        my: {
            var: {
                name: null,
                description: null,
                popover: false
            }
        }
    },
    methods(){
        myFunction(){
            let url = '/my_url';
            axios.get(url).then(response => {
                let data = response.data;
                this.my.var.title = data.title;
                this.my.var.description = data.description;
                this.setPopup('var');
            }).catch(error => {
                console.error(error);
                console.error(error.message);
            });
        },
        setPopup(v){
            this.$nextTick(() => {
                this.my[v].popover = true;
            });
        }
    }
});

The result here is nothing: the popover never appears (as for the documentation, if the content is null, the popover will not show up, which is fair, but the content is binded to a variable that actuallt changes).

Is there a way to make this work with ajax?

Thanks

Jul
25
1 year ago
Activity icon

Replied to WherePivot Using The Word 'pivot' Instead Of The Actual Table

@bobbybouwmann thanks for the response. That's not exactle what I'm trying to get, but it's my fault, my explanation war completely wrong. So, the need is a row from table A depending on table B's id and matching some pivot's params. The raw SQL looks like this:

select t1.*
from t1, pivot
where pivot.id_t3 = :param1
and pivot.pivot_a <= :param2
and pivot.pivot_b >= :param2
and pivot.id_t1 = t1.id
and t1.fieldX = :param3

I know I can do it woth a raw query and it'd save me hours as I have it done already, but I want to learn a bit more about Eloquent. I tried to match pivot.id_t1 = t1.id, but could not get to the correct method / syntax.

Activity icon

Replied to WherePivot Using The Word 'pivot' Instead Of The Actual Table

@bobbybouwmann the thing is that I need to retrieve this table as well as it's relations, how could I call them all together in one method?

Activity icon

Started a new conversation WherePivot Using The Word 'pivot' Instead Of The Actual Table

Hi, I'm trying to make a M2M relation using a custom table with extra values. This values need to be queried so I can retrieve the proper relation. It loosk like this:

    -table_a
        -id_a
        -col1
        -col2
        -colN
    -table_b
        -id_b
        -col3
        -col4
        -colM
    -table_pivot
        -id_pivot
        -id_a
        -id_b
        -pivot_1
        -pivot_2
        -pivot_N

Now in my model A I'm trying to do this:

public function getByPivot($val){
    return $this 
        ->wherePivot('pivot_a', '<=', $val)
        ->wherePivot('pivot_b', '>=', $val)->get();
}

public function table_b(){
    return $this->belongsToMany(Table_B::class, 'table_pivot', 'id_a', 'id_b')
        ->withPivot(['pivot_a', 'pivot_b']);
}

The SQL I'm getting with this is as below:

select * from `table_a` where `pivot` = pivot_a and `pivot` = pivot_b

I don't know if this is an actual bug or I'm doing something wrong, but the SQL I want to achieve is like this:

select * from `table_a` where `pivot_a` <= val and `pivot_b` >= val

How can I?

Thanks

Jul
12
1 year ago
Activity icon

Replied to Bootstrap's Popover Dinamically

Thanks for the quick response. I tried to add this 'data-content' like so (bear in mind that it's my first Vue's app, still learning it):

<div class="col-md-6">
    {{ Form::customButton(__('button'), ['field'=>'myField', 'activate'=>'nextField', '@click'=>'getRandomData', 'data-content'=>'myVar.myField.description']) }}
</div>

However this way doesn't seem to work either, the "myVar.myField.description" var doesn't get evaluated when I inspect it on the DOM inspector. I'm pretty sure I'm missing something, but don't know what... Any clue please?

Thank you!

-- EDIT:

I could achieve the binding by adding a colon to the ':data-content'=>'myVar.myField.description', now it gets binded properly, but the popover it's still not working, it just does not appear

Jul
11
1 year ago
Activity icon

Started a new conversation Bootstrap's Popover Dinamically

I'm making a form in which every field depends on the previous one, so it is mostly made in ajax: when a button is clicked, I'm calling the server via axios to ger a random data, and then this button gets disabled, and the next one gets enabled to repeat and get new data depending on the fist random data.

One of the fields I'm getting is a description ment to be in a popover, but of course it cannot be set at creation time as it's comig from the server later on. The code I'm trying to use unsuccessfully is as follows:

    <!-- template.blade.php -->
    <!-- there are several fields like this one -->
    <div class="col-md-3">
        <div id="myField_container" class="row">
            <div class="col-md-6">
                {{ Form::label('myField', __('myField')) }}
            </div>
            <div class="col-md-6">
                {{ Form::customButton(__('button'), ['field'=>'myField', 'activate'=>'nextField', '@click'=>'getRandomData']) }}
            </div>
            <div class="col-md-6">
                {{ Form::text('myField', old('myField'), ['class'=>'form-control', 'disabled'=>'disabled', 'v-model'=>'myVar.myField.name']) }}
            </div>
        </div>
    </div>
    // the_js_file.js
    app = new Vue({
        data: {
            myVar: {
                myField: {
                    id: null,
                    name: null,
                    description: null
                }
            }
        }
        methods: {
            setPopup(el, field){
               $(el)
                   .html('<i class="fa fa-question-circle"></i>')
                   .prop('title', this.myVar[field]['name'])
                   .data('toggle', 'popover')
                   .popover({
                       content: this.myVar[field]['descripcion']
                   });
            },
            getRandomData(e){
                var rn = this.getRandomNumber();
                var url = this.url('getRandomData?randomNumber='+rn);
                
                axios.get(url).then(response => {
                    var data = response.data[0];

                    // this sets the properties properly
                    this.setProps(data, 'myField', ['id', 'name', 'description']);

                    // but this one is failling
                    this.setPopup(e.target, 'myField');
                    this.swapToNext(e);
                }).catch(error => {
                    console.log('error: ');
                    console.log(error);
                });
            }
        }
    });

As I've read, DOM cannot be modified in runtime because it's not evaluated by Vue. In this case, the idea is to change the button's content, which is happening, via JQuery's .html(), and add the popover to this "new" element.

Is there any way to achieve this?

Thank you all.

Jul
05
1 year ago
Activity icon

Started a new conversation Accessor Not Working With Chunk()?

I'm new in Laravel, and checking this forum very often to solve lots of issues while learning. So I tried to create an accessor in one of my models to basically convert the colum "title" into a slug. I followed several answers from this forum, but none of them seems to work for me, basically the method I created doesn't get executed at all. Here's the code:

// app/Http/Models/Competence.php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use App\Http\Helpers\StringHelper;

class Competencia extends Model {
    
    protected $table = 't_competences';
    
    public $timestamps = false;
    
    protected $fillable = ['name', 'other_non_relevant_fields'];
    
    protected $appends = ['slug'];
    
    // ...
    
    public function getSlugAttribute(){
        return (new StringHelper())->slug($this->nombre);
    }
    
    // ...
}
<!-- app/resources/parts/competences.blade.php -->

@foreach($competences->chunk(2) as $c)
    @section('vars')
        <!-- Definition of some vars, I know it's a bad practice, but it's a need -->
        $classname = 'my-classes'
    @endsection
    <div id="{{ $c->slug }} class="{{ $classname }}>
        <!-- some more code -->
    </div>
@endforeach 

What I don't get to understand is why, if I remove the "chunk(2)" method, which I need so I can split the collection in two columns, the accessor works, but with it I get the exception "Property [slug] does not exist on this collection instance.".

Any idea of why this happens or any way to split this collection?

Thanks very much.