mercredi 27 juin 2018

Laravel 5.4 Spatie blade issue after deploying (probably a cache issue)

I have 02 env:

  1. local
  2. prod

Both have :

  • the same data, permission-role-user tables are populated by seeds
  • the same branch, and same version (git log returns the same last commit)

In fact, I tried to deploy last features which use new permissions and affect blade rendering. Below what I have in blade:

@can('manage_user’)

@endcan
@can('d_xxx’)

@endcan
@can('manage_manage_xxxx’)

@endcan

Locally, I have no issue

enter image description here

But in prod, Spatie seems ignoring existing permissions :(

enter image description here

I already tried those solution but without result:

  • php artisan config:clear
  • php artisan config:cache
  • php artisan view:clear
  • php artisan cache:forget spatie.permission.cache
  • restart the server

Has anyone faced this kind of issue, and what was the solution?



via Chebli Mohamed

Laravel: How to through 404 if the user enter the ID manually in the route?

Building an app (Blog/posts). Where only auth users can edit their post(which ofcourse belongs to them only). For example, Post with an id of 15 belongs to particular user, so the if he edits it, the route will be like this

http://localhost:8000/post/15/edit

this is correct.

But when the user enters any other post ID(which doesn't belongs to him) in the route, it shows

http://localhost:8000/post/16/edit

ErrorException (E_NOTICE)
Trying to get property 'user_id' of non-object

How to show unauthorised page in this case?



via Chebli Mohamed

composer global require "laravel/installer"

I'm having trouble asking you to help me install this error Laravel I'm a beginner I do not know where the problem is

[Composer\Downloader\TransportException]
The "http://packagist.org/p/provider-2013%24e48c5623bcfc80a0725d0e5e18083a9
2c1bb3b59e1591733c4ed8b07280b77d7.json" file could not be downloaded: faile
d to open stream: HTTP request failed!



via Chebli Mohamed

Mock classes on unit tests with Codeception and Laravel 5 module

In unit tests, this doesn't work:

class SomeClassCest
{
    public function tryToTest(UnitTester $I)
    {
        $mock = Mockery::mock(MyClass::class);
        $mock->shouldReceive('action')->once()->andReturn(true);
        $I->haveInstance(MyClass::class, $mock);

        $classToTest = app(SomeClass::class);
        // this method calls the MyClass action method
        $classToTest->run();

        // some asserts here
    }
}

The mock doesn't take effect, the $mock->action is never called, the real implementation is called instead. How to mock classes on unit tests with Codeception and the Laravel 5 module?



via Chebli Mohamed

Vue custom v-model value doesn't update the value after getting it from axios get request

I have a component called "TextInput":

            <template>
            <q-input
                v-model="content"
                @input="handleInput"
                color="secondary"
                :float-label="floatLabel" />
        </template>

        <script>
            import { QInput, QField } from "quasar-framework/dist/quasar.mat.esm";
            export default {
                props: ['floatLabel', 'value'],
                data () {
                    return {
                        content: this.value
                    }
                },
                components: {
                    'q-field': QField,
                    'q-input': QInput,
                },
                methods: {
                    handleInput(e) {
                        this.$emit('input', this.content)
                    }
                }
            }
        </script>

I used this component in another component:

            <template>
            <card card-title = "Edit Skill">
                <img slot="img" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAJeSURBVEhLzZVPSFRRGMVnKopI+odghFjWQDD05v8/dGCEaFEhbnwIQQTVol2rCHElQog7lwm6qdy0jBJpWQvBImgTEqGYoKIDYhS4qt9n38Qb5973ni7KA2fuPd937jt35t33JrKvUCqVjmaz2XI8Hm8qFArHmT8KS/ytehk7UqlUHPOzTCbzA36EDroNbsEnQcS/zFjWy5mRy+VuYaxiHIDNWo4wl6ANlb5g/VvfIAw3ZDfQ0dJfWIKi8uE4zil6LuuKon2DEonEMZpL6XT6ipbqsDOIi12EH/AnisViC/MqfK49exCN+xheqWyAN0huNN5FOAnlF/gMh+l3Sp+5b9AUu+tT2QBvEKfwMPMemXPR28wfy7wG3yCaa8lk8rzKBniDgmANkgCa6yqN8AYx3sX/xsB+6TOag2iM0phQaYQ3CL88V+5OUrefOp6byzSq+Xz+jJaM4AC049vEf8GPcv+MQRSn4UOVRnBIcixchfN4v1r4jf4vwmTj9UGIq/BLLBY7oiUj8IyxeEilEWymG88M0yj+WQI7/nQAhV6ac4xdWjKCRXfwzMMR/MMm0nvK+BO+gCvoE7p8G1GK9yguMG4/1TYQdg2f8U3tJdd5YH1M+NrnMFRV7hoE9MhfikpfHMC8xU5Oqg4NNnmWTW7K/5WW/IFZ3lcZlaHBBgfhmMpgYH5Jzk2VocG69/C6ymBglrf3u93+fKxb5aBcUhkM13UPEjTOwu+MtYfwtbatwL8B645yKHB6TrPDNIvlxflJy1bsOagGFpf/SZDcK4JKKq0gpKtSqRxS+b8QifwGm+z/Ksto7VkAAAAASUVORK5CYII=">
                <form class="clearfix" slot="form">
                    <bim-text v-model="skill.name" :floatLabel="input_label"></bim-text>
                    <q-btn
                        @click="edit"
                        icon="save"
                        size="14px"
                        color="secondary"
                        label="Save" />
                </form>
            </card>
        </template>

        <script>
            import { QBtn } from "quasar-framework/dist/quasar.mat.esm";
            import { Card, TextInput } from "../../base";
            import router from '../../../routes/routes';
            export default {
                data: function () {
                    return {
                        id: this.$route.params.id,
                        skill: {
                            name: ''
                        },
                        input_label: 'Skill Name'
                    }
                },
                components: {
                    'card': Card,
                    'bim-text': TextInput,
                    'q-btn': QBtn
                },
                methods: {
                    edit: function(){
                        axios.patch('/api/skills/'+this.id, {
                            name: this.skill.name,
                        }, { headers: { Authorization: 'Bearer '.concat(localStorage.getItem('token')) } })
                        .then(response => {
                        alert('success');
                        router.push({ name: "IndexSkills"});
                        }).catch(error => {
                            console.log('dd');
                        });
                    }
                },
                mounted() {
                    axios.get("/api/skills/"+this.id, { headers: { Authorization: 'Bearer '.concat(localStorage.getItem('token')) } })
                        .then(response => {
                            this.skill = response.data.data;
                        }).catch(error => {
                            alert('error')
                        });
                }
            }
        </script>

        <style scoped>
            .q-btn{
                float: right;
                margin-top: 20px;
            }
        </style>

As you can see, I created an skill object with empty property called name and I made an axios request to get the specified object using its id. In then function of the axios request skill should be updated but what happened was that the v-model still empty.

What would be the problem here? and how can I solve it?

Thanks in advance.



via Chebli Mohamed

Passing data to all views served by a controller in Laravel

I have a controller that has two methods, each serving up a different view:

class NavController extends Controller {
 public function posts()
 {
  $posts = Post::all();
  $tabs = Tag::all();
  return view('posts')->with('posts', => $posts, 'tags' => $tags);
 }
 public function users()
 {
  $users = User::all();
  $tabs = Tag::all();
  return view('users')->with('users', => $users, 'tags' => $tags);
 }
}

I wish to avoid writing the logic for retrieving and passing tags in each and every method and thus I am wondering if there is a way to retrieve the data in one place in the controller and then pass it by default to every view that is served up by this controller.



via Chebli Mohamed

How to set up cors in laravel 5.5?

I am doing project in reactjs and created api in laravel 5.5. My product manager told me to use cors library. I search alot and found so many library packages. Can you please suggest me the library which is more stable and reliable? And also simple to use. Thank You in advance.



via Chebli Mohamed