Laravel Nova BelongsTo Field Appending Namespace To Current Namespace - laravel

I am currently using laravel 5.8 with laravel nova.
When i use a model class inside my nova action file, like this:
Namespace App\Nova\Actions;
use App\Nova\User;
use Orlyapps\NovaBelongsToDepend\NovaBelongsToDepend;
class PlayerDD extends Action
{
public $name = 'Spelers toekennen';
/**
* Perform the action on the given models.
*
* #param \Laravel\Nova\Fields\ActionFields $fields
* #param \Illuminate\Support\Collection $models
* #return mixed
*/
public function handle()
{
}
/**
* Get the fields available on the action.
*
* #return array
*/
public function fields()
{
return [
BelongsTo::make(User::class)
];
}
}
Laravel returns the error: Class 'App\Nova\Actions\App\Nova\User' not found.
While i am using the root namespace for the User model, the namespace gets appended to the current namespace.
Is there a fix for this, and where should i look for conflicts?
Thanks in advance!
BTW, i tried this \App\Nova\User

Related

Is possible in Laravel Nova 4 use a field of nested relation in search fields?

I have the following db:
Showcases (n to 1) Workers (1 to 1) Users
I need in the showcase resource section find showcase by user's name. In the Nova's documentation they explains that is possible search by related field like this:
public static $search = [
'id', 'author.name'
];
If I try 'worker.user.name' it doesn't works. Any idea?
You'll have to define it on your Laravel Model, otherwise it wont work.
use Laravel\Nova\Query\Search\SearchableRelation;
/**
* Get the searchable columns for the resource.
*
* #return array
*/
public static function searchableColumns()
{
return ['id', new SearchableRelation('author', 'name')];
}
You can use this package titasgailius/search-relations.
<?php
namespace App\Nova\Resources\OrderManagement;
use App\Nova\Resources\Resource;
use Titasgailius\SearchRelations\SearchesRelations;
class Showcase extends Resource
{
use SearchesRelations;
/**
* The relationship columns that should be searched.
*
* #var array
*/
public static $searchRelations = [
'worker.user' => ['name'],
];
/**
* Get the fields displayed by the resource.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function fields(Request $request)
{
return [];
}
}
*Assuming you have set the belongsTo relationships properly in your models.

Laravel router namespace method

In Laravel documentation routing there is a namespace method.
Route::namespace
I tried to explore what does it really do but couldn't find it's definition in Laravel source codes. Where is it?
It's not related to code, just to group the routes. Like this:
The source is here: https://github.com/laravel/framework/blob/b73691ac7b309cd2c4fb29b32d3eed76fecca58b/src/Illuminate/Routing/RouteGroup.php#L40, it just adds the namespace at end of the current namespace.
You have a controller group like 'Products' for example,
App/
Http/
Controllers/
Products/
Stocks.php
Prices.php
Sizes.php
And you need to modify their namespaces like this to meet the PSR-4 requirements to enable autoloading of controllers:
namespace App\Http\Controllers\Products;
class Stocks {
function index(){
}
}
Then if you want to access the methods of these controllers, you might want to group them with Route::namespace():
Route::namespace("Products")->group(function(){
Route::get("stocks", "Stocks#index");
});
This will search for the Stocks class in the App\Http\Controllers\Products namespace instead of App\Http\Controllers namespace. and call the index method.
Note that you might run composer dumpautoload to let the framework rebuild the autoload.php with the PSR-4 namespaces to make these things effective.
Later Edit:
framework/src/Illuminate/Routing/Router.php defines the Route class, which redirects the Route::namespace method to RouterRegistrar class at this line:
/**
* Dynamically handle calls into the router instance.
*
* #param string $method
* #param array $parameters
* #return mixed
*/
public function __call($method, $parameters)
{
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}
if ($method == 'middleware') {
return (new RouteRegistrar($this))->attribute($method, is_array($parameters[0]) ? $parameters[0] : $parameters);
}
return (new RouteRegistrar($this))->attribute($method, $parameters[0]);
}
in the last line. And in that method,
/**
* The attributes that can be set through this class.
*
* #var array
*/
protected $allowedAttributes = [
'as', 'domain', 'middleware', 'name', 'namespace', 'prefix',
];
/**
* Set the value for a given attribute.
*
* #param string $key
* #param mixed $value
* #return $this
*
* #throws \InvalidArgumentException
*/
public function attribute($key, $value)
{
if (! in_array($key, $this->allowedAttributes)) {
throw new InvalidArgumentException("Attribute [{$key}] does not exist.");
}
$this->attributes[Arr::get($this->aliases, $key, $key)] = $value;
return $this;
}
namespace attribute is being set, to use in ->group() method.

