Laravel Nova: TypeError: Cannot read property 'status' of undefined - laravel

A bunch of my Nova ressources stopped working. When trying to create them, I get the following error in the console:
Uncaught (in promise) TypeError: Cannot read property 'status' of undefined
at a.<anonymous> (app.js?id=7319bf5027431449796c:1)
at y (app.js?id=7319bf5027431449796c:1)
at Generator._invoke (app.js?id=7319bf5027431449796c:1)
at Generator.e.(anonymous function) [as next] (http://url/vendor/nova/app.js?id=7319bf5027431449796c:1:460720)
at o (app.js?id=7319bf5027431449796c:1)
at app.js?id=7319bf5027431449796c:1
at new Promise (<anonymous>)
at new t (app.js?id=7319bf5027431449796c:1)
at a.<anonymous> (app.js?id=7319bf5027431449796c:1)
at a.<anonymous> (app.js?id=7319bf5027431449796c:1)
Nothing shows up in the error log at all. Any pointers to where I should be looking? Only affects some ressources, others are working fine.
Edit: Here is one of the affected Nova ressources:
<?php
namespace App\Nova;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\HasMany;
use Laravel\Nova\Fields\ID;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Naxon\NovaFieldSortable\Concerns\SortsIndexEntries;
use Naxon\NovaFieldSortable\Sortable;
class Unterprodukt extends Resource
{
use SortsIndexEntries;
public static $defaultSortField = 'order';
/**
* The model the resource corresponds to.
*
* #var string
*/
public static $model = 'App\Unterprodukt';
/**
* Get the displayble label of the resource.
*
* #return string
*/
public static function label()
{
return 'Unterprodukte';
}
/**
* Get the displayble singular label of the resource.
*
* #return string
*/
public static function singularLabel()
{
return 'Unterprodukt';
}
/**
* The logical group associated with the resource.
*
* #var string
*/
public static $group = 'Versicherung';
/**
* The single value that should be used to represent the resource when being displayed.
*
* #var string
*/
public static $title = 'name';
/**
* The columns that should be searched.
*
* #var array
*/
public static $search = [
'id',
'name',
];
/**
* Get the fields displayed by the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function fields(Request $request)
{
return [
ID::make()
->sortable()
->hideFromIndex(),
Text::make('Name', 'name')
->sortable(),
BelongsTo::make('Produkt', 'produkt', 'App\Nova\Produkt')
->sortable(),
Sortable::make('Reihenfolge', 'id')
->sortable(),
HasMany::make('Dokumente', 'dokumente', 'App\Nova\Dokument'),
];
}
/**
* Get the cards available for the request.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function actions(Request $request)
{
return [];
}
}

The quick fix for this is this way
Sortable::make('Order', 'id')->exceptOnForms()

I had this problem too, and it was because I had an incoherence between nova fields and migrations fields.
Now, another possible way to fix it is:
php artisan nova:publish
php artisan view:clear
Check this issue for more details: https://github.com/laravel/nova-issues/issues/1735

Related

Call to undefined method Illuminate\\Auth\\Access\\Response::authorize()

I'm trying to use Response::authorize() in ContactPolicy.php
but I got this error
Call to undefined method Illuminate\Auth\Access\Response::authorize()
<?php
namespace App\Policies;
use App\models\User;
use App\Contact;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;
class ContactPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can permanently delete the contact.
*
* #param \App\models\User $user
* #param \App\Contact $contact
* #return mixed
*/
public function forceDelete(User $user)
{
return $user->role === 'admin'
? Response::allow()
: Response::authorize();
}
}
I visit this website to check Response class Illuminate/Auth/Access/Response.html
then I checked file in my project vendor\laravel\framework\src\Illuminate\Auth\Access\Response.php
in my files I didn't find it ... all i find is this
<?php
namespace Illuminate\Auth\Access;
class Response
{
/**
* The response message.
*
* #var string|null
*/
protected $message;
/**
* Create a new response.
*
* #param string|null $message
* #return void
*/
public function __construct($message = null)
{
$this->message = $message;
}
/**
* Get the response message.
*
* #return string|null
*/
public function message()
{
return $this->message;
}
/**
* Get the string representation of the message.
*
* #return string
*/
public function __toString()
{
return (string) $this->message();
}
}
how this file miss the rest of methods the laravel docs talk about ??
tihs method requires laravel >= 6.x
this method not available in laravel 5
so make sure the laravel version in your project matches the laravel version that you browsing in laravel docs or in laravel API

"Class 'App\Models\Post' not found"

I'm trying to let my users made posts in my social media website. I already have a 'App\Models\Post'. How do I solve it??
Also the error appears when I try to submit the post, and the trouble is in the line that says: "$post = new Post();"
Ok, so here says that it looks like my post is mostly code, so I'll write no sense things so this pritty little thing go off. I'm not a native english speaker, so if you find a spelling or grammatical error please correct me :)
Here is the code of my Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post ;
use App\User;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('makePost');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function makePost(Request $request)
{
$this->validate($request, array(
'post' => 'required',
'title' => 'nullable|max:50',
'label' => 'nullable|max:25',
));
$post = new Post();
$post->post = $request->post;
$post->title = $request->title;
$post->label = $request->label;
$post->save();
return redirect()->route('index');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
And here is my Post Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function Users() {
return $this->belongsTo(User::class);
}
}
You defined the namespace in your Model wrong, if its in the Model directory change it to:
namespace App\Models;
If not you can always change your controller to:
use App\Post;
Try to use:
use App\Post;
Instead of use App\Models\Post ;

