laravel-4

Use Laravel seed and sql files to populate database

柔情痞子 提交于 2020-06-24 01:40:57
问题 I got several .sql files of countries, states and cities of the world from github. How can I run them with Laravel's seed files to populate those tables in my database? 回答1: Add DB::unprepared() to the run method of DatabaseSeeder . Run php artisan db:seed at the command line. class DatabaseSeeder extends Seeder { public function run() { Eloquent::unguard(); $this->call('UserTableSeeder'); $this->command->info('User table seeded!'); $path = 'app/developer_docs/countries.sql'; DB::unprepared

Use Laravel seed and sql files to populate database

最后都变了- 提交于 2020-06-24 01:39:05
问题 I got several .sql files of countries, states and cities of the world from github. How can I run them with Laravel's seed files to populate those tables in my database? 回答1: Add DB::unprepared() to the run method of DatabaseSeeder . Run php artisan db:seed at the command line. class DatabaseSeeder extends Seeder { public function run() { Eloquent::unguard(); $this->call('UserTableSeeder'); $this->command->info('User table seeded!'); $path = 'app/developer_docs/countries.sql'; DB::unprepared

filtering a paginated eloquent collection

ぃ、小莉子 提交于 2020-06-14 09:39:08
问题 I am trying to filter a paginated eloquent collection, but whenever I use any of the collection methods, I lose the pagination. $models = User::orderBy('first_name','asc')->paginate(20); $models = $models->each(function($model) use ($filters) { if(!is_null($filters['type'])) { if($model->type == $filters['type']) return $model; } if(!is_null($filters['state_id'])) { if($model->profile->state_id == $filters['state_id']) return $model; } if(!is_null($filters['city_id'])) { if($model->profile-

filtering a paginated eloquent collection

旧巷老猫 提交于 2020-06-14 09:38:35
问题 I am trying to filter a paginated eloquent collection, but whenever I use any of the collection methods, I lose the pagination. $models = User::orderBy('first_name','asc')->paginate(20); $models = $models->each(function($model) use ($filters) { if(!is_null($filters['type'])) { if($model->type == $filters['type']) return $model; } if(!is_null($filters['state_id'])) { if($model->profile->state_id == $filters['state_id']) return $model; } if(!is_null($filters['city_id'])) { if($model->profile-

Eloquent query scope on relationship

我怕爱的太早我们不能终老 提交于 2020-06-13 05:58:29
问题 I have two models called Page and User and I've set up the Eloquent relation as follows: public function user() { return $this->belongsTo('User') } When I dd(Page::with('user')->get()) I get the right result. But now I want to have a search filter on this result. I've used a scopeSearch but I'm not sure how to search on the resultset. At the moment I have a scope like this: public function scopeSearch($query, $search) { $query->where('username', 'LIKE', '%'.$search.'%') ->orWhere('name',

How to Call function on laravel pivot model

时光怂恿深爱的人放手 提交于 2020-06-12 07:34:56
问题 I was trying to get the attendance of each user in a course where the users and the courses have many to many relationship and the attendance table is based on the relationship. But i couldn't call $user->pivot->attendance(); directly i had to call for the pivot object it self so i can call a function to it as you can see in the answer The Following is a sample of my Database scheme I need to run $users=$course->users; foreach($users as $user) { $user->pivot->attendance(); //error in this

Extend Resource Controllers

前提是你 提交于 2020-06-12 07:12:12
问题 I'm doing what I want in a certain way and I'm looking for alternatives or better ways of doing. I'm using Resource Controllers in my application. Also I'm using softdelete in several models, so my routes are as follows: Route::get('users/deleted', array('uses' => 'UserController@trash')); Route::put('users/{id}/restore', array('uses' => 'UserController@restore')); Route::resource('users', 'UserController'); The first route is to display objects that have been deleted. The second allows me to

How to delete all the rows in a table using Eloquent?

纵饮孤独 提交于 2020-06-09 07:55:44
问题 My guess was to use the following syntax: MyModel::all()->delete(); But that did not work. I'm sure it's super simple, but I've searched for documentation on the subject and can't find it! 回答1: The reason MyModel::all()->delete() doesn't work is because all() actually fires off the query and returns a collection of Eloquent objects. You can make use of the truncate method, this works for Laravel 4 and 5: MyModel::truncate(); That drops all rows from the table without logging individual row

Counting a many to many relationship?

独自空忆成欢 提交于 2020-06-01 08:28:50
问题 I have a many to many relationship between users and images. User Model public function image() { return $this->belongsToMany('\App\Image'); } Image Model public function user() { return $this->belongsToMany('\App\User'); } Tables users id | name images id | url image_user id | image_id | user_id When a user 'favourites' an image, it's stored in the pivot table. id | image_id | user_id 1 1 1 2 2 1 3 1 2 I need a count of each images favourites. I try something like: Image::with('user')->find

Eloquent relationships laravel

我们两清 提交于 2020-05-19 03:59:27
问题 I have 3 tables categories id name categories_product id category_id product_id and a product table products id name I want products based on catergori_id, how kan i do that with laravel Eloquent? 回答1: Simple as this: Product::whereCategori_id($category)->get(); 来源: https://stackoverflow.com/questions/26920372/eloquent-relationships-laravel