call the another function within a function - laravel

In laravel I want to call a function within function to make recursive.I caught the route error.how to call the function 'recursive in tableFetch'
class queryTest extends Controller
{
public function tableFetch() {
recursive();
}
function recursive(){
//condition
}
}
I want to do it for check the manager of the given person and then get the manager of the fetched value in query so need to do it recursive

A controller is not a good place for this. Instead, manage it in your Person Model(or whatever you have).
Everyone has a manager. So, your model has HasOne relation to itself.
Person Model:
public function manager()
{
return $this->hasOne(Person::class, 'manager_id');
}
Now if you need to check the manager of given person untill you meet a certain condition you can do it inside the model and get the result in the controller.
public function checkManager()
{
$manager = $this->manager
if (check manager)
return $manager;
//check for the last manager
return $this->manager ? $this->checkManager() : null;
}
Inside controller
function index()
{
$person = Person::find($id);
$manager = $person->checkManager();// this will do the recursive you need
}

Do something like this
class queryTest extends Controller
{
public function tableFetch() {
$this->recursive();
}
function recursive(){
//condition
}
}

you need to ask more precise details about your needs, because Laravel has some complications.
try doing this :
class queryTest extends Controller
{
public function tableFetch() {
$this->recursive();
}
public function recursive() {
//condition
}
}

Related

Get attributes from relationship parent model

I'm loading some data for the user like this.
Method 1
$user->load(['subscription.payementTypes']);
Method 2
$user->load(['charity.payementTypes']);
Is there anyway I can figure out in the PaymentType model that it is called via subscription or charity?
I want to call an Accessor in PaymentType based on Subscription or Charity
// For Subscriptions
public function getPaymentDescriptionAttribute()
{
// if Subscription
// if Charity
}
The relationships are as follows.
//Class User
public function subscription()
{
return $this->hasOne(Subscription::class);
}
public function charity()
{
return $this->hasOne(Charity::class);
}
//Class Subscription
public function paymentTypes()
{
return $this->belongsToMany(PaymentType::class);
}
I feel like in class Subscription, you should name it paymentTypes. I'd say it's a hasMany though?
public function paymentTypes()
{
return $this->hasMany(PaymentType::class);
}
and in the PaymentType you can do
public function subscription
{
return $this->belongsTo(Subscription::class);
}
if it is not working you can do somthing like this
in User.php
//Class User
public function subscriptionWithPaymentType()
{
return $this->hasOne(Subscription::class)->with('paymentTypes');
}
then you can do this
$user->load(['subscriptionWithPaymentType']);

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.

Laravel - Delete if no relationship exists

