mardi 1 décembre 2015

Determining If a File Exists in Laravel 5

Goal : If the file exist, load the file, else load the default.png.


I've tried

  @if(file_exists(public_path().'/images/photos/account/{{Auth::user()->account_id}}.png'))
    <img src="/images/photos/account/{{Auth::user()->account_id}}.png" alt="">
  @else
    <img src="/images/photos/account/default.png" alt="">
  @endif


Result

It kept load my default image while I'm 100% sure that 1002.png is exist.

How do I properly check if that file is exist ?



via Chebli Mohamed

How to know which fields were updated

I'm working in laravel 5, I have a module where the user can update the information of students, for that The user has a preloaded form with the current data and he can modify the fields that he wants.Then, in the controller I do:

$perfil->update(Input::all())

That works pretty good. So my question is: there's a way of get the name of the fields that have been updated?



via Chebli Mohamed

Laravel 5: Stunned why a query is not working at the else statement with exact same parameter

I am super stunned why this works for the first, but not for the else part. I have the following function:

public function category($slug) {
    if(!isset($slug[4])) {
        $category_id = ProductCategory::where('slug', $slug[3])->first()->pluck('id');
    } else {
        $parent_id = ProductCategory::where('slug', $slug[3])->first()->pluck('id');
        $category_id = ProductCategory::where('slug', $slug[4])->where('parent_id', $parent_id)->first()->pluck('id');
    }
    return $category_id;
}

$slug is an array like this:

Array ( [0] => [1] => shop [2] => category [3] => women )

or

Array ( [0] => [1] => shop [2] => category [3] => women [4] => skirts )

When $slug[4] is not available it returns the $category_id fine, but when $slug[4] is available, the exact same query ProductCategory::where('slug', $slug[3])->first()->pluck('id') gives null, while $slug[3] is in both cases the same...

A getQueryLog() gives me this:

Array (
    [query] => select * from `product_categories` where `slug` = ? limit 1
    [bindings] => Array ( [0] => women )
)



via Chebli Mohamed

Create data to be outputted in a view?

In my view I output data from my database in the view via:

{{ $data->id }}

In one particular view I need do not get the data from a database but need to keep the view the same and manually set the id.

I've tried setting the id in my controller like:

$data['id'=>1];

But this fails to output in the view with:

{{ $data->id }}

Where am I going wrong?



via Chebli Mohamed

PHP-Laravel5 except csrf from another website

I've tried to exclude requests from another localhost server (http://localhost:8080/order/placeorder) to another one localhost server (http://localhost:8000) I don't want to disable all csrf protection by removing \App\Http\Middleware\VerifyCsrfToken::class, in Illuminate\Foundation\Http\Kernel.php

I've tried to modify app/Http/Middleware/VerifyCsrfToken.php

protected $except = [
    'http://localhost:8080/*',
    'http://localhost:8080',
    '/order/placeorder/*',
    'http://localhost:8080/order/placeorder'
];

and I also tried this way

private $openRoutes = [
    'http://localhost:8080/*',
    'http://localhost:8080',
    '/order/placeorder/*',
    'http://localhost:8080/order/placeorder'
];

public function handle($request, Closure $next)
{
    //add this condition
    foreach($this->openRoutes as $route) {

        if ($request->is($route)) {
            return $next($request);
        }
    }

    return parent::handle($request, $next);
}

But I still got this error --->> TokenMismatchException in VerifyCsrfToken.php Can anyone suggest me what should I do and what I've done wrong? Thank you



via Chebli Mohamed

How to pass captured parameter in route to middleware?

If I have middleware like:

<?php namespace App\Http\Middleware;

class SomeMiddleware
{
    public function handle($request, Closure $next, $id = null)
    {
        //
    }
}

In kernel.php:

'someMiddleware'    => \App\Http\Middleware\SomeMiddleware::class,

In routes.php :

Route::put('post/{id}', ['middleware' => 'someMiddleware']);

How I can pass id captured in {id} to my middleware? I know that I can pass some custom parameter like this:

Route::put('post/{id}', ['middleware' => 'someMiddleware:16']);

But in laravel documentation there is no described how to pass argument captured in route pattern.



via Chebli Mohamed

Posting data using VueJs and Laravel 5

I am trying to do the most simple post using Vuejs and laravel but I keep getting an "error 500" along with a strange "Uncaught (in promise)" error when I look it up in chrome dev tools, so heres the code.

HTML

<html>
<head>
    <meta charset="UTF-8">
    <meta name="token" id="token" value="{{ csrf_token() }}">
    <title>Guestbook</title>
    <link rel="stylesheet" href="http://ift.tt/1K1B2rp">
</head>
<body>

<div id="chatbox">
    <div class="container">
        <div class="row">
            <form method="POST" v-on:submit="sendMessage">
                <h1 v-if="nameIsSet">@{{ userInfo.name }}</h1>
                <input type="text" placeholder="Name" v-model="userInfo.name" v-if="! nameIsSet"><button v-if="! nameIsSet" class="btn btn-info" v-on:click="setName">Set Name</button>
                <br>
                <input type="text" placeholder="Message" v-if="nameIsSet" v-model="userInfo.message"><button v-if="nameIsSet">Send Message</button>
                {{ csrf_field() }}
            </form>
        </div>
    </div>
</div>

<script src="http://ift.tt/1LL40sh"></script>
<script src="js/view-resource.min.js"></script>
<script src="js/guestbook.js"></script>
</body>
</html>

The VueJs Script

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('value');

new Vue({
    el: '#chatbox',
    data:{
        userInfo:{
            name: '',
            message: ''
        },
        nameIsSet: false
    },
    methods:{
        setName: function(){
            this.nameIsSet = true;
        },
        sendMessage: function(e){
            e.preventDefault();
            console.log(userInfo);
            var userInfo = this.userInfo;
            this.$http.post('api/messages', userInfo);
        }
    }
})

The Laravel 5 Routes

Route::get('/', function(){
    return view('guestbook');
});

//API

Route::get('api/messages', function(){
    return App\Message::all();
});

Route::post('api/messages', function(){
    App\Message::create(Request::all());
});

As described above it does not work, i'm not sure what the server side error is here anyone gots any ideas ^^?



via Chebli Mohamed