laravel-4

How to set local timezone in laravel

天涯浪子 提交于 2019-12-10 04:22:51
问题 Is there a way to set local timezone in laravel? In config/app.php 'timezone' => 'UTC', What should be added so that timezone value above uses local timezone? After some research, stumbled upon the following PHP way of dealing with it: $userTimezone = Auth::user()->timezone; date_default_timezone_set($userTimezone); // and change the configuration so they match Config::set('app.timezone', $userTimezone) But, Is there an elegant solution for this other than converting the timezone using the

Default scope for Eloquent-models?

一世执手 提交于 2019-12-10 03:41:52
问题 Here is a sample database-table ( users ): id - int(11) auto_increment name - varchar(100) banned - int(1) The column banned is a boolean value which is 0 ( false ) by default. If an user has been banned, the value is 1 . I'd like to exclude any banned users from all queries by default . I could create a query scope and then use that everywhere. However, I'd much more prefer simply having that check on by default. I could also create a newQuery -method of my own, like so: // Inside User-model

Laravel Eloquent, select only rows where the relation exists

亡梦爱人 提交于 2019-12-10 03:09:01
问题 I am trying to select from a table, but I only want to select things that have an existing relationship. For example, if I have Users and Comments, and Users haveMany Comments, I want to do something like: User::hasComments()->paginate(20); So, I only want to select Users that have at least 1 Comment, and paginate the result of that query. Is there any way to do this? 回答1: According to Laravel's Eloquent documentation for querying relations (look for the "Querying Relationship Existence"

Laravel 4 Validation is Broken

混江龙づ霸主 提交于 2019-12-10 03:05:51
问题 A simple date format mm/dd/yyyy validation is all I need... $rules = array( 'renewal_date' => array('required', 'date_format:?') ); What do I set the date format to? The Laravel documentation could be so much better. 回答1: Documentation is pretty clear to me, you should use date_format:format "The field under validation must match the format defined according to the date_parse_from_format PHP function." Looking at it: http://php.net/manual/en/function.date-parse-from-format.php, I see you can

Laravel: how to add where clause using query builder?

[亡魂溺海] 提交于 2019-12-10 03:01:06
问题 I have this query, made using Laravel query builder: $rows = DB::table('elements')->where('type', 1); That corresponds to: "SELECT * from elements WHERE type=1" Now, in some cases I need to add a second Where to create a query like this: SELECT * from elements WHERE type=1 AND lang='EN' Using classic php I'd do something like: $sql = 'SELECT * from elements WHERE type=1'; if($var==true) $sql .= " AND lang='EN'"; How can I do that using Laravel Query Builder? Thank you. 回答1: You may try

Does Eloquent relation sync also remove?

让人想犯罪 __ 提交于 2019-12-10 03:00:44
问题 When updating a model and I sync a relationship, if I don't pass in all the ids that already exist, will that relationship be removed? 回答1: You decide: sync has 2nd parameter that defaults to true and is responsible for detaching: $model->relationship()->sync([1,2,3]); $model->relationship()->sync([4,5,6]); // attached [4,5,6], detached [1,2,3] $model->relationship()->getRelatedIds(); // [4,5,6] // but: $model->relationship()->sync([4,5,6], false); // attached [4,5,6], detached [] $model-

SQLSTATE[HY000] [2002] Permission denied

北城以北 提交于 2019-12-10 02:58:47
问题 I am getting this error don't know the reason SQLSTATE[HY000] [2002] Permission denied and here is the website on which i am tryiing to upload my file example.com . 回答1: This happen because selinux avoid db connections from the httpd server to the remote db server. To solve it you need to access your server trough ssh or just open a console if you have pretencial acces and do the follow: You must check in the SELinux if the port 80 is managed in. You can check it typing # semanage port -l |

Laravel composer install hangs on “Installing dependencies” and consume a lot of resources (CPU, DISK)

柔情痞子 提交于 2019-12-10 02:51:57
问题 Problem 1: I tried install laravel 4 on my server (Ubuntu 12.04, nginx, php5-fpm) but installation hangs: # composer install --verbose Loading composer repositories with package information Installing dependencies (including require-dev) - no errors, no nothing, it's just hangs and that's all. I've tried installation using composer create-project laravel/laravel --prefer-dist ...and result was pretty the same: Installing laravel/laravel (v4.0.9) - Installing laravel/laravel (v4.0.9) Loading

Laravel Blade - pass variable via an @include or @yield

天大地大妈咪最大 提交于 2019-12-10 02:43:35
问题 I need to pass a variable to an included Blade file. I have attempted this two-ways; however, neither have been successful. Pass a variable, title , to the included file: @section('left') @include('modal', ['title' => 'Hello']) @stop Use @yield and set the section: @section('left') @include('modal') @section('title') Hello @stop @stop I am using Laravel 4.2. I am unaware if what I am trying to do is possible, but I imagine it is. 回答1: According to the documentation, the include -way should be

How do I use a package language file?

一笑奈何 提交于 2019-12-10 02:39:59
问题 I'm trying to use a Laravel 4 package language file, but I don't know how to do it. I created a package with php artisan workbench vendor/package --resources . I then create file workbench/vendor/package/src/lang/en/routes.php . In that routes file a I have this: <?php return [ 'foo' => 'bar' ]; Now how do I access that? I tried with Lang::get('routes.foo') and Lang::get('vendor/package::routes.foo') but both fails and just gives me the parameter itself I entered. I'm calling it in my service