Laravel Nova metrics filtering - laravel

I have a model called Property which has an 'active' flag. I want a metric at the top of my resource which shows a count of active Properties.
My calculate method is exactly as in the doc but this shows all Properties rather than active ones:
public function calculate(Request $request)
{
return $this->count($request, Property::class);
}
How can I add a filter?
I've tried a where clause:
public function calculate(Request $request)
{
return $this->count($request, Property::class)->where('active','=',1);
}
And a query scope:
public function calculate(Request $request)
{
return $this->count($request, Property::class)->active();
}
I thought I might be able to use the Nova filter I set up on the resource list page but that didn't seem to work either. I'm sure it's really easy but I haven't worked it out. Thanks for your help!

Your can use every type of Eloquent\Builder instance in the $model param.
Instead of:
public function calculate(Request $request)
{
return $this->count($request, Property::class);
}
Set a Scope on your Model
App\Property.php
...
public function scopeActive($query)
{
return $query->where('active', 1);
}
public function scopeInactive($query)
{
return $query->where('active', 0);
}
And use this scope as the $model param in your calculate method, because the call of the scope returns a Eloquent\Builder Instance
public function calculate(Request $request)
{
return $this->count($request, Property::active());
// return $this->count($request, Property::inactive());
}
Edit
Of course you can make the Eloquent Builder call inline:
public function calculate(Request $request)
{
return $this->count($request, Property::where('active', 1));
}

Related

Can I use return to a public function in controller in Laravel 8

I got an error which is undefined function.
I tried to use a public function in return:
public function CreateForm() //This the function that I want to use back
{
$names2 = DB::table('pendaftaran')
->where('isActive',0)
->orderBy('id','desc')
->get();
return view('contact')->with($variables);
}
So this a function I want to return to a function
public function AddUserSubmit(Request $request)
{
$this->validate($request,
[
'Nama'=>'required',
'NoKP'=>'required',
]);
Pendaftaran::create($request->all());
return CreateForm(); //Can I return to a public function ?
}
Yes this should work but you need to add context and use the $this keyword otherwise return CreateForm(); will be interpreted as trying to invoke a global function.
Try replacing:
return CreateForm();
With
return $this->CreateForm();
See this other question for more information: https://stackoverflow.com/a/17861505/4517964

Laravel trait crud controller with request validation and resources

I'm trying to refactor my code to be more reusable.
I created a trait CrudControllerTrait to implement the index,show,store,update,destroy methods.
But I found 2 problems:
BrandController.php
public function store(BrandNewRequest $request)
{
$requestData = $request->validated();
return new BrandResource($this->brands->store($requestData));
}
ProductController.php
public function store(ProductNewRequest $request)
{
$requestData = $request->validated();
return new ProductResource($this->products->store($requestData));
}
The trait method would be:
public function store(xxxxx $request)
{
$requestData = $request->validated();
return new xxxxxResource($this->repository()->store($requestData));
}
Problem1: The hint type. How can I abstract them? If I remove it shows that errror:
"message": "Too few arguments to function App\\Http\\Controllers\\BrandController::store(), 0 passed and exactly 1 expected"
Problem2: Return the resource. How can create the new resource? On the collection I can solve it doing this:
public function index()
{
$models = $this->repository()->index();
return $this->resource()::collection($models);
}
The resource is on the controller who uses the trait:
public function resource()
{
return BrandResource::class;
}
But with single resource didn't know how to do it...
The idea is, that I have so much controllers using the same pattern: BrandController, ProductController, etc. I'd love to reuse these 5 crud methods on the same trait...
The only way I found is creating an abstract method.
trait CrudRepositoryTrait
{
abstract function model();
public function index()
{
return $this->model()::with($this->with())->get();
}
public function find($id)
{
return $this->model()::findOrFail($id);
}
public function store($data)
{
$request = $this->dtoRequest($data);
return $this->model()::create($request);
}
(...)
}
And then, an example how to use this treat:
class ProductRepository implements ProductRepositoryContract
{
use CrudRepositoryTrait;
function model()
{
return Product::class;
}
(...)
}
By this way I could reuse a lot of code.

How to get model relations in Laravel?

I would like to get the model relations, in array;
My model look like:
class User extends Model
{
public function profile() {
return $this->haOne(Profile::class);
}
public function settings() {
return $this->morphOne(Settings::class, 'settingsable');
}
public function addresses() {
return $this->hasMany(Addresses::class);
}
}
And my code:
$user = User::with(['profile', 'settings', 'addresses'])->find(1);
$user->getRelations(); // return ['profile', 'settings', 'addresses'];
If I have more then 10 relation, I don't want to list all.
I would like to get like this:
$relations = ['profile', 'settings', 'addresses'];
Is this posible?
You could try adding a scope to the model, and so, you have to only write them once.
class User extends Model
{
public function profile() {
return $this->haOne(Profile::class);
}
public function settings() {
return $this->morphOne(Settings::class, 'settingsable');
}
public function addresses() {
return $this->hasMany(Addresses::class);
}
public function scopeWithRelations(Builder $query){
return $query->with([...]);
}
}
$users = User::withRelations()->get();
This way you only have to write them once there, and everywhere in the code you'll use the scope.
Not exactly 100% what you're asking, but this could be a solution.

Call to a member function diffForHumans() on null

i am getting outputs of my posts in the system but i am getting this errror Call to a member function diffForHumans() on null... Can u please help me? Thanks...
Article model is that:
protected $fillable=[
'user_id','content','live','post_on'
];
public function setLiveAttribute($value)
{
$this->attributes['live']=(boolean)($value);
}
And the articlescontroller is that
public function index()
{
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function create()
{
return view('articles.create');
}
public function store(Request $request)
{
Article::create($request->all());
}
public function show()
{
}
public function update()
{
}
public function destroy()
{
}
The problem is for sure not here. You probably try to run diffForHumans() in your view for example like this:
$user->post_on->diffForHumans()
but post_on is set to null instead of date.
So you should verify where exactly you run this method, set this column as date and in your view add something like this:
Posted: {{ $date ? $date->diffForHumans: '-' }}

How to redirect store to update method?

How to redirect store to update method? I tryed the following code:
public function store(ProductRequest $request)
{
return $this->update(new Product, $request);
}
public function update(Product $product, ProductRequest $request)
{
// code
}
However, the first parameter of update need an already in database user and the above code does not work as expected. (it update the entire users in db!)
What is the correct way to achieve that?
public function store(UserRequest $request)
{
return $this->maintain(new User, $request);
}
public function update(User $user, UserRequest $request)
{
return $this->maintain($user, $request);
}
private function maintain($user, $request)
{
//code;
}
The model for the update method could be the problem, your code is okay for this part:
public function store(Request $request)
{
return $this->update(new Product, $request);
}
public function update(Product $product, Request $request)
{
$product->fill($request->all())->save();
// code
}
For example, with route model binding:
Route::resource('products', 'ProductController');
Route::model('products', App\Product::class);
Or with a custom binding:
Route::resource('products', 'ProductController');
Route::bind('products', function($param) {
return Product::where('slug', $param)->first();
});
Make sure you are not using get() in custom binding, it will pass back a collection.

Resources