Posts

Showing posts with the label Laravel 4

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...

Add "ON DELETE CASCADE" To Existing Column In Laravel

Answer : Drop foreign key first. Thanks to Razor for this tip $table->dropForeign('answers_user_id_foreign'); $table->foreign('user_id') ->references('id')->on('users') ->onDelete('cascade'); $table->foreign('user_id') ->references('id')->on('users') ->onDelete('cascade'); In my case, i'll need to put the col name in an array else that will be an error. Schema::table('transactions', function (Blueprint $table) { $table->dropForeign(['transactions_order_id_foreign']); $table->foreign('order_id') ->references('id')->on('orders') ->onDelete('cascade') ->change(); }); mysql 5.7 ver