Controller Variable not Showing up in Query Builder Laravel - php

I am encountering a strange issue in Laravel.
The below is an index function in one of my controllers.
public function index($merchant_url_text)
{
//
$deals = DB::table('tbl_deal')
-> join ('tbl_merchant', 'tbl_deal.merchant_id', '=', 'tbl_merchant.merchant_id')
-> where ('merchant_url_text', $merchant_url_text) -> toSql();
//return $merchant_url_text.$deal_id;
dd($deals);
//return $merchant_url_text;
}
As you can see I am passing merchant_url_text from route.
Route::get('/testroute/{merchant_url_text}', ['uses' =>'dealsVisibleController#index']);
When I am trying to debug the query by printing it, I am getting
"select * from `tbl_deal` inner join `tbl_merchant` on `tbl_deal`.`merchant_id` = `tbl_merchant`.`merchant_id` where `merchant_url_text` = ?"
This means that the query builder is not reading the $merchant_url_text variable. However, when I return just that variable, it is being printed.
Just can't figure out why the query builder is not able to include the $merchant_url_text variable in the query when it is available in the index function.
Any suggestions.

I am pretty sure that your code is correct. The SQL output function toSql() does not show the values of variables and only prints out a ? for security reasons.

You may access all your queries by using
$queries = DB::getQueryLog();
It is also printing the query parameters as array.
To get the last query:
dd(end($queries));
To disable the log:
DB::connection()->disableQueryLog();
See the docs for further information.

Related

Laravel use query result in a controller

I have a problem with laravel
I want to use a query result in my controller with a if clause to manage what i return to the view.
$res = Chaussures::where('id',$id);
if($res->name=='Adidas'){
return...
}
else {
return...
}
I tried this but it didn't work
You need to execute the query to get the result first. Then you can do whatever you need with the data.
$res=Chaussures::where('id',$id)->first();
Searching for models by ID is such a common task that Laravel has a method called find() to make it easier. This line does the same as the above.
$res=Chaussures::find($id);

DB::select failing in Laravel 5.2

I have a Laravel 5.2.45 application, also, I have a complex query to do, so I tried to consult it using:
DB::select("the query");
I read that this should work, but it's not the case, so just testing I simplified the query to: "Select * from aTable" but it also doesn't give any result, it takes a long long timeloading the webpage and then just doesnt show anything. I'm using this exactly: dd(DB::select("SELECT * FROM myTable AS mt"))
So, I'm wondering what is exaclty happening, it's still a valid function in Laravel 5.2? it's a really simple query and am not sure in what is failing. Thanks in advance!
I think you are trying to execute raw query. If you execute raw query in laravel please try this way:
$tableData = DB::select( DB::raw("SELECT * FROM table WHERE id = 100 ") );
dd($tableData);
You can set custom function also for printing data in your helper function like
function pr($var){
echo "<pre>";
print_r($var);
echo "</pre>";
}
than you can call pr($tableData);
I think this should work for you. Thank You :)
It's strange but you can check your Query Log to find problem
$users = DB::select('SELECT * FROM myTable AS mt');
print_r(DB::enableQueryLog());
How to enable query log
https://github.com/laravel/framework/commit/e0abfe5c49d225567cb4dfd56df9ef05cc297448
Try this:
$users = DB::table('myTable')->get();

Laravel 5 Using orderBy() with Models

So I can't for the life of me figure out why I can't use orderBy. I'm getting the error:
Call to undefined method Illuminate\Database\Eloquent\Collection::orderBy()
Here is my code:
Route::get('/teams', function(){
$page = 'Teams';
$teams = App\Team::all()->orderBy('teamFirstName')->get();
return view('teams')->with('page', $page)->with('allTeams', $teams);
});
I have tried removing the ->get(); I have tried using just Team::all. I'm using a Laravel cheat sheet and I seem to be following the Model syntax. I have also double checked that it is the right column name in my DB and have even tried using just id.
If I remove the ->orderBy() the query works fine and I can get all of the teams. So what am I doing wrong?
It's because all() returns a Collection which does not have an orderBy method (though it does have a sortBy method which you could use). The following is a bit simpler though and should perform better.
$teams = App\Team::orderBy('teamFirstName')->get();
Once you call all(), the query is ran and results are fetched and any ordering done after this would be done by PHP. It's usually best to let the database handle the ordering though so use orderBy() first before fetching the results with get().
This line:
$teams = App\Team::all()->orderBy('teamFirstName')->get();
Need to be:
$teams = App\Team::orderBy('teamFirstName')->get();
Note:
user3158900 has a good explanation.

Laravel Eloquent ORM get function working Confusion

I am new to laravel so can anybody explain me that what exactly get() function of eloquent ORM does. I have following query and I need to know on which line the database is queried.
$propertiesQuery = Property::with('country', 'city','bannerInvoicesCount','rejectedBannerInvoicesCount')
->where('is_deleted','=',1);
if(Auth::user()->type == 'po') {
$propertiesQuery->where('user_id', '=', Auth::user()->id);
}
$propertiesQuery->orderBy('created_at', 'DESC');
$properties = $propertiesQuery->get();
Now in above code I have accessed Model once whether that is the point when db is queried or when I have called get() function. I am not been able to comprehend the internal working of this. That exactly on which statement the query is executed.
#Raza before eloquant $propertiesQuery->get() step, it prepare your query like:
select * from table where user_id=1
and when you call/trigger the $propertiesQuery->get() method, it forward your query to database and fetch the records against your query. like, normally we do in plain sql:
eg: mysql_query($query);
then mysql_fetch_assoc() etc methods
so get() method contains on: execute the query and return all the records in the array format.
I hope it will help you.

Codeigniter using different function's where clause

I have a Postgres database with a table for orders and a table order_contents, which includes all items of the order, and the order_id. I'm trying to make a view with order history.
In my model I have two functions, one to retrieve orders, and another to retrieve all the contents of an order, like so:
function retrieve_orders($userID){
$query = $this->db->get('orders');
$this->db->where('user_id', $userID);
return $query->result_array();
}
function get_order_contents($orderID){
$query = $this->db->get('order_contents');
$this->db->where('order_id', $orderID);
return $query->result_array();
}
So in my controller I call retrieve_orders() and pass the resulting array to the view. In the view, I want to create an HTML table for each order with all it's contents. So I iterate through each order and call get_order_contents in the loop.
Everything should work, however what happens is this error shows up in the source:
Error Number: ERROR: column "user_id" does not exist
LINE 3: WHERE "user_id" = '3'
^SELECT *
FROM "order_contents"
WHERE "user_id" = '3'
As you can see, for some reason, when I call get_order_contents() in the view, it uses the where clause I specified for the retrieve_orders() function.
What I have done so far is try to manually stop caching, but to no avail. Any help here is appreciated.
In each function, try to put where() before get().
When you try to get() order_contents it will use where clause from previous where().
Common practice is, use get() at the last call of $this->db after other calls such as where(), etc. See the Active Record documentation in codeigniter.
Chain your db functions like this
return $this->db->where('user_id', $userID)
->get('orders')
->result_array();
why not use $this->db->get_where()

Categories