Could you give some example of usage of Laravel binding call() method? Below you have Laravel Container Interface call() method.
/**
* Call the given Closure / class#method and inject its dependencies.
*
* #param callable|string $callback
* #param array $parameters
* #param string|null $defaultMethod
* #return mixed
*/
public function call($callback, array $parameters = [], $defaultMethod = null);
I guess to work with that this way:
$this->app->call('how_to_use_this_params')
Thanks in advance.
Related
I am new to Laravel. I am currently building a resource. I was just wondering what is the purpose of passing in the $request object if we use the $this variable when using resources. Below is an example of my code.
class PetitionResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return ["id"=> $this->id,"title"=>$this->title,'author'];
}
}
hi i want create my own authorization to study the new veri=sion of framework ...
this is my route :
$routes->add('/user/login', 'User::login',['filter'=>'usersFiltersNoAuth']);
$routes->add('/login', 'User::login',['filter'=>'usersFiltersNoAuth']);
$routes->add('/user/registration', 'User::registration',['filter'=>'usersFiltersNoAuth']);
$routes->add('/logout', 'User::logout');
$routes->add('/user/changeEmail', 'User::changeEmail',['filter'=>'usersFiltersAuth']);
$routes->add('/user/changePassword', 'User::changePassword',['filter'=>'usersFiltersAuth']);
And this is my 2 filter class:
class UsersFiltersNoAuth implements FilterInterface
{
/**
* Do whatever processing this filter needs to do.
* By default it should not return anything during
* normal execution. However, when an abnormal state
* is found, it should return an instance of
* CodeIgniter\HTTP\Response. If it does, script
* execution will end and that Response will be
* sent back to the client, allowing for error pages,
* redirects, etc.
*
* #param \CodeIgniter\HTTP\RequestInterface $request
* #param array|null $params
*
* #return mixed
*/
public function before(RequestInterface $request, $params = null)
{
// if no user is logged in then send them to the login form
if (isset($_SESSION['user_id']))
{
return redirect()->to('/user/index');
}
}
//--------------------------------------------------------------------
/**
* Allows After filters to inspect and modify the response
* object as needed. This method does not allow any way
* to stop execution of other after filters, short of
* throwing an Exception or Error.
*
* #param \CodeIgniter\HTTP\RequestInterface $request
* #param \CodeIgniter\HTTP\ResponseInterface $response
* #param array|null $arguments
*
* #return void
*/
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
}
//--------------------------------------------------------------------
} // End of UsersFiltersNoAuth Class.
class UsersFiltersAuth implements FilterInterface
{
/**
* Do whatever processing this filter needs to do.
* By default it should not return anything during
* normal execution. However, when an abnormal state
* is found, it should return an instance of
* CodeIgniter\HTTP\Response. If it does, script
* execution will end and that Response will be
* sent back to the client, allowing for error pages,
* redirects, etc.
*
* #param \CodeIgniter\HTTP\RequestInterface $request
* #param array|null $params
*
* #return mixed
*/
public function before(RequestInterface $request, $params = null)
{
// if no user is logged in then send them to the login form
if (!isset($_SESSION['user_id']))
{
session()->set('redirect_url', current_url());
return redirect()->to('/login');
}
}
//--------------------------------------------------------------------
/**
* Allows After filters to inspect and modify the response
* object as needed. This method does not allow any way
* to stop execution of other after filters, short of
* throwing an Exception or Error.
*
* #param \CodeIgniter\HTTP\RequestInterface $request
* #param \CodeIgniter\HTTP\ResponseInterface $response
* #param array|null $arguments
*
* #return void
*/
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
}
//--------------------------------------------------------------------
} // End of UsersFiltersAuth Class.
if i try to go to /user/chengeEmail or /user/changePassword when ($_SESSION['user_id] is set) i am redirect to /user/index why ?
Moreover there is a way to apply a filter to an entire controller ? except some method ?
Assume we have a model name "Article" in Laravel and want to make query for retrieving latest articles, so one way is to define a method in the "Article" model like this:
public function newArticle()
{
return static::where('created_at', '>', Carbon::subMonths(1));
}
The question is, why we should use
static::
in the above code?
Is it possible to use
$this or self::
instead of
"static::" ?
Thanks in advance,
You could but there is not interest because:
where method does not exist statically on Illuminate\Database\Eloquent\Model class, so it calls __callStatic magic method which delegates the call to an instance
/**
* Handle dynamic static method calls into the method.
*
* #param string $method
* #param array $parameters
* #return mixed
*/
public static function __callStatic($method, $parameters)
{
return (new static)->$method(...$parameters);
}
It calls where method on instance but it does not exist either, so it calls __call magic method which delegates it an Illuminate\Database\Eloquent\Builder instance.
/**
* Handle dynamic method calls into the model.
*
* #param string $method
* #param array $parameters
* #return mixed
*/
public function __call($method, $parameters)
{
if (in_array($method, ['increment', 'decrement'])) {
return $this->$method(...$parameters);
}
return $this->forwardCallTo($this->newQuery(), $method, $parameters);
}
/**
* Get a new query builder for the model's table.
*
* #return \Illuminate\Database\Eloquent\Builder
*/
public function newQuery()
{
return $this->registerGlobalScopes($this->newQueryWithoutScopes());
}
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
So I have a trait like as below:
trait RepositoryTrait
{
/**
* Find a model by its primary key.
*
* #param int $id
* #param array $columns
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function find($id, array $columns = ['*'])
{
return $this->query()->find($id, $columns);
}
}
and the interface:
interface Repository
{
/**
* Find a model by its primary key.
*
* #param int $id
* #param array $columns
* #return \Illuminate\Database\Eloquent\Model|null
*/
public function find($id, array $columns = ['*']);
}
and I have repository class like:
class FixedAssetRepository implements Repository
{
use RepositoryTrait;
/**
* FixedAsset model.
*
* #var FixedAsset
*/
protected $model;
/**
* Repository constructor.
*
* #param FixedAsset $fixedAsset
*/
public function __construct(FixedAsset $fixedAsset)
{
$this->model = $fixedAsset;
}
}
and what I'm looking for is to define somehow that method find (in trait or interface) is type of FixedAsset - but this should work always for each new Repository class that I'll create (that implements Repository interface).
I need it for type hinting in PhpStorm 10.0.4
Is it possible somehow?
So the result should be that when I call somewhere I.e.:
$fixedAsset = $this->fixedAssetRepositry->find($id);
PhpStorm will know that $fixedAsset is a object of a class FixedAsset not just \Illuminate\Database\Eloquent\Model (now is working like that).
So I need something that like in interface (or trait?):
/**
* Find a model by its primary key.
*
* #param int $id
* #param array $columns
* #return $this->model
*/
public function find($id, array $columns = ['*']);
but of course #return $this->model doesn't work.
I'll be appreciated for any suggestions about that.
The only solution that I can think of right now would be using #method in PHPDoc comment for that FixedAssetRepository class and "redeclare" that find() method with correct signature (return type). Since it's a PHPDoc solution it will not have any effect on PHP code during runtime.
Sample code:
/**
* #method FixedAsset find(int $id, array $columns) Find a model by its primary key
*/
class FixedAssetRepository implements Repository
{
...
More on the #method tag -- https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc.md#711-method