Bulk Insertion in Laravel using eloquent ORM
You can just use Eloquent::insert(). For example: $data = [ [‘name’=>’Coder 1’, ‘rep’=>’4096’], [‘name’=>’Coder 2’, ‘rep’=>’2048’], //… ]; Coder::insert($data);
You can just use Eloquent::insert(). For example: $data = [ [‘name’=>’Coder 1’, ‘rep’=>’4096’], [‘name’=>’Coder 2’, ‘rep’=>’2048’], //… ]; Coder::insert($data);
You’ll need to order by the same field you’re ordering by now, but descending. As an example, if you have a time stamp when the upload was done called upload_time, you’d do something like this; For Pre-Laravel 4 return DB::table(‘files’)->order_by(‘upload_time’, ‘desc’)->first(); For Laravel 4 and onwards return DB::table(‘files’)->orderBy(‘upload_time’, ‘desc’)->first(); For Laravel 5.7 and onwards return … Read more
You can do it like this: Table::select(‘name’,’surname’)->where(‘id’, 1)->get();
Add it in two steps, and it’s good to make it unsigned too: public function up() { Schema::create(‘priorities’, function($table) { $table->increments(‘id’, true); $table->integer(‘user_id’)->unsigned(); $table->string(‘priority_name’); $table->smallInteger(‘rank’); $table->text(‘class’); $table->timestamps(‘timecreated’); }); Schema::table(‘priorities’, function($table) { $table->foreign(‘user_id’)->references(‘id’)->on(‘users’); }); }
If you are using post as a model (without dependency injection), you can also do: $posts = Post::orderBy(‘id’, ‘DESC’)->get();
You either have to declare public $timestamps = false; in every model, or create a BaseModel, define it there, and have all your models extend it instead of eloquent. Just bare in mind pivot tables MUST have timestamps if you’re using Eloquent. Update: Note that timestamps are no longer REQUIRED in pivot tables after Laravel … Read more
The problem is caused by the fact that the Model‘s toArray() method ignores any accessors which do not directly relate to a column in the underlying table. As Taylor Otwell mentioned here, “This is intentional and for performance reasons.” However there is an easy way to achieve this: class EventSession extends Eloquent { protected $table=”sessions”; … Read more
You can actually do this within the query. $results = Project::orderBy(‘name’)->get(); This will return all results with the proper order.
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
Laravel 5 now supports changing a column; here’s an example from the offical documentation: Schema::table(‘users’, function($table) { $table->string(‘name’, 50)->nullable()->change(); }); Source: http://laravel.com/docs/5.0/schema#changing-columns Laravel 4 does not support modifying columns, so you’ll need use another technique such as writing a raw SQL command. For example: // getting Laravel App Instance $app = app(); // getting laravel … Read more