laravel-query-builder

Get the Query Executed in Laravel 3/4

烈酒焚心 提交于 2019-11-26 06:10:38
问题 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? 回答1: 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

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

谁都会走 提交于 2019-11-26 03:24:31
问题 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

Laravel 4 Eloquent Query Using WHERE with OR AND OR?

戏子无情 提交于 2019-11-26 02:27:35
问题 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? 回答1: 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); }); 回答2: If you want to use parameters for a,b,c,d in Laravel 4 Model::where(function ($query) use ($a,$b) { $query-

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

偶尔善良 提交于 2019-11-26 02:03:41
问题 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? 回答1: 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

How to Create Multiple Where Clause Query Using Laravel Eloquent?

末鹿安然 提交于 2019-11-26 01:34:47
问题 I\'m using the Laravel Eloquent query builder and I have a query where I want a WHERE clause on multiple conditions. It works, but it\'s not elegant. Example: $results = User::where(\'this\', \'=\', 1) ->where(\'that\', \'=\', 1) ->where(\'this_too\', \'=\', 1) ->where(\'that_too\', \'=\', 1) ->where(\'this_as_well\', \'=\', 1) ->where(\'that_as_well\', \'=\', 1) ->where(\'this_one_too\', \'=\', 1) ->where(\'that_one_too\', \'=\', 1) ->where(\'this_one_as_well\', \'=\', 1) ->where(\'that_one