I have to following models.
Manager(id,name,...)
Account(id,manager_id,...)
Subscription(id,account_id,quantity,...)
I want to get all the subscriptions of all the accounts associate with the online manager.
I tried something like this.
$managerAccounts = Account::where('manager_id', auth()->id())->get('id');
return $this->count($request, Subscription::where('account_id', $managerAccounts), 'quantity');
But this is not working properly, any suggestions?
All I needed to do is to change it to whereIn.
$managerAccounts = Account::where('manager_id', auth()->id())->get('id');
return $this->count($request, Subscription::whereIn('account_id', $managerAccounts), 'quantity');
Related
What patterns can I use for 'automatic' resource CRUD operations for given Models in Laravel?
Say I have two models SomeModel and SomeRelatedModel where some_related_model.some_model_id is an FK to SomeModel.
The standard method on the SomeModelController for handling the create POST /api/someModel might look like this:
public function store(Request $request)
{
$user = Auth::guard('api')->user();
$data = $request->get('data');
$data['user_id'] = $user->id;
$someModel = SomeModel::create($data);
// has this request been made with the data for the
// related model? If so create this too.
if($data['relatedModel']){
SomeRelatedModel::create(array_merge(
['some_model_id' => $someModel->id]
$data['relatedModel']
));
}
// has this request been made expecting to get related
// models back in the response? If so load these
if($request->has('with')){
$someModel->load($request->get('with'));
}
return (new PostResource($post))
->toResponse($request)
->setStatusCode(201);
}
This works but is very verbose and for models with a sub-sub relation would need changing further. Similar work will need to be done for the other endpoints for all resources.
Is there a more versatile (or tidy) pattern using out-of-the box classes to get a similar effect?
Have a look at Laravel Orion. Fits your use case.
im new to Laravel and facing an interesting Issue right now in my App.
I have 3 tables.
Producers
id
producer_name
Types
id
type_name
Models
id
model_name
device_type_id
device_producer_id
Within my Producers Model I have defined the follwing Filter method:
public function scopeFilterByType($query, $type_id)
{
$query->whereHas('models', function($q) use $type_id { $q->where('device_type_id', $type_id );});
}
Using Tinker I can do the following:
App\DeviceProducer::filterByType(3)->get()
And get full response with my Producers associated to my given type.
I created an Function so when a user select a device type Ajax will load all Producers from this type.
public function reqProducer(Request $request)
{
$producers = DeviceProducer::filterByType($request->type_id)->get();
return response()->json( $producers );
}
But when AJAX is calling my endpoint it gets HTTP500 error.
I figured out when using a request without WhereHas for example:
$producers = DeviceProducer::where('id', $request->producer_id)->get();
It just works fine and I get my results. So it seems have to do something with "WhereHas". I know I could Solve this by first asking Models Table and the creating an Foreach loop. But I this solution would be less readable then my first attempt.
Does anyone has an suggestion what im doing wrong or is it just like there is noch AJAX support for WhereHas querys?
Kind regards
Mike
I think this is your issue use $type_id
Please fix as
public function scopeFilterByType($query, $type_id)
{
$query->whereHas('models', function($q) use ($type_id) { $q->where('device_type_id', $type_id );});
}
I'm trying to add index to Algolia using Laravel Scout based on a condition. For example I have a Article model and I only want to add this article to Algolia if the article is active. My first approach was this:
public function toSearchableArray()
{
if($this->active) return $record;
return [];
}
this only adds the active records but still attempts to add empty arrays which is considered as Operation in algolia ( I will be charged for it). The second approach was to use shouldBesearchable() function from scout:
public function shouldBeSearchable()
{
if($this->active) return true;
return false;
}
This doesn't work with php artisan scout:import "App\Article". Has anyone faced a similar problem?
It was a bug in Laravel Scout, shouldBeSearchable is not release yet (on master branch) so you may experience some issue like this one.
Although, good news: it was just fixed by this PR.
https://github.com/laravel/scout/pull/250
This is the default laravel notifications data field
{
"type":"Update Appointment",
"appointment_id":"379",
"date":null,
"updated_by":"Mahir",
"status":"2"
}
In controller i want to get all notifications with status = 2 and mark as read
Laravel 5.3 doc shows
$user = App\User::find(1);
foreach ($user->unreadNotifications as $notification) {
$notification->markAsRead();
}
How do i modify this to get all notifications with status = 2
Update : looking for something like this
$noti = $user->unreadNotifications->where('data->status',"2");
Note : my database doesn't support json data type.
According to laravel 5.6: JSON Where Clauses
and work with MySQL 5.7
I hope this answer help you
I think, you can create your own channel, (see also Illuminate\Notifications\Channels\DatabaseChannel), where you can override send() method, for saving your notifications. Before that, you can write your migration to update "notifications" table to add your custom filtering field.
See: https://laravel.com/docs/5.5/notifications#custom-channels
Sorry for my English. Hope, you'll understand.
$user = $user->unreadNotifications->where('data.complaint_id',1);
Its work fine for me, I hope it's helpful for you.
I'm using Laravel Spark and I'm reading the docs, but I can't find any method to get a list of my Spark developers. It looks like the only usage I can find that references the protected $developers variable is the middleware which compares an email address with Spark::developer().
Is there anything like Spark::getDevelopers() that would either return the protected array, or a collection of the actual users with matching emails?
I could do this but it seems needlessly expensive:
$users = User::get();
$developers = $users->filter(function ($user) {
return Spark::developer($user->email);
});
This does the trick without having to modify anything:
$developers = User::whereIn('email', Spark::$developers)->get();
Add this to ManagesAppDetails.php
public static function getDevelopers(){
return self::$developers;
}
then you can do:
spark::getDevelopers()
hope this helps!