Controller:
public function getSpecificPost($id)
{
$returnArray = with(new Posts)->getSpecificPost($id);
print_r($returnArray);
}
?>
Model:
public function getSpecificPost($post_id)
{
//exit($post_id);
return DB::table('posts')->where('id', $post_id)->toSql();
}
?>
If I uncomment the exit, it gives me a 1 as return.
When I comment the exit I of course get a query, the query is as follows:
select * frompostswhereid= ?
Its Laravel Framework, help me out please!
The ? is a placeholder for your variable. It will be replaced with (in this case) 1. See the colored block on this page:
Note: The Laravel query builder uses PDO parameter binding throughout
to protect your application against SQL injection attacks. There is no
need to clean strings being passed as bindings.
You are aware of the fact that your don't actually execute the query? If you want to get the specific post with $post_id, you can change the line in your model to this one:
return DB::table('posts')->where('id', $post_id)->first();
If you want to see all executed queries (with replaced variables!) with Eloquent, you can use Laravel PHP Debugbar.
Related
I'm working on a old PHP project, that is running in legacy SQL query, which is good but I like to use query builders like Laravel Illuminate SQL package!
So i have added all required package dependencies to run Illuminate SQL, and this query builder seems to work fine with pagination!
$users = Capsule::table('users')->paginate(1)->toArray();
But, the paginator seems not to be able to listen the query string! For example, by running the above code it would give some properties like, next_page , previous_page etc...
And when I try to pass the query string in the URL it can't fetch the data from query string(From the GET request)!
Visiting this page http://app.app/?page=2 would give the same result set.
How i should configure the Illuminate sql package so it can also listen to the query strings?
EDIT
Also, i've tried to use the illuminate/http package, but the $request->all() method always returns an empty array! Here is the code:
<?php
require_once './vendor/autoload.php';
use \Illuminate\Http\Request;
$req = new Request();
echo '<pre>';
print_r($req->all());
echo '</pre>';
It returns empty input data array,
What i am missing to use the http package, any idea would be helpful.
You have to set a page resolver:
\Illuminate\Pagination\Paginator::currentPageResolver(function ($pageName = 'page') {
return (int) ($_GET[$pageName] ?? 1);
});
Laravel uses this resolver.
You need to notice, that paginator won't use other parameters for query string - it just uses page parameter to get valid results page. But for example if you use:
http://app.app/?page=2&price=2
it won't take from database results with price = 2 - it's your job to do this.
However the 2nd thing are urls generated by paginator.
If you do it like this:
$users = Capsule::table('users')->paginate(1);
You can also use in next line
$users->appends(request()->except('page'));
This will add all other parameters from query string (except page) to urls, so first_page_url will then contain all other parameters from request.
You can also wrap it using fluent syntax like this:
$users = Capsule::table('users')->paginate(1)->appends(request()->except('page'));
You need to fetch the query string in the controller to pass the get parameter to
paginate.
You can use the Illuminate\Http\Request class to receive and use the HTTP payload.
Your controller file:
<?php
use Illuminate\Http\Request;
class YourControllerClassName{
public function someFunction(Request $request){
echo "<pre>";
print_r($request->all());
print_r($request->input('page'));
$users = Capsule::table('users')->paginate($request->input('page'))->toArray();
}
}
I try like this :
public function displayList()
{
\DB::enableQueryLog();
$query = Self::orderBy('program_code')->orderBy('output_code')
->orderBy('account_code')->all();
dd(\DB::getQueryLog());
return $query;
}
The result is like this :
[]
It displays an empty array
Is there anyone can help me?
Most suitable way to go about this is listen to db queries. You can do
\DB::listen(function ($query) {
dump($query->sql);
dump($query->bindings);
dump($query->time);
})
in your route file. this will dump out executing db queries. But if you want a much cleaner approach you could wrap above listener inside laravel logger like this.
\Log::info(
\DB::listen(function ($query) {
dump($query->sql);
dump($query->bindings);
dump($query->time);
})
);
then the output will be dump into your-app/storage/logs/laravel.log.
NOTE: Keep in mind to remove or comment out above codes as they are for development purpose only.
Further, you could put it in AppServiceProvider like it is mentioned in database transactions
I suggest you use the package Laravel debugbar: https://github.com/barryvdh/laravel-debugbar. It shows you list of queries executed, and other useful data you want to see.
I have a problem, when I try to run this function in my model it does nothing. The print statement prints out.
DELETE FROM child_participantsWHERE Child_Name='test'
andParent_username='tester2'
Which when I run from command line works correctly(the record exists and is deleted). But when I try it from my web application it gives me no error but does not actually delete anything. I know i am passing data correctly because I receive it in my controller and model. What gives?
function remove_child($username, $participant_name)
{
$where = "`Child_Name`='$participant_name' and`Parent_username`='$username'";
$this->db->where($where, null, false);
$this->db->delete($this->child_table);
echo $this->db->last_query();
}
From the documentation:
If you use multiple function calls they will be chained together with AND between them:
Try changing:
$where = "`Child_Name`='$participant_name' and`Parent_username`='$username'";
to
$this->db->where('Child_Name', $participant_name);
$this->db->where('Parent_username', $username);
// translates to WHERE Child_Name='XXX' and Parent_username='XXX'
Hope this helps!
Do you get the same results when you break it out into two where method calls? I would do this over how you are using the where method.
$this->db->where('Child_Name',$participant_name);
$this->db->where('Parent_username',$username);
$this->db->delete($this->child_table);
also, turn on the profiler to see all the queries that are being run to make sure there are not other parts of code we cannot see that might be interfering or a transaction not being committed
$this->output->enable_profiler(TRUE);
Another suggestion is the practice of soft deletes so that way your data is not truly gone and also minimizes how much you need to rely on reconstructing your log file. Also to make simple CRUD operations faster you can use a very simple extension of the base model. One that I have used by recommendation is https://github.com/jamierumbelow/codeigniter-base-model
Check that does your user has delete privilege in the database. if it has than change your
code like this:
function remove_child($username, $participant_name)
{
$this->db->trans_start();
$this->db->where('Child_Name',$participant_name);
$this->db->where('Parent_username',$username);
$this->db->delete($this->child_table);
$this->db->trans_complete();
return TRUE;
}
i hope that this will solve your problem.
I'm new to Yii and everything seems good, but the problem is, when I`m using the binding params, like (DAO stile):
$command = $this->conn->createCommand($sql);
$command->bindColumn("title", "test_title");
$result = $command->query();
or (Active Record):
$row = Movies::model()->find("m_id=:m_id", array(":m_id"=>27));
or
$row = Movies::model()->findByPk(24);
I've tried everything:
1) added a config param to mysql config. in main.php - 'enableParamLogging' => true
2) changed strings from ' to "
3) added another param just in case of mysql versions - 'emulatePrepare'=>true
Nothing works for me.
I thought that the problem is in params, but bindColumn method doesn't use it, so my presumption is that some module of Yii hasn't been include in config file or something like that.
My model looks like this (created in /models dir):
class Movies extends CActiveRecord {
public static function model($className = __CLASS__) {
parent::model($className);
}
}
Just for everybody to avoid unnecessary questions: the database is configured properly in main.php conf file, there is a table movies, there is a PKs 24 and 27 also.
All native SQL works fine, except using in DAO special methods to bind some params and if in AR using findByPk or find. I hope that this is clear, guys don't bother me with obvious simple technical possibilities, that I can (as U presume) did wrong.
PS Another helpful info - when calling
$command->bindColumn("title", "test_title");
FW's throwing an Exception - CDbCommand and its behaviors do not have a method or closure named "bindColumn". So, as mentioned above, I think that Yii don't see those special methods and this is certain. How can I repair it?
Ok, why this code don't work either? There is no Exceptions, just blank page.
$sql = "SELECT title, year_made FROM movies WHERE year_made=':ym'";
$command = $this->conn->createCommand($sql);
$command->bindParam(":ym", "2012", PDO::PARAM_STR);
$result = $command->query();
The DAO binding isn't working because there is no bindColumn. You only have bindParam, that binds a variable to a column, or bindValue, that binds a value.
I don't know what's wrong with the AR query though.
Edit: Your DAO code should look like this:
$sql = "SELECT * FROM sometable WHERE title = :title";
$command = $this->conn->createCommand($sql);
$command->bindParam(":title", $tile_var);
$result = $command->query();
Have you generated movie model by Gii generator or you wrote it by yourself?
Edit:
As I know you have to add tableName
public function tableName() {
return 'movies';
}
You may have two separate issues. At least your DAO code (if that is what you actually have in your code) is binding wrong.
Binding needs to occur before the query is done (and it's done on the command object), not on the result object. See more info here: http://www.yiiframework.com/doc/guide/1.1/en/database.dao#binding-parameters
As far as your AR queries go, those could also be problematic. You can use findByAttributes instead of this line (although your line should work):
$row = Movies::model()->find("m_id=:m_id", array(":m_id"=>27));
The below should work if you have a standard id key. If you don't (and are using m_id, have you set your model's primaryKey() function up?
$row = Movies::model()->findByPk(24);
Also, you'll be getting a model object back, not a row if that changes how you are handling the results ...
I have some code in Codeigniter that looks like this:
$this->db->select('email_account_id');
$this->db->where('TRIM(LOWER(email_account_incoming_username))',trim(strtolower($email_address)));
$query = $this->db->get($this->users_db.'email_account');
Throws this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND TRIM(LOWER(email_account_incoming_username)) = 'email#server.co.uk'' at line 3
SELECT `email_account_incoming_username`, `email_account_id` FROM (`crmuat_users`.`email_account`) WHERE `email_account_id` IN () AND TRIM(LOWER(email_account_incoming_username)) = 'email#server.co.uk'
Now I don't for the life of me understand how the "IN()" got in there?
Anybody able to see?
You must have called the $this->db->where_in() method somewhere. It is impossible for CodeIgniter Active Record to generate the IN() clause unless you tell it to do so. Try to see if you called the method by accident before you do the select. Also note, that since the Active Record class has this characteristic of singularity in generating query, it is possible that you've called the method somewhere else withing another method that utilizes the Active Record class.
For example, the following will cause the IN() clause to be generated within the final query.
function a()
{
...
$this->db->where_in();
...
$this->b();
}
function b()
{
// Produces the same error as stated in the question because the call of
// where_in() beforehand.
$this->db->select(...)->where(...)->get(...);
}
P.S: Why are you doing trimming and converting the string into lowercase twice? It seems redundant to me.