Datatable JSON parsing error - php

I am trying to display some data in a datatable in my laravel project but I encounter an error which says my data is not in the correct format I know the data is in the correct format as I have other datatables using the same data here is my controller:
<?php
namespace App\Http\Controllers;
use Datatable;
use View;
use App\Models\EC2Instance;
use App\Models\ChangedProperties;
use Illuminate\Support\Facades\Input;
use App\Models\ChangelogItem;
class ChangedPropertiesController extends Controller {
protected $layout = 'changed_properties';
public function details($changelog_item_id)
{
$changed_property = new ChangedProperties;
$changed_properties = $changed_property->where('changelog_item_id', $changelog_item_id)->get();
$table = Datatable::table()
->addColumn(
'Changed Property', 'Change Type', 'Changelog Item ID', 'Previous Value', 'Updated Value'
)
->setUrl('/changed_properties/'. $changed_properties)
->noScript();
return View::make('changed_properties')
->with('property',$changed_properties)
->with('table', $table);
}
public function instance_details($changelog_item_id)
{
$query = ChangedProperties::select(array('resource_id',
'resource_type',
'changed_property',
'change_type',
'previous_value',
'updated_value',
'changelog_item_id'
))
->get();
return Datatable::collection($query)
->showColumns(
'changed_property', 'change_type','changelog_item_id', 'previous_value', 'updated_value')
->searchColumns( 'changed_property', 'change_type', 'previous_value', 'updated_value')
->orderColumns( 'changed_property', 'change_type', 'previous_value', 'updated_value'
)
->make();
}
}
and routes:
/Homepage Route
Route::get('/', 'CRUDController#users');
// Route which populates datatable in homepage
Route::get('search', array('as' => 'instance.search', 'uses' => 'CRUDController#instances'));
//route which renders instance details
Route::get('/instance_details/{instance_id}','DetailsController#details');
//route which links to individual instance details
Route::get('/ec2_instance/{resource_type}/{resource_id}',array('as'=>'InstanceId', 'uses'=>'DetailsController#instance_details'));
//route which renders changed properties for changelog item
Route::get('/changed_properties/{changelog_item_id}/','ChangedPropertiesController#details');
//route which links to individual instance details
Route::get('/changed_properties/{changlog_item_id}/',array('as'=>'ChangelogItemId', 'uses'=>'ChangedPropertiesController#instance_details'));
Route::resource('sns', 'SNSController');

Related

Laravel Undefined variable: intent Facade\Ignition\Exceptions\ViewException