Below is the one of the model. I would like to delete a Telco entry only if no other model is referencing it? What is the best method?
namespace App;
use Illuminate\Database\Eloquent\Model;
class Telco extends Model
{
public function operators()
{
return $this->hasMany('App\Operator');
}
public function packages()
{
return $this->hasMany('App\Package');
}
public function topups()
{
return $this->hasMany('App\Topup');
}
public function users()
{
return $this->morphMany('App\User', 'owner');
}
public function subscribers()
{
return $this->hasManyThrough('App\Subscriber', 'App\Operator');
}
}
You can use deleting model event and check if there any related records before deletion and prevent deletion if any exists.
In your Telco model
protected static function boot()
{
parent::boot();
static::deleting(function($telco) {
$relationMethods = ['operators', 'packages', 'topups', 'users'];
foreach ($relationMethods as $relationMethod) {
if ($telco->$relationMethod()->count() > 0) {
return false;
}
}
});
}
$relationships = array('operators', 'packages', 'topups', 'users', 'subscribers');
$telco = Telco::find($id);
$should_delete = true;
foreach($relationships as $r) {
if ($telco->$r->isNotEmpty()) {
$should_delete = false;
break;
}
}
if ($should_delete == true) {
$telco->delete();
}
Well, I know this is ugly, but I think it should work. If you prefer to un-ugly this, just call every relationship attributes and check whether it returns an empty collection (meaning there is no relationship)
If all relationships are empty, then delete!
After seeing the answers here, I don't feel copy pasting the static function boot to every models that need it. So I make a trait called SecureDelete. I put #chanafdo's foreach, inside a public function in SecureDelete.
This way, I can reuse it to models that need it.
SecureDelete.php
trait SecureDelete
{
/**
* Delete only when there is no reference to other models.
*
* #param array $relations
* #return response
*/
public function secureDelete(String ...$relations)
{
$hasRelation = false;
foreach ($relations as $relation) {
if ($this->$relation()->withTrashed()->count()) {
$hasRelation = true;
break;
}
}
if ($hasRelation) {
$this->delete();
} else {
$this->forceDelete();
}
}
}
Add use SecureDelete to the model that needs it.
use Illuminate\Database\Eloquent\Model;
use App\Traits\SecureDelete;
class Telco extends Model
{
use SecureDelete;
public function operators()
{
return $this->hasMany('App\Operator');
}
// other eloquent relationships (packages, topups, etc)
}
TelcoController.php
public function destroy(Telco $telco)
{
return $telco->secureDelete('operators', 'packages', 'topups');
}
In addition, instead of Trait, you can also make a custom model e.g BaseModel.php that extends Illuminate\Database\Eloquent\Model, put the function secureDelete there, and change your models to extends BaseModel.

Laravel hasMany with multiple constraints

I have query that I run on my ServiceController
return Service::with('contacts', 'locations.datetimes')->find($service->id);
This works great perfectly, but I need to change it. The relationships are defined as follows:
class Service extends Model
{
public function locations()
{
return $this->belongsToMany('App\Models\Service_location', 'service_location_service');
}
}
class Service_location extends Model
{
public function datetimes()
{
return $this->hasMany('App\Models\Service_detail');
}
}
I need a second constraint on the datetimes where I need the relationship to be along the lines of
return $this->hasMany('App\Models\Service_detail')->where('service_id', $service->id);
The problem is I can't find a way to pass through the $service_id. How do you handle two constraints on a hasMany relationship?
Let try this. Change Service_location to this
class Service_location extends Model
{
public function datetimes()
{
return $this->hasMany('App\Models\Service_detail');
}
public function scopeServiceId($query)
{
return $query->where('service_id', $service->id);
}
}
Now your query will be
Service::with(['contacts', 'locations.datetimes' =>function($q) use($serviceId){
$q->serviceId($serviceId);
}])->find($service->id);
There is no way to pass argument to a relationship function. If you do its highly likely that you will run into the N+1 query problem.

if beLongsTo is empty for particular item return another belongsTo relationship

I want to return the customer adress if there is no deliveretadress on the order.
This what I have been trying:
public function deliveryAddress()
{
if ( $this->delivery_address )
{
return $this->belongsTo("DeliveryAddress",'delivery_address');
}
return $this->belongsTo("Customer", 'customer_id');
}
This does not work if I want to use Order::with('deliveryAddress')->get(), only If I have a model and not a collection.
Is this possible?
When you use a relation you still don't have a row available, relations are queries in the process of built, this way, you still dont havedelivery_address` available here:
if ( $this->delivery_address )
Because at this point your model is still not up. It (or they) will be up after a get() or a first() and then you'll have access to them. So you can
public function deliveryAddress()
{
return $this->belongsTo("DeliveryAddress",'delivery_address');
}
public function customerAddress()
{
return $this->belongsTo("Customer", 'customer_id');
}
class Order extends Eloquent {
public function getAddress()
{
if ($this->delivery_address)
{
return $this->deliveryAddress;
}
else
{
return $this->customerAddress;
}
}
}
$order = Order::with('deliveryAddress', 'customerAddress')->first();
echo $order->getAddress()->street_name;
This is untested code, so it might not work at first, but this is the idea of what you can do.

Resources