Using latestOfMany() in Laravel - laravel

If I have a model called Placement which can have many PlacementTqLimit and I want to get the latest one, I'm assuming I can use this.
public function latestLimit()
{
return $this->hasOne(PlacementTqLimit::class)->latestOfMany();
}
And then add a custom attribute to get the value
public function getLatestLimitValueAttribute()
{
if (empty($this->latestLimit())) {
return '100';
}
return $this->latestLimit()->value;
}
However, when I dump the $this->latestLimit() I'm getting a Illuminate\Database\Eloquent\Relations\HasOne... I'm sure I shouldn't have to call return $this->hasOne(PlacementTqLimit::class)->latestOfMany()->latest(). Am I doing something wrong here?

$this->latestLimit() will return hasOne that extend query builder.
If you want to get the latest of PlacementTqLimit model just use $this->latestLimit without () or you can also use $this->latestLimit()->first()
So, to get the latest value limit, example code is:
public function getLatestLimitValueAttribute()
{
if (!$this->latestLimit) {
return '100';
}
return $this->latestLimit->value;
}

Related

How to used Tucker-Eric/EloquentFilter Laravel

good day, I am using Tucker-Eric/EloquentFilter Laravel.
I want to filter it by relationship using Models
I want to automate it, instead of using the following:
public function users($users)
{
// dd($users);
return $this->r('users', $users);
}
public function user($user)
{
// dd($user);
return $this->r('user', $user);
}
public function owner($owner)
{
// dd($owner);
return $this->r('owner', $owner);
}
I want to make it one function that based on the relationship
so that I want to add another relationship on the model I don't need anymore to add another function.
Thanks!
We specifically stayed away from the type of implicit functionality you're looking for and opted for explicit filter methods to avoid security issues if/when new relations/properties were added to a model they wouldn't implicitly be available to filter against.
With that, what you're looking for isn't recommended because of the security concerns above but it can still exist if you implement it.
It sounds like the setup method would be the best place to implement it since it would be called first every time ->filter() is called.
public function setup()
{
foreach($this->input() as $key => $val) {
if($this->getModel()->$key() instanceof \Illuminate\Database\Eloquent\Relations\Relation) {
// Your logic here
}
}
}

Laravel pagination & get all

I have an API endpoint api/users that returns Users in paginated form:
public function index()
{
return User::latest()->paginate(10);
}
I also need to have access to ALL users as well, however the pagination makes this not possible.
For example I want to get all users as well using:
return User::latest()->get();
How can I use both without creating another endpoint? e.g. api/allusers
public function index($page)
{
if($page=='All')
{
return User::latest();
}
return User::latest()->paginate($page);
}

Returning other entry in the url includes the first entry as well

public function show(Criminal $criminal){
$profile = Criminal::with(['profile','crimes'])->findOrFail($criminal);
dd($profile);
}
I have this method and it should return like this when I type localhost:8000/criminal/1
But when I say like criminal/3
it also returns the criminal/1 json output like this :
the first entry looks like this :
try this
public function show(Criminal $criminal, $id){
$profile = Criminal::with(['profile','crimes'])->findOrFail($id);
dd($profile);
}
There is no need to query the database again, the model passed to your function is already an eloquent model.
public function show(Criminal $criminal) {
dd($criminal);
}
If you really want to lazy load the relations, this can be done as follows:
public function show(Criminal $criminal) {
$criminal->load('profile', 'crimes')
dd($criminal);
}
This should not be necessary however as Laravel loads relations when needed.

Laravel policies - passing the class as a variable to $user->can() method doesn't work

I have a route with dynamic model recognition. In other words, I take the desired model as an argument and use it in the controller. I have complex authorization in my app and I need to pass the model class name as a variable to the $user->can() method for using policies, but for some reason it doesn't work. Here's my code:
Policy:
public function view($user, Model $model) {
return $user->model_id == $model_id;
}
public function create($user) {
return $user->isAdmin();
}
Controller:
public function createModel($model) {
$model_class = $model . '::class';
if (Auth::user()->can('create', $model_class)) {
return $model_class::create();
}
return 'invalid_permissions';
}
If I hardcode the model class name it works. For example, if my model is 'Car' and in the controller I put:
if (Auth::user()->can('create', Car::class)) {
Anybody got any ideas why this is so and how to fix it? I hope that it's possible because I would have to change my whole concept if it isn't.
*Note: this is example code, not my actuall classes

Route to two methods of controller with one row in Laravel 4

It is simple to route this way:
Route::get('user/profile', 'PaymentsController#profile');
Route::get('user/delete', 'PaymentsController#delete');
I want to do this with one row:
Route::get('user/{subsection}', 'PaymentsController#'.$subsection);
but my syntax seems to be wrong. Is it possible to be done with one row? It would be nice if it is possible.
No, you can't do that, but you can make proxy method
Route::get('user/{subsection}', 'PaymentsController#profileDelete');
And method will looks like
public function profileDelete($subsection) {
return $this->$subsection();
}
public function profile(){}
public function delete(){}
Also, you can bind {subsection}
Route::bind('subsection', function ($subsection) {
if (!in_array($subsection, ['profile', 'delete'])) {
throw new Exception;
}
return $subsection;
});
Not exactly one row, but you can do this:
Route::get('user/{subsection}', function($subsection){
if(!method_exists('PaymentsController', $subsection)){
App::abort(404);
}
return App::make('PaymentsController')->callAction($subsection);
});
Alternatively to the method_exists you could also use a route condition to only allow a predefined set of subsections:
Route::get('user/{subsection}', function($subsection){
return App::make('PaymentsController')->callAction($subsection);
})->where('subsection', '(profile|delete)');

Resources