eloquent

Convention table names (with underscore)

隐身守侯 提交于 2020-01-01 01:30:15
问题 What is the correctly table name of this table in Laravel 3/4? Structure image_projects (id, project_id, image, ext, size, created_at, updated_at, active) image_projects imageprojects imageProjects And, how I can create the Model? app/models/image_projects.php app/models/imageprojects.php app/models/imageProjects.php app/models/image/projects.php app/models/projects/image.php 回答1: It makes no difference what you name your table, as long as you then name the file & class in singular form, with

laravel migration re-organising column order

五迷三道 提交于 2019-12-31 17:51:16
问题 When you create a new column in a table you can use the ->after('column name') to dictate where it goes. How can I create a migration that re-orders the columns in the right order I want? 回答1: Try this, hope it help you to find right solution: public function up() { DB::statement("ALTER TABLE example MODIFY COLUMN foo DATE AFTER bar"); } public function down() { DB::statement("ALTER TABLE example MODIFY COLUMN foo DATE AFTER bar"); } 回答2: If you want to do it without destroying data, you

A __construct on an Eloquent Laravel Model

为君一笑 提交于 2019-12-31 12:14:05
问题 I have a custom setter that I'm running in a __construct method on my model. This is the property I'm wanting to set. protected $directory; My Constructor public function __construct() { $this->directory = $this->setDirectory(); } The setter: public function setDirectory() { if(!is_null($this->student_id)){ return $this->student_id; }else{ return 'applicant_' . $this->applicant_id; } } My problem is that inside my setter the, $this->student_id (which is an attribute of the model being pulled

Eloquent where condition based on a “belongs to” relationship

我与影子孤独终老i 提交于 2019-12-31 08:59:34
问题 Let's say I have the following model: class Movie extends Eloquent { public function director() { return $this->belongsTo('Director'); } } Now I'd like fetch movies using a where condition that's based on a column from the directors table. Is there a way to achieve this? Couldn't find any documentation on conditions based on a belongs to relationship. 回答1: You may try this (Check Querying Relations on Laravel website): $movies = Movie::whereHas('director', function($q) { $q->where('name',

Eloquent where condition based on a “belongs to” relationship

匆匆过客 提交于 2019-12-31 08:59:31
问题 Let's say I have the following model: class Movie extends Eloquent { public function director() { return $this->belongsTo('Director'); } } Now I'd like fetch movies using a where condition that's based on a column from the directors table. Is there a way to achieve this? Couldn't find any documentation on conditions based on a belongs to relationship. 回答1: You may try this (Check Querying Relations on Laravel website): $movies = Movie::whereHas('director', function($q) { $q->where('name',

Eloquent filter on pivot table created_at

橙三吉。 提交于 2019-12-31 07:35:08
问题 I want to filter the contents of two tables which have an Eloquent belongsToMany() to each other based on the created_at column in the pivot table that joins them. Based on this SO question I came up with the following: $data = ModelA::with(['ModelB' => function ($q) { $q->wherePivot('test', '=', 1); }])->get(); Here I'm using a simple test column to check if it's working, this should be 'created_at'. What happens though is that I get all the instances of ModelA with the ModelB information if

Laravel advanced search query fix

人盡茶涼 提交于 2019-12-31 07:15:33
问题 I have a search form with multiple input and select boxes I need help to get if conditions in my query in order to each part works separately and all at once. here is my blade codes: <form action="{{route('advancesearch')}}" method="post"> {{csrf_field()}} <div class="sidebar-title"> <span>Advanced Search</span> <i class="fa fa-caret-down show_sidebar_content" aria-hidden="true"></i> </div> <!-- ./sidebar-title --> <div id="tags-filter-content" class="sidebar-content"> <div class="filter-tag

Creating new record and relationships in one go

馋奶兔 提交于 2019-12-31 05:57:04
问题 I have the following basic schema: players id name profiles id player_id email subsets id profile_id alias I was under the impression the following operation was possible when creating a new record: Player::create([ 'name' => 'Player 1', 'profile.email' => 'player1@email.com', 'profile.subset.alias' => 'Player 1 alias' ]); Since this code doesn't seem to work, is there anyway to save relationships records together with the create method? 回答1: Basically, you can't do this as easy as it looks.

How to use orderby on element that was joined with Laravel Eloquent method WITH

本秂侑毒 提交于 2019-12-31 05:13:11
问题 The problem is that the query can't find the specific_method(specific_method, specific_model,SpecificModel,specificMethod etc...), that should been joined with the method WITH from Laravel Eloquent. Any ideas how to solve it? My code: //SpecificModel <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class SpecificModel extends Model { protected $guard_name = 'web'; protected $table = 'SpecificTable'; protected $guarded = ['id']; public function specificMethod(){ return $this

Laravel 4 eloquent models chaining additional where clauses

江枫思渺然 提交于 2019-12-31 04:53:05
问题 I Have the following code (member is just a standard Eloquent model) $members = new Member; $members->where('user_id', '=', 5); $_members = $members->get(); The last query run produces " SELECT * from members ", so it seems to be ignoring my where clause, what am I doing wrong here? By the way I know I could do $members = new Member::where(...) etc... but I will be adding the where clauses in a loop in order to create filtering on the results from the database. UPDATE The only way around this