Laravel - empty result by trying to show a specific thread

My database is filled with data and showing all data works. But if I try to show a specific thread only, I get an empty result and I can't figure out why.
web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/threads', 'ThreadsController#index');
Route::get('/threads/{threads}', 'ThreadsController#show');
ThreadsController#index works, but ThreadsController#show doesn't work.
ThreadsController.php
<?php
namespace App\Http\Controllers;
use App\Thread;
use Illuminate\Http\Request;
class ThreadsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$threads = Thread::latest()->get();
return view('threads.index', compact('threads'));
//return $threads;
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function show(Thread $thread)
{
//return view('threads.show', compact('thread'));
return $thread;
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function edit(Thread $thread)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Thread $thread)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Thread $thread
* #return \Illuminate\Http\Response
*/
public function destroy(Thread $thread)
{
//
}
}
This is an example from laracast tutorial: link
Why this example doesn't work? I can't find any typo in my code.
My version:
php artisan --version
Laravel Framework 5.4.32
Rename your show route to the following:
Route::get('/threads/{thread}', 'ThreadsController#show');
You are loading a Thread object, and not a Threads object.

How can i add TrimString Middleware in laravel 5.3?

Just came to know that Laravel 5.4 has an awesome feature TrimString, which removes the white spaces from any input. I want this middleware in my 5.3 project, any idea how to do that?
I just copied the middleware from GitHub repo of Laravel but it is not working.
Thanks
If you want to use this feature in Laravel 5.3.
Add these two classes into your App\Http\Middleware
https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php
https://github.com/laravel/laravel/blob/master/app/Http/Middleware/TrimStrings.php
Updating it's namespace to App\Http\middleware.
Like:
TransformsRequest.php
namespace App\Http\Middleware;
use Closure;
use Symfony\Component\HttpFoundation\ParameterBag;
class TransformsRequest
{
/**
* The additional attributes passed to the middleware.
*
* #var array
*/
protected $attributes = [];
/**
* Handle an incoming request.
*
* #param \Illuminate\Http\Request $request
* #param \Closure $next
* #return mixed
*/
public function handle($request, Closure $next, ...$attributes)
{
$this->attributes = $attributes;
$this->clean($request);
return $next($request);
}
/**
* Clean the request's data.
*
* #param \Illuminate\Http\Request $request
* #return void
*/
protected function clean($request)
{
$this->cleanParameterBag($request->query);
$this->cleanParameterBag($request->request);
if ($request->isJson()) {
$this->cleanParameterBag($request->json());
}
}
/**
* Clean the data in the parameter bag.
*
* #param \Symfony\Component\HttpFoundation\ParameterBag $bag
* #return void
*/
protected function cleanParameterBag(ParameterBag $bag)
{
$bag->replace($this->cleanArray($bag->all()));
}
/**
* Clean the data in the given array.
*
* #param array $data
* #return array
*/
protected function cleanArray(array $data)
{
return collect($data)->map(function ($value, $key) {
return $this->cleanValue($key, $value);
})->all();
}
/**
* Clean the given value.
*
* #param string $key
* #param mixed $value
* #return mixed
*/
protected function cleanValue($key, $value)
{
if (is_array($value)) {
return $this->cleanArray($value);
}
return $this->transform($key, $value);
}
/**
* Transform the given value.
*
* #param string $key
* #param mixed $value
* #return mixed
*/
protected function transform($key, $value)
{
return $value;
}
}
TrimStrings.php
namespace App\Http\Middleware;
class TrimStrings extends TransformsRequest
{
/**
* The attributes that should not be trimmed.
*
* #var array
*/
protected $except = [
//
];
/**
* Transform the given value.
*
* #param string $key
* #param mixed $value
* #return mixed
*/
protected function transform($key, $value)
{
if (in_array($key, $this->except)) {
return $value;
}
return is_string($value) ? trim($value) : $value;
}
}
And add into your App\Http\Kernel.php
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\App\Http\Middleware\TransformsRequest::class,
\App\Http\Middleware\TrimStrings::class,
];
To use it just use:
dd(request('email'));
More on it:
https://laravel-news.com/laravel-5-4-middleware

Laravel 4.2 - ReflectionException (-1)

I am getting following error:
ReflectionException (-1)
Class PhotosController does not exist
This is my route:
Route::resource('photos', ' PhotosController');
When I change to Route::get('photos', 'PhotosController#index'); it is working fine, but using resource it is falling? What is going on?
PhotosController:
<?php
class PhotosController extends \BaseController {
/**
* Display a listing of the resource.
* GET /photos
*
* #return Response
*/
public function index()
{
return Photo::all();
}
/**
* Show the form for creating a new resource.
* GET /photos/create
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
* POST /photos
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
* GET /photos/{id}
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
* GET /photos/{id}/edit
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
* PUT /photos/{id}
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
* DELETE /photos/{id}
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
If composer dump-autoload doesn't fix it, then it is probably a typo in the class name or routes file, or incorrectly using subdirectories/namespaces on your controllers.

Resources