laravel Eloquent ORM - How to get compiled query? - laravel

In Laravel 4.2 I want to get Compiled Query.
This is what i have:
$product = Product::where('id', '=', '100')->get();
I want compiled query like:
select * from products where id = 100
Purpose of the question is: i want to use it as sub query in another query.
I have searched and found Class Grammer and Class MySQL But i did not found solution for that.
Is there any solution?
Your help would be appreciated.

The easiest way is probably mostly finding out what query was just executed, rather than what the query will be. Something like this:
function latestQuery()
{
$queries = DB::getQueryLog();
return end($queries);
}
Hopefully that's the same for your purposes.

You can get the SQL query like this:
use DB;//write this at the top of the file above your Class
DB::enableQueryLog();//Enable query logging
$product = Product::where('id', '=', '100')->get();
dd(DB::getQueryLog());//print the SQl query

You can register an event listener in your routes file(in development phase), which will listen for the laravel query event and var_dump the executed query.
Event::listen('illuminate.query', function($sql)
{
var_dump($sql);
});
But this will turn out to be messy. So better use something like Clockwork. It is awesome, you can view all the executed query in your browser.

You can use grammer for subqueries, See below example for reference :
$users = DB::table('users')
->where('user.id', '=', $userID)
->join('product', 'product.userid', '=', 'user.id');
$price = $users->select(array(DB::raw('SUM(price)')))
->grammar
->select($users); // Compiles the statement
$result = DB::table('users')->select(array(DB::raw("({$price}) as price)))->get();
This add a subquery to your main query.

Related

Laravel / Lumen $users = \App\User::find$Uid)->get();

normally i use this:
$users = \App\Users::where('age', '>=', '20')->paginate(20);
But if I use raw querys or use the get(); method from querybuilder, I got an array not a collection or a paginator instance as result.
How can I get my ->links(); from that array result?
Hope all is explained well. :-)
KR,
Marcel
Write{{ $users->links() }} in your view page. According to your query, pagination will show while your data return more than 20 rows.
For better understand Read this doc

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

Case sensitive query in yii2

To find record in yii2 I use following code:
$response = Response::findOne(['unique_url' => $unique_url]);
But it return record regardless $unique_url case.
How to do it case sensitive?
I think you should use LIKE BINARY
and for this you should extended you modelSearch adding the clause in query condition
public function search($params)
{
$query = YuorModel::find();
.......
.......
$query->andFilterWhere(['like binary', 'unique_url', $this->unique_url])
->andFilterWhere(['like', 'your_field2', $this->your_field2])
.......
Best solution which I found for this:
Response::find()->where('BINARY [[unique_url]]=:unique_url', ['unique_url'=>$unique_url])->one();

Paginator on laravel 4.2

I am new to laravel and I am trying to get a pagination function into my result pages, so I have the following function to generate results from query and I would like to have a pagination on the results page, but I don't seem to get it work correctly
public function showResults()
{
$selectedquery = Input::get('Annonces');
$what = Input::get('what');
$where = Input::get('where');
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->get();
return View::make('results',array('results' => $results));
}
Any Help?
Well, for one, you're missing the call to ->paginate(n). Right now, your closure is ->get(), which returns all results for your annonces table. This is good, but doesn't work for pagination. Change the function like so:
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->paginate(10);
This will return all results grouped into 10 results per page. Feel free to change that as you see fit.
Lastly, somewhere on your view where you display the results, you will need to use this code to display a page-viewer:
<?php echo $results->links(); ?>
<!-- OR -->
{{ $results->links(); }}
Also, be sure to check out the docs on Laravel's pagination. You'll find it's pretty comprehensive!
Laravel Pagination
Hope that helps!

Laravel Pagination with appends() Error

I am new to Laravel and am using version 4.1.
I am attempting to query database using pagination and then run the results through the appends() function to add additional parameters to my URL.
Here is the code I am using
$query = DB::table('tableName');
$query->paginate(50);
$results = $query->get();
And that runs as desired. Now when I attempt to create the pagination list (Bootstrap default) and run the following code I get an error.
$pagination = $results->appends(array('key' => 'value'))->links();
This is the error I receive.
Call to a member function appends() on a non-object
I know I'm doing something wrong, I just can't figure out what...
Thanks in advance,
SC
I'm not familiar with the appends function, but you do have an error I can see. Try changing
$query = DB::table('tableName');
$query->paginate(50);
$results = $query->get();
To...
$query = DB::table('tableName');
if(Input::has('someinput')) {
$query->where('someinput', Input::get('someinput'));
}
if(Input::has('otherinput')) {
$query->where('otherinput', Input::get('otherinput'));
}
$results = $query->paginate(50);
Once you run paginate(), an instance of Paginator is returned. I don't see a get() method for Paginator though so I'm not sure how you weren't getting an error there.

Resources