samedi 2 avril 2016

Vuejs ajax GET request not returning data in Laravel 5.1 Blade template

am trying to retrieve data from a database using vuejs ajax call with a plugin called vue-resource. Unfortunately, the json data object contains the html page and not the actual data from the database. Can someone please tell me what am doing wrong? This is my code:

routes.php

    <?php
Route::get('find', 'FruitsCtrl@index');

fruitsctrl.php (controller)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Fruit;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class FruitsCtrl extends Controller
{
    public function index(Request $req){
        $fruits = Fruit::all();
        return view('fruitsHome', $fruits);
    }
}

fruit.php (model)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Fruit extends Model
{
    protected $fillable = [
            'id', 'name', 'type'
    ];
}

fruitshome.blade.php (view)

<head>
        <meta id="token" content="{{ csrf_token() }}">
</head>
<body>
    <div class="row" id="vapp">
        <ul>
            <li v-for="fruits in fruit">
                @{{ fruit.name }}
                @{{ fruit.type }}

            </li>
        </ul>
    </div>
<body>

app.js (javascript)

Vue.http.headers.common['X-CSRF-TOKEN'] = document.querySelector('#token').getAttribute('content');
var v = new Vue({
    el : "#vapp",
    ready :function () {
        this.fetchEvents();
    },
    data : {
        fruit : {id:'', name:'', type:''},
        events : []
    },
    methods : {
        fetchEvents : function(){
            this.$http.get('/').then(function(res){
                this.events = res;
            },function (data){
                console.log(error ...);
            });
        }
    }
});



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1TrAnFb
via IFTTT

JWT Token That Never Expires For A Specific Route Group

I have a laravel web application. Several 3rd parties connect to the APIs on my server, the website uses those same APIs and so does my mobile app. Now for the website I am fine with the token expiring after a certain time preferably 1 hour. However for my mobile app I do not want the token to expire at all.

I know with Tymon/JWTAuth I can do this in the config file or my .env file but this applies to the entire app, how do I set specific token expiration lengths for each route group I have?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1V15M2y
via IFTTT

Laravel 5 Ajax Post route

This is my first attempt to use ajax to Post instead of Get. I get a 200 response as if it were working but the function in the controller is never being run.

I used this same concept in my ajax Get requests and they work fine but the Post is not working as expected and sortable('serialize') creates a Post variable so I need to use Post.

The alert in the success: function always runs as if it were successful but the controller function is never hit (I have it making a simple database change just to verify if it is running).

Ajax:

$(function() {
  $('[id^="sortable_"]').sortable(
    {
        connectWith: '.sortable-line-item-list-5',    
        update : function (event, ui) 
        { 
        var items = $(this).sortable('serialize');  
            $.ajax({
                type: 'post',
                url: '/api/sort_order_item',
                data: {
                    'items': items,
                },
                success: function()
                {
                    alert('looks like it is working...');
                },
            });
        }
    });
    $( '[id^="sortable_"]' ).disableSelection();
});

Route:

Route::post('api/sort_order_item', ['as' => 'api/sort_order_item', 'uses' =>'ApiController@SortOrderItem']);

Controller:

public function SortOrderItem()
{
    $this_item = \pmms\Checklist_template_line_item::findOrFail(20);
    $this_item->list_position = 1;
    $this_item->save(); 
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1RTR8tt
via IFTTT

Laravel 5.0 How to get a value on a pivot table

I am an absolute beginner of laravel.

I would like to get "role_id" on the pivot table or "admin", but I don't know how. I would like to make a system that shows a different interface based on what role the user has. So I would like to distinguish users from 'admin', 'instructor', and 'user' by getting "role_id" on the pivot table.

Any advices would be appreciated. Thanks in advance

Users.php

public function users(){
    return $this->belongsToMany('App\User');
}

Roles.php

public function roles(){
    return $this->belongsToMany('App\Role')->withTimestamps();
}

Role table:

1|admin|2016-04-02 16:51:25|2016-04-02 16:51:25
2|instructor|2016-04-02 16:51:25|2016-04-02 16:51:25
3|student|2016-04-02 16:51:25|2016-04-02 16:51:25

I would like to get role_id on the pivot table.

{
"id": "1",
"name": "admin",
"created_at": "2016-04-02 16:51:25",
"updated_at": "2016-04-02 16:51:25",

 "pivot": {
     "user_id": "1",
     "role_id": "1",
     "created_at": "2016-04-02 16:54:06",
     "updated_at": "2016-04-02 16:54:06"
  }

}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1SvgRTz
via IFTTT

ErrorBag is always empty in Laravel 5.2

I was creating a new Laravel project and when debugging the errors of a form request, I noticed that my ErrorBag was always empty. Even when in the controller $validator->fails() returned true. I tried every solution I found on the internet but nothing worked.

Even when creating a fresh project with composer (as described here) my out of the box auth system has an empty error bag when leaving all the field empty.

And yes, all the routes are using the middleware group web.

Any ideas what is causing this problem? (Session::put() and Session::get() are working)



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1SveqAf
via IFTTT

Combining Form Requests in Laravel

I have two requests that validate forms:

  • StoreProductRequest (validates a newly created product)
  • StoreProductBuildRequest (validates the components within the product)

In the 'edit' view, this functionality is combined into one form.

What is the best way to run through both form requests when submitting this form? I would rather avoid creating a new request (StoreProductAndBuildRequest?) as this isn't very DRY.

Related, but I wonder if there's a cleaner / more Laravel way: How to merge two Request in Laravel



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1VjDBuw
via IFTTT

call a non static function in a static function in laravel 5

i am using laravel 5. And in a model i have a static function which i am calling in controller. It's working fine but i want same changes in this function with another non static function and when i am calling it inside static function it produce error.

Non-static method App\Models\Course::_check_existing_course() should not be called statically

Here is my model

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

    class Course extends Model {
        public $course_list;
        protected $primaryKey = "id";
        public function questions(){
            return $this->belongsToMany('App\Models\Question','course_questions')->where("status",1)->orderBy("id","DESC");
        }

        public static function courses_list(){
            self::_check_existing_course();
        }
        private function _check_existing_course(){
            if(empty($this->course_list)){
                $this->course_list = self::where("status",1)->orderBy("course")->get();
            }
            return $this->course_list;
        }
    }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1REdk5i
via IFTTT