laravel-4

Laravel Eloquent setting a default value for a model relation?

让人想犯罪 __ 提交于 2019-12-06 03:43:35
I have two models: class Product extends Eloquent { ... public function defaultPhoto() { return $this->belongsTo('Photo'); } public function photos() { return $this->hasMany('Photo'); } } class Photo extends Eloquent { ... public function getThumbAttribute() { return 'products/' . $this->uri . '/thumb.jpg'; } public function getFullAttribute() { return 'products/' . $this->uri . '/full.jpg'; } ... } This works fine, I can call $product->defaultPhoto->thumb and $product->defaultPhoto->full and get the path to the related image, and get all photos using $product->photos and looping through the

Many to many relationships with taxonomy in Eloquent

给你一囗甜甜゛ 提交于 2019-12-06 03:25:03
问题 I'm using Laravel 4. I have many to many relationships in my system. And I choose to use Wordpress taxonomy table scheme. But how can I make models relationships with Laravel 4 Eloquent ORM? Here is my database tables; Table terms : +------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------------+------+-----+---------+----------------+ | term_id | bigint(20) unsigned | NO | PRI | NULL | auto

Laravel Seeding Does not Fill in Fields

流过昼夜 提交于 2019-12-06 03:04:55
问题 I have a database seed file: class ContactTableSeeder extends Seeder { public function run() { $contacts = array( array( 'first_name' => 'Test', 'last_name' => 'Contact', 'email' => 'test.contact@emai.com', 'telephone_number' => '0111345685', 'address' => 'Address', 'city' => 'City', 'postcode' => 'postcode', 'position' => 'Director', 'account_id' => 1 ) ); foreach ($contacts as $contact) { Contact::create($contact); } } } When I run php artisan migrate:refresh --seed it seeds the database

Accepting an encoded URL in a Laravel 4 route

爱⌒轻易说出口 提交于 2019-12-06 02:55:25
I'm developing a website in Laravel 4 Beta 5 and I'm trying to pass on an encoded URL to the router. The problem is, an encoded URL has percentages etc. in it, so it is blocked by Laravel. The URL is encoded with the Javascript function encodeURIComponent() . Is there a way to override Laravel so I can use any character in my route? This is my current code: Route::get('add/{encoded_url}', function($encoded_url) { return 'The URL is: '.rawurldecode($encoded_url); }); I have tried to override Laravel by appending where('encoded_url', '*reg-ex*'); , but it didn't work (I'm not very good with reg

Laravel 4 Migrations throwing 1072 error

 ̄綄美尐妖づ 提交于 2019-12-06 02:52:20
问题 I have a couple of migrations in a new Laravel 4 project. One is for regions and the other for areas. Each region has a number of areas, and areas belong to regions. I have used Laravel 4 and the migration functions on a number of occasions but have never come accross this issue before. When I run php artisan migrate:install followed by php artisan migrate I get the following error: $ php artisan migrate [Exception] SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'region_

Method Chaining based on condition

旧街凉风 提交于 2019-12-06 02:49:41
问题 How can you do method chaining based on condition in laravel 4 ? Say if one value is not false then the method inside will be chained to the method called before the if statement. Is it possible in laravel? $data = User::where('username', $somevariable ); if(isset( $somevar_again )) { $data->where('age', 21); } $data->orderBy('reg_date', 'DESC')->get(); return $data->first(); // tried code above and its giving me wrong result in codeigniter I can do this $this->db->select('e.*, v.name_en as v

How can I paginate an array of objects in Laravel?

纵然是瞬间 提交于 2019-12-06 02:43:28
I'm building an application using Laravel 4.2. I have a model for units and another for users and pivot table user_units . Every user in this application can select a unit and add it to his favorite list then he can publish this unit with his information as an ad. I want to select all units published by all users The user_units (pivot) table has the following columns: id user_id unit_id publish adtype addinfo created_at updated_at With relations methods on models public function users() { return $this->belongsToMany('User', 'user_units') ->withPivot('id','publish', 'adtype', 'addinfo'); }

In Laravel how to use snake case for database table columns and camel case for model attributes

≯℡__Kan透↙ 提交于 2019-12-06 02:34:51
问题 I am new to laravel 4 and I am trying to create a rest API following best practices defined by Apigee. One of the best practice defined by apigee is to use camel case for json attribute keys, this way when using the API in Javascript the corresponding objects will follow attributes code convention (camel case). I want to be able to define datatable columns following snake case but when retrieving eloquent objects though my api, the corresponding JSON has to follow camel case. I read about a

How to implement user permissions in Laravel 4?

江枫思渺然 提交于 2019-12-06 02:34:31
What I basically want is user permissions. I've got an table called 'accounts' in my database. There is a column called 'group_id'. I want to set it when the 'group_id' = 3, then the user is admin. Then he can view special sites, buttons, and things like that. I've tried to implement something like that: public function ($roleName) { $role = $this->roles; if ($role->name == $roleName) { return true; } return false; } Also, I don't know what and how the model is needed, do I need an new one and things like that. Old post, but maybe someone will find this useful Add a method to your User model

Laravel Composer Update Error - Permission Denied

╄→尐↘猪︶ㄣ 提交于 2019-12-06 02:25:26
I'm having error during updating composer. It's a Laravel 4 project which I've downloaded from .... and first time I updated it, it was fine and got updated. but due some reason I deleted that replace it with a fresh copy of that project, but now when I started to update it, it gives me the following error: [ErrorException] copy(/home/username/.composer/cache/files/symfony/security-core/3a27d7b34ee6 2cb0fdf5ad970e7777912ef4722f.zip): failed to open stream: Permission denied In between first and second update of this composer I did the following: Tried to installed Vagrant and homestead, but