laravel api handler how to order by a child column - laravel-4

I'm using laravel API handler
and I was wondering on how to send a request to order by a column from 'with' table
Here is my backend code
$query = Product::query();
$query->company();
$query->with('category'); $result=ApiHandler::parseMultiple($query);
return $result->getResponse();
and the request-url
http://localhost/public/api/v1/product/view/0?&_offset=0&_limit=10&_config=meta-total-count,meta-filter-count&_sort=-category.category_name

$query = Product::getModel()
->newQuery()
->select('products.*','categories.*')
->join('categories','product.category_id,'=','categories.id')
->orderBy('category.sort_order');
Something like that should do.

Related

Separate a relationship from the collection on Laravel 8 - Eloquent

I am generating a query that collects an invoice, and the related invoice customer.
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
return view('invoiceView', ['invoice'=>$invoice]);
It works correctly, through $invoice->clients, I can access the client.
But I would like to be able to separate the client from the collection invoice. To send it through another variable. So that it looks something like this:
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
//Some magic here
return view('invoiceView', ['invoice'=>$invoice,'client'=>$client]);
It is to take advantage of an invoice creation view (to be able to edit), which is expected by the $clients collection.
I have searched, to do this in the view, but it is not possible or it is not recommended.
I guess I could do something like thisbefore sending it to view:
$client = $invoice->customer
But then I would be sending it twice on view.
To unset the relationship from a model you can use unsetRelation($relation) method
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
$client = $invoice->fullClient;
$invoice->unsetRelation('fullClient');
return view('invoiceView', ['invoice'=>$invoice,'client'=>$client]);
you could use pluck()
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
//The magic
$client = $invoice->pluck('fullClient');
return view('invoiceView', ['invoice'=>$invoice,'client'=>$client]);
docs

Laravel6 WhereHas Error 500 when using AJAX

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 );});
}

API request with multiple body inputs

I am new to laravel this is how I make an API with one single body item
$shops_categories=ShopsCategories::all()->where('title',$request->title);
how to request more than an item in the body for example title , email , password ?
Eloquent allows you to chain as many where as required. So you are able to do
$shops_categories = ShopsCategories::where('title',$request->title)->where('email',$request->email)->where('password',$request->email)->get();
You can do it like so:
$cats = ShopsCategories::query()->where(['title' => $title, 'email'=>$email, 'name'=>$name]);
i guess you are performing query operation in controller itself which is not recommended,please create an instance for the model and make a function call to model and in the model you can return like
return $this::where('id','=',$id)->get()
and also please try to avoid using text/string in where condition instead use id's or unique values
You can also convert request to array using all()
//$request->all() is same as ['title'=> 'my title', ...]
$shops_categories=ShopsCategories::where($request->all())->get();

Where Clause in eloquent is responding with `No Properties`

I am new to laravel. I am trying to keep a where clause for the get method like this.
$employees = newdb::where('status', 'Active');
$response = $employees;
return response()->json($response,200);
But i am getting No Properties as output
In PHP I used this
"SELECT * FROM newdb WHERE status = 'Draft'";
What am i doing wrong? I tried different suggestions But none worked correctly. How do i do this? What is wrong with my code?
where method returns Builder object. So, you have to call get method on it to fetch data.
Try this
$employees = newdb::where('status', 'Active')->get();
return response()->json($employees, 200);

Laravel Lumen - Eloquent Query Log

I'm using Laravel Lumen to build an API.
I've come to a point where I need to find out what SQL query is being generated by Eloquent. I know how to do this in Laravel 4 and Laravel 5 but i've tried the same code in Lumen and the query is blank?
$queries = DB::getQueryLog();
$last_query = end($queries);
echo 'Query<pre>';
print_r($last_query);
exit;
The above code, when run in Laravel works fine - in Lumen the query is blank?
To get the query log in Laravel Lumen working you need to enable it:
DB::connection()->enableQueryLog();
You can add that code in to your controller, middleware, etc then use:
$queries = DB::getQueryLog();
$lastQuery = end($queries);
dd($lastQuery)
To print your query.
You can also use the following with eloquent:
$myModel = Users::where('active', true);
dd($myModel->getSql(), $myModel->getBindings());
You must run the getSql() and getBindings() before you call ->first() or ->get(), etc
Just call this after the query to keep it quick and simple:
echo $query->toSql();

Resources