laravel-query-builder

A JOIN With Additional Conditions Using Query Builder or Eloquent

给你一囗甜甜゛ 提交于 2019-11-27 05:07:01
问题 I'm trying to add a condition using a JOIN query with Laravel Query Builder. <?php $results = DB::select(' SELECT DISTINCT * FROM rooms LEFT JOIN bookings ON rooms.id = bookings.room_type_id AND ( bookings.arrival between ? and ? OR bookings.departure between ? and ? ) WHERE bookings.room_type_id IS NULL LIMIT 20', array('2012-05-01', '2012-05-10', '2012-05-01', '2012-05-10') ); I know I can use Raw Expressions but then there will be SQL injection points. I've tried the following with Query

How to Use Order By for Multiple Columns in Laravel 4?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 01:14:16
I want to sort multiple columns in Laravel 4 by using the method orderBy() in Laravel Eloquent. The query will be generated using Eloquent like this: SELECT * FROM mytable ORDER BY coloumn1 DESC, coloumn2 ASC How can I do this? Simply invoke orderBy() as many times as you need it. For instance: User::orderBy('name', 'DESC') ->orderBy('email', 'ASC') ->get(); Produces the following query: SELECT * FROM `users` ORDER BY `name` DESC, `email` ASC You can do as @rmobis has specified in his answer, [Adding something more into it] Using order by twice: MyTable::orderBy('coloumn1', 'DESC') ->orderBy(

Creating and Update Laravel Eloquent

♀尐吖头ヾ 提交于 2019-11-27 00:03:52
What's the shorthand for inserting a new record or updating if it doesn't exist? <?php $shopOwner = ShopMeta::where('shopId', '=', $theID) ->where('metadataKey', '=', 2001)->first(); if ($shopOwner == null) { // Insert new record into database } else { // Update the existing record } weotch Here's a full example of what "lu cip" was talking about: $user = User::firstOrNew(array('name' => Input::get('name'))); $user->foo = Input::get('foo'); $user->save(); Below is the updated link of the docs which is on the latest version of Laravel Docs here: Updated link Updated: Aug 27 2014 - [

Get the Query Executed in Laravel 3/4

那年仲夏 提交于 2019-11-26 18:03:50
How can I retrieve the raw executed SQL query in Laravel 3/4 using Laravel Query Builder or Eloquent ORM? For example, something like this: DB::table('users')->where_status(1)->get(); Or: (posts (id, user_id, ...)) User::find(1)->posts->get(); Otherwise, at the very least how can I save all queries executed to laravel.log? Laravel 4+ In Laravel 4 and later, you have to call DB::getQueryLog() to get all ran queries. $queries = DB::getQueryLog(); $last_query = end($queries); Or you can download a profiler package. I'd recommend barryvdh/laravel-debugbar , which is pretty neat. You can read for

Laravel join query with conditions

旧街凉风 提交于 2019-11-26 14:55:35
问题 I have 4 tables. User table: id col1 col2 CoursesAssigned table: id user_id course_id approved CourseInfo table: id parent_id CourseParents table: id start_date end_date I think the table names and its column names are self explanatory. I have 2 kinds of users - i) assigned ii) unassigned. I show them in 2 different pages. While showing the assigned students I need those students from users table for each of whom there is at least one row in CoursesAssigned table where user_id is his own user

Laravel 4 Eloquent Query Using WHERE with OR AND OR?

萝らか妹 提交于 2019-11-26 11:33:21
How do I say WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1) For more complicated queries am I supposed to use raw SQL? Make use of Parameter Grouping ( Laravel 4.2 ). For your example, it'd be something like this: Model::where(function ($query) { $query->where('a', '=', 1) ->orWhere('b', '=', 1); })->where(function ($query) { $query->where('c', '=', 1) ->orWhere('d', '=', 1); }); Yilmazerhakan If you want to use parameters for a,b,c,d in Laravel 4 Model::where(function ($query) use ($a,$b) { $query->where('a', '=', $a) ->orWhere('b', '=', $b); }) ->where(function ($query) use ($c,$d) { $query-

How Do I Get the Query Builder to Output Its Raw SQL Query as a String?

♀尐吖头ヾ 提交于 2019-11-26 11:28:54
Given the following code: DB::table('users')->get(); I want to get the raw SQL query string that the database query builder above will generate. In this example, it would be SELECT * FROM users . How do I do this? jfortunato To output to the screen the last queries ran you can use this: DB::enableQueryLog(); // Enable query log // Your Eloquent query dd(DB::getQueryLog()); // Show results of log I believe the most recent queries will be at the bottom of the array. You will have something like that: array(1) { [0]=> array(3) { ["query"]=> string(21) "select * from "users"" ["bindings"]=> array

Get Specific Columns Using “With()” Function in Laravel Eloquent

不想你离开。 提交于 2019-11-26 10:13:39
I have two tables, User and Post . One User can have many posts and one post belongs to only one user . In my User model I have a hasMany relation... public function post(){ return $this->hasmany('post'); } And in my post model I have a belongsTo relation... public function user(){ return $this->belongsTo('user'); } Now I want to join these two tables using Eloquent with() but want specific columns from the second table. I know I can use the Query Builder but I don't want to. When in the Post model I write... public function getAllPosts() { return Post::with('user')->get(); } It runs the

How to Use Order By for Multiple Columns in Laravel 4?

我的未来我决定 提交于 2019-11-26 09:35:42
问题 I want to sort multiple columns in Laravel 4 by using the method orderBy() in Laravel Eloquent. The query will be generated using Eloquent like this: SELECT * FROM mytable ORDER BY coloumn1 DESC, coloumn2 ASC How can I do this? 回答1: Simply invoke orderBy() as many times as you need it. For instance: User::orderBy('name', 'DESC') ->orderBy('email', 'ASC') ->get(); Produces the following query: SELECT * FROM `users` ORDER BY `name` DESC, `email` ASC 回答2: You can do as @rmobis has specified in

Creating and Update Laravel Eloquent

有些话、适合烂在心里 提交于 2019-11-26 09:06:46
问题 What\'s the shorthand for inserting a new record or updating if it doesn\'t exist? <?php $shopOwner = ShopMeta::where(\'shopId\', \'=\', $theID) ->where(\'metadataKey\', \'=\', 2001)->first(); if ($shopOwner == null) { // Insert new record into database } else { // Update the existing record } 回答1: Here's a full example of what "lu cip" was talking about: $user = User::firstOrNew(array('name' => Input::get('name'))); $user->foo = Input::get('foo'); $user->save(); Below is the updated link of