|
Suppose you have 'article' table in your database, and you're going to conduct a Search action WHERE your article is published AND your Title contains your keyword OR your Content contains your keyword. some may do code like this
public static function getSearch($search=null){
$result = DB::table('article')
->where('article.is_publish','1')
->where('article.title','LIKE','%'.$search.'%')
->orWhere('article.content', 'LIKE','%'.$search.'%')
->select('*')
->orderBy('article.id', 'desc')->paginate(10);
return $result;
}
The above code will result a raw Query like below :
SELECT * FROM `article` WHERE `article`.`is_publish` = '1' AND `article`.`title` LIKE '%search%' OR `article`.`content` LIKE '%search%'...
That's a correct Query but will give you wrong result as your Published and unPublished article will be showing. We can fix it Using Advance Where of Query Builder in Laravel .
public static function getSearch($search=null){
$result = DB::table('article')
->where('article.is_publish','1')
->where(function($query) use ($search){
$query->where('article.title','LIKE','%'.$search.'%')
->orWhere('article.content', 'LIKE','%'.$search.'%');
})
->select('*')
->orderBy('article.id', 'desc')->paginate(10);
return $result;
}
Code above will result Query string as follow :
SELECT * FROM `article` WHERE `article`.`is_publish` = '1' AND (`article`.`title` LIKE '%search%' OR `article`.`content` LIKE '%search%')...
This will give you the correct result. CMIIW
Reference
modified 6-Nov-14 22:52pm.
|
|
|
|
|
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. - http://nodejs.org/
If you dont want to install it via Terminal , you check itu here http://nodejs.org/download/
Install Node.js
you can Install it using Chris Lea launchpad repo, and type these commands one by one :
sudo apt-get install python-software-properties
sudo apt-add-repository ppa:chris-lea/node.js
sudo apt-get update
sudo apt-get install nodejs
Test your nodes to make sure that its installed successfully by just typing 'node' :
node
> 3 + 3
6
or create a file somewhere in your directory (for ex : app.js )
and type this :
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
save the file, then execute it via terminal like this:
node app.js
After that you can check your installed node Version with this command :
node -v
To make your life easier with Nodejs, you can install NPM ( Nodejs Package Modules ). its a module manager built for Nodejs.
Install NPM
type this command in your terminal
sudo apt-get install npm
once installed, you can now easyly manage modules or packages you wish to include tou your Nodejs Project
Install Express
Express is a Nodejs Framework. And again this cool stuff could make your node-life is easier.
type this to install it
npm install express
to make an easy Application or project with express you can install it again (doesnt matter) with this :
npm install -g express
and now , to create your nodes App you can just type
express myapp
Express will generate your default ( you modify it your way later) modules for you like this
create : myapp
create : myapp/package.json
create : myapp/app.js
create : myapp/public
create : myapp/public/javascripts
create : myapp/public/images
create : myapp/public/stylesheets
create : myapp/public/stylesheets/style.css
create : myapp/routes
create : myapp/routes/index.js
create : myapp/routes/user.js
create : myapp/views
create : myapp/views/layout.jade
create : myapp/views/index.jade
Run you apps with
cd myapp
node app.js
And your Nodes is ready !!!.
I have written an example of modular nodejs app here
Making A Modular Web Applications With Nodejs And Express
Source
modified 6-Nov-14 22:35pm.
|
|
|
|