Posts

Showing posts with the label Eloquent

A __construct On An Eloquent Laravel Model

Answer : You need to change your constructor to: public function __construct(array $attributes = array()) { parent::__construct($attributes); $this->directory = $this->setDirectory(); } The first line ( parent::__construct() ) will run the Eloquent Model 's own construct method before your code runs, which will set up all the attributes for you. Also the change to the constructor's method signature is to continue supporting the usage that Laravel expects: $model = new Post(['id' => 5, 'title' => 'My Post']); The rule of thumb really is to always remember, when extending a class, to check that you're not overriding an existing method so that it no longer runs (this is especially important with the magic __construct , __get , etc. methods). You can check the source of the original file to see if it includes the method you're defining.

Bulk Insertion In Laravel Using Eloquent ORM

Answer : You can just use Eloquent::insert() . For example: $data = array( array('name'=>'Coder 1', 'rep'=>'4096'), array('name'=>'Coder 2', 'rep'=>'2048'), //... ); Coder::insert($data); We can update GTF answer to update timestamps easily $data = array( array( 'name'=>'Coder 1', 'rep'=>'4096', 'created_at'=>date('Y-m-d H:i:s'), 'modified_at'=> date('Y-m-d H:i:s') ), array( 'name'=>'Coder 2', 'rep'=>'2048', 'created_at'=>date('Y-m-d H:i:s'), 'modified_at'=> date('Y-m-d H:i:s') ), //... ); Coder::insert($data); Update: to simplify the date we can use carbon as @Pedro Moreira suggested $now = Carbon::now('utc')->toDateTimeString(); $data = array( ...

Creating And Update Laravel Eloquent

Answer : 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 - [ updateOrCreate Built into core...] Just in case people are still coming across this... I found out a few weeks after writing this, that this is in fact part of Laravel's Eloquent's core... Digging into Eloquent’s equivalent method(s). You can see here: https://github.com/laravel/framework/blob/4.2/src/Illuminate/Database/Eloquent/Model.php#L553 on :570 and :553 /** * Create or update a record matching the attributes, and fill it with values. * * @param array $attributes * @param array $values * @return static */ public static function updateOrCreate(array $attributes, array $values = ar...

Convert Timestamp To Date In Laravel?

Answer : timestamp columns like created_at are being parsed first. the U is simply the timestamp format. you can return your own format. for other formats see date docs. edit: as stated in my comment, getDateFormat() is for both ways (insert, selects). your best bet would be using format inside the model. example: public function getMyBirthdayAttribute() { return $this->my_birthday->format('d.m.Y'); } use $model->my_birthday to call the attribute. // controller $posts = Post::all(); // within sometemplate.blade.php @foreach($posts as $post) my formatted date: {{ $post->my_birthday }} @endforeach The better way to manage dates with Laravel IMHO is to use the getDates accessor that is build into Laravel. All you need to do is to set a method on your Model as such. public function getDates() { return [ 'my_birthday', 'created_at', 'updated_at', ]; } This will return a Carbon object. Carbon is insa...