Composer update breaks the website Laravel 5.2

I accidentally run composer update which breaks my website. I am using Laravel 5.2. Now, I am getting this error
ErrorException in EventServiceProvider.php line 8:
Declaration of
App\Providers\
EventServiceProvider::boot(Illuminate\Contracts\Events\ Dispatcher $events) should be compatible with
Illuminate\Foundation\Support\Providers\EventServiceProvider::boot()
I tries to remove arguments from EventServiceProvider like this
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot()
{
parent::boot();
//
}
EventServiceProvider before changes:
<?php
namespace App\Providers;
use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as
ServiceProvider;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* #var array
*/
protected $listen = [
'App\Events\SomeEvent' => [
'App\Listeners\EventListener',
],
];
/**
* Register any other events for your application.
*
* #param \Illuminate\Contracts\Events\Dispatcher $events
* #return void
*/
public function boot(DispatcherContract $events)
{
parent::boot($events);
//
}
and from RouteServiceProvider.php
/**
* Define your route model bindings, pattern filters, etc.
*
* #param \Illuminate\Routing\Router $router
* #return void
*/
public function boot()
{
//
parent::boot();
}
RouteServiceProvider before changes:
<?php
namespace App\Providers;
use Illuminate\Routing\Router;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as
ServiceProvider;
class RouteServiceProvider extends ServiceProvider
{
/**
* This namespace is applied to your controller routes.
*
* In addition, it is set as the URL generator's root namespace.
*
* #var string
*/
protected $namespace = 'App\Http\Controllers';
/**
* Define your route model bindings, pattern filters, etc.
*
* #param \Illuminate\Routing\Router $router
* #return void
*/
public function boot(Router $router)
{
//
parent::boot($router);
}
Now, I am getting this error:
BadMethodCallException in Macroable.php line 74:
Method controllers does not exist.
Please help me. Thank you.
Based on the comments and discussion, you've somehow ended up updating your Laravel framework to 5.3.31 which has breaking changes with 5.2. The solution would to be to either downgrade to the latest version under 5.2 or upgrade the complete application to 5.3 following the upgrade guide.
To fix with the downgrade replace the current framework package in composer.json with "laravel/framework": "5.2.*", and run composer update

Controller RESTful methods in laravel 5

when i executing php artisan make:controller Test command in laravel 5
i am getting below blueprint
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class Test extends Controller
{
//
}
But Documentation said we should get skeleton with RESTful methods
class Test extends BaseController {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
So what is wrong in my case . how to get what exactly RESTful Resource Controllers should be ?
I think you have to add --resource option at the end of this command to generate a RESTful controller. Following the official laravel docs, your command should be like this:
php artisan make:controller Test --resource

Call to a member function on a non-object eloquent attach

I am having an issue with laravel not seeing my tags() method for attaching new tags on a new entry. I keep getting Call to a member function on a non-object when I try to run the method and attach tags to my Tile model. All methods are returning their relations. I followed the same order as the documentation says eloquent.
Controller
$tile = \Tiles\Tile::find($tile_id);
$tile->tags()->attach($tag_array);
Model
<?php namespace Tiles;
use Illuminate\Database\Eloquent\Model;
class Tile extends Model {
/**
* The Tile table
* #var string
*/
protected $table = 'tiles';
/**
* Pivot table for tags
* #var string
*/
protected $pivot = 'tag_tile';
/**
* Get the tags associated with the given tile
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags() {
return $this->belongsToMany('Tiles\Tag', $this->pivot, 'tile_id', 'tag_id')->withTimestamps();
}
}
Try it
Model
public function tags() {
return $this->belongsToMany('Tiles\Tag', $this->pivot, 'tag_id', 'tile_id')->withTimestamps();
}
Thanks for all your help. I figured out the solution. I created a method in my model and pushed each to an array and fed it to the attach method. It works now.

Resources