Eloquent – where not equal to
Use where with a != operator in combination with whereNull Code::where(‘to_be_used_by_user_id’, ‘!=’ , 2)->orWhereNull(‘to_be_used_by_user_id’)->get()
Use where with a != operator in combination with whereNull Code::where(‘to_be_used_by_user_id’, ‘!=’ , 2)->orWhereNull(‘to_be_used_by_user_id’)->get()
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
In php 7.2+ you can’t use count on the relation object, so there’s no one-fits-all method for all relations. Use query method instead as @tremby provided below: $model->relation()->exists() generic solution working on all the relation types (pre php 7.2): if (count($model->relation)) { // exists } This will work for every relation since dynamic properties return … Read more
Laravel supports aliases on tables and columns with AS. Try $users = DB::table(‘really_long_table_name AS t’) ->select(‘t.id AS uid’) ->get(); Let’s see it in action with an awesome tinker tool $ php artisan tinker [1] > Schema::create(‘really_long_table_name’, function($table) {$table->increments(‘id’);}); // NULL [2] > DB::table(‘really_long_table_name’)->insert([‘id’ => null]); // true [3] > DB::table(‘really_long_table_name AS t’)->select(‘t.id AS uid’)->get(); // … Read more
Laravel 4+ Note for Laravel 5 users: You’ll need to call DB::enableQueryLog() before executing the query. Either just above the line that runs the query or inside a middleware. 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 … Read more
The reason MyModel::all()->delete() doesn’t work is because all() actually fires off the query and returns a collection of Eloquent objects. You can make use of the truncate method, this works for Laravel 4 and 5: MyModel::truncate(); That drops all rows from the table without logging individual row deletions.
It is really easy to do a bulk insert in Laravel using Eloquent or the query builder. You can use the following approach. $data = [ [‘user_id’=>’Coder 1’, ‘subject_id’=> 4096], [‘user_id’=>’Coder 2’, ‘subject_id’=> 2048], //… ]; Model::insert($data); // Eloquent approach DB::table(‘table’)->insert($data); // Query Builder approach In your case you already have the data within the … Read more
Query Builder: DB::table(..)->select(..)->whereNotIn(‘book_price’, [100,200])->get(); Eloquent: SomeModel::select(..)->whereNotIn(‘book_price’, [100,200])->get();
I believe this is a perfect use-case for Eloquent events (http://laravel.com/docs/eloquent#model-events). You can use the “deleting” event to do the cleanup: class User extends Eloquent { public function photos() { return $this->has_many(‘Photo’); } // this is a recommended way to declare event handlers public static function boot() { parent::boot(); static::deleting(function($user) { // before delete() method … Read more
Mass assignment is when you send an array to the model creation, basically setting a bunch of fields on the model in a single go, rather than one by one, something like: $user = new User(request()->all()); (This is instead of explicitly setting each value on the model separately.) You can use fillable to protect which … Read more