I tried looking for all the possible solutions none of it worked and this is very basic trying to send data from a controller to view in Laravel.
Paymentcontroller
public function payment() {
$plans =[
'Basic' => "Monthly"
];
$intent = $user->createSetupIntent();
return view('pages.subscription', compact('intent', 'plans'));
}
PageController
public function index(string $page)
{
if (view()->exists("pages.{$page}")) {
return view("pages.{$page}");
}
return abort(404);
}
View pages.subscription
<div>
{{ $intent }}
</div>
route
Route::get('{page}', ['as' => 'page.index', 'uses' => 'PageController#index']);
Route::get('/subscription', 'PaymentController#payment');
This makes the page work but doesn't display the data
Move Route::get('/subscription', 'PaymentController#payment'); before Route::get('{page}',.... (it should be your last route in the list).
Currently when you call /subscription endpoint you are calling PageController#index, but it doesn't contain logic of your PaymentController#payment and doesn't pass any data to view.

backpack laravel - preselect select filter with $_GET param

I have a Document and Post (for that document) Backpack CRUD resources. I want to create a button inside the post datatable so that when I click that button, it will link to the documents of that post.
this is my model post
class Post extends Model{
...
function allDocuments(){
return '<i class="fa fa-list"></i> Documents';
}
}
this is my PostCrudController
class PostCrudController extends CrudController{
...
$this->crud->addButtonFromModelFunction('line', 'all_documents', 'allDocuments', 'beginning');
}
and that's my DocumentCrudController
class DocumentCrudController extends CrudController{
...
$this->crud->addFilter([
'name' => 'post_id',
'type' => 'select2',
'label'=> 'Post To Filter',
'value' => $_GET['post_id'] // <------- Hoped this works, but doesn't
], function() {
$condos = [];
foreach(Post::get() as $c){
$condos[$c->id] = $c->title." >> ".$c->category->name." >> ".$c->category->condomimium->name;
}
return $condos;
}, function($value) { // if the filter is active
$this->crud->addClause('where', 'post_id', $value);
});
}
I've seen that backpack datatables uses a POST request with the parameters (in my case post_id) to filter the datatable, but I need to call a GET request to preselect my filter with the datatable result accordingly.
thanks in advance
Why not using Request in laravel ? something like this $request->input(post_id); and instantiate the request in the method like this
use Illuminate\Http\Request;
public function store(Request $request) { // logic here }
Actually it works even without 'value' => $_GET['post_id']. Backpack recognizes automatically filter. The name of the filter must match to the name of the GET param

Laravel - route within view isn't working

I'm learning laravel, working with 3 files, Welcome.blade.php / route.php / tryaction.php and it's a controller.
I made three links that fetched from database table => hug, greet and slap
when I click any link it gives me an error that actions is not defined.
my Welcome.blade.php:
<ul>
#foreach ($actions as $action)
<li>{{$action->name}}</li>
#endforeach
</ul>
my route.php:
<?php
Route::get('/', [
'uses' => 'tryaction#getHome',
]);
//to deal with get requests
Route::get('/{action}/{name?}', [
'uses' => 'tryaction#doget',
'as' => 'benice'
]);
my tryaction.php controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\actionstable;
class tryaction extends Controller
{
public function doget($action, $name = null){
return view('actions.'.$action,['name'=>$name]);
}
public function getHome(){
$actions = actionstable::all();
return view('welcome',['actions'=>$actions]);
}
}
when I replace the href route in welcome.blade.php with # instead of {{ route('benice', ['action' => $action->name]) }} the error stops from showing on
the data are fetched correctly and the data is shown on the page .. the problem in the route and it's that the actions is not defined, here is the error page:

Laravel 5 Route binding and Hashid

I am using Hashid to hide the id of a resource in Laravel 5.
Here is the route bind in the routes file:
Route::bind('schedule', function($value, $route)
{
$hashids = new Hashids\Hashids(env('APP_KEY'),8);
if( isset($hashids->decode($value)[0]) )
{
$id = $hashids->decode($value)[0];
return App\Schedule::findOrFail($id);
}
App::abort(404);
});
And in the model:
public function getRouteKey()
{
$hashids = new \Hashids\Hashids(env('APP_KEY'),8);
return $hashids->encode($this->getKey());
}
Now this works fine the resource displays perfectly and the ID is hashed.
BUT when I go to my create route, it 404's - if I remove App::abort(404) the create route goes to the resource 'show' view without any data...
Here is the Create route:
Route::get('schedules/create', [
'uses' => 'SchedulesController#create',
'as' => 'schedules.create'
]);
The Show route:
Route::get('schedules/{schedule}', [
'uses' => 'Schedules Controller#show',
'as' => 'schedules.show'
]);
I am also binding the model to the route:
Route::model('schedule', 'App\Schedule');
Any ideas why my create view is not showing correctly? The index view displays fine.
Turns out to solve this, I had to rearrange my crud routes.
Create needed to come before the Show route...
There's a package that does exactly what you want to do: https://github.com/balping/laravel-hashslug
Also note, that it's not a good idea to use APP_KEY as salt because it can be exposed.
Using the above package all you need to do is add a trait and typehint in controller:
class Post extends Model {
use HasHashSlug;
}
// routes/web.php
Route::resource('/posts', 'PostController');
// app/Http/Controllers/PostController.php
public function show(Post $post){
return view('post.show', compact('post'));
}

Creating new data using POST dosen't work in laravel 5

I've got a problem moving from laravel 4 to laravel 5.
I hae a test example that i downloadet some time ago in laravel 4 and it works prefectly. But when i wrote the same example it stops at some point.
I'm using ExtJS 5 for framework.
Controller:
<?php namespace App\Http\Controllers\books;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class BookController extends Controller {
public function load()
{
$books = DB::table('books')->select('id','title','author','price','quantity')
->orderBy('id','asc')->get();
// dodajemo u array polje cijena koja množi cijenu i količinu
foreach ($books as $b)
{
$b->cijena = $b->price * $b->quantity;
}
die(print_r($data));
//die(print_r($books));
return Response::json($books);
//print_r($x);
}
public function create()
{
$input = Input::all();
if ($input['id'] == '')
{
$books = DB::table('books')->insertGetId(array('title' => $input['title'], 'author' => $input['author'], 'price' => $input['price'], 'quantity' => $input['quantity']));
return Response::json(array('msg'=>'New Books record successfully created.'));
}
else
{
$book = DB::table('books')->where('id', '=', $input['id']);
$book -> update($input);
return Response::json(array('msg' => 'Book successfully update.'));
}
}
public function delete($bookId)
{
//$x = $bookId;
//die(print_r($x));
$book = DB::table('books')->where('id', '=', $bookId)->delete();
}
}
Route:
<?php
Route::get('load_books', 'BookController#load');
Route::post('create', 'BookController#create');
Route::get('delete/{bookId}', 'BookController#delete');
Route::get ('/', function()
{
//return View::make('index');
return View::make('books');
});
when i open the aplication in browser i get the view and the form of ExtJS, thanks to #lukasgeiter i addet use DB & use Response and got the data shown.
In ExtJS POST methode is used to create new book
The problem now is when i try to create new book i get:
POST http://localhost/testni_server/artikli/public/books/create 500 (Internal Server Error)
And i can't figure out how to fix this problem.
Evrything is the same as in laravel 4, except it works in laravel 4, but not in laravel 5

Categories