eloquent

Laravel sql server change table name for schema

隐身守侯 提交于 2020-01-14 02:50:30
问题 I'm working on a project and i'm trying to set some tables with a schema on my SQL Server Data Base, so i manually change the tables name to the schema_name.table_name on the migration file and it worked, so here is an example: Schema::create('electoral.centro_votacions', function (Blueprint $table) { $table->bigInteger('id_estado'); $table->bigInteger('id_municipio'); $table->bigInteger('id_parroquia'); $table->bigInteger('id'); $table->string('nombre'); $table->longText('direccion'); $table

Laravel model accessor fetching from cache - performance enhancements

不想你离开。 提交于 2020-01-13 09:54:12
问题 I have a list of items in database and each item has option to be voted down or up. Those votes are stored in MySql along with other item fields. For example someting like this: Schema::create('items', function ($table) { $table->increments('id'); $table->text('message'); $table->integer('up_votes')->unsigned()->default(0); $table->integer('down_votes')->unsigned()->default(0); $table->timestamps(); }); User can vote down/up every day. When user decides to vote I store his decision to

Laravel model accessor fetching from cache - performance enhancements

荒凉一梦 提交于 2020-01-13 09:54:09
问题 I have a list of items in database and each item has option to be voted down or up. Those votes are stored in MySql along with other item fields. For example someting like this: Schema::create('items', function ($table) { $table->increments('id'); $table->text('message'); $table->integer('up_votes')->unsigned()->default(0); $table->integer('down_votes')->unsigned()->default(0); $table->timestamps(); }); User can vote down/up every day. When user decides to vote I store his decision to

Laravel model accessor fetching from cache - performance enhancements

一曲冷凌霜 提交于 2020-01-13 09:54:08
问题 I have a list of items in database and each item has option to be voted down or up. Those votes are stored in MySql along with other item fields. For example someting like this: Schema::create('items', function ($table) { $table->increments('id'); $table->text('message'); $table->integer('up_votes')->unsigned()->default(0); $table->integer('down_votes')->unsigned()->default(0); $table->timestamps(); }); User can vote down/up every day. When user decides to vote I store his decision to

How to count eloquent relationship without the N+1 issue and loading the entire model?

回眸只為那壹抹淺笑 提交于 2020-01-13 07:16:15
问题 I am showing a list of categories, and the article count within each category. I get the expected result, but I am having the N+1 problem. My CategoriesController index function: public function index() { return View::make('categories.index', [ 'articleCategories' => Category::where('type', 'articles')->orderBy('name')->get(), ]); } The Category model has many relationship to articles: public function articles() { return $this->hasMany('Article'); } My categories.index view: @foreach(

Conversation, Many-to-Many relationship

岁酱吖の 提交于 2020-01-13 03:35:25
问题 I am developing an application where I need users to interact with each other using a chat-like system. To this purpose, I want to create a Conversation model. As far as I've been able to read, I am going to use a Many-to-Many relationship. Having the following models: Conversation , User and Message , I imagined the following tables: conversations: id | user1_id | user2_id - I am not sure if Laravel would understand the user IDs being numbered messages: id | message | conversation_id | user

How to set Laravel Carbon timezone for timestamps?

对着背影说爱祢 提交于 2020-01-12 14:13:49
问题 I have a project which is primarily based in CET region. I set CET in config/app.php, but all pivot timestamps in the base are stored in UTC time? How can I set "global" timezone for timestamps? i made this test: <?php $timezone = date_default_timezone_get(); echo "The current server timezone is: " . $timezone; echo "<br />".date('m/d/Y h:i:s a', time()); $mytime = Carbon\Carbon::now(); echo "<br />".$mytime->toDateTimeString(); ?> and here's the result: The current server timezone is: CET 06

Laravel Eloquent with()-> returning null

拟墨画扇 提交于 2020-01-12 07:20:11
问题 I am trying to use Eloquent to get a specific product that has a brand_id column that maps to a brands table, the brand array is coming back empty. Is there anything obvious here that needs to be changed? $product = Product::with('images')->with('brand')->select($fields)->where('display', '=', 1)->find($id); //Product model class Product extends Eloquent { ... public function brand() { return $this->belongsTo('Brand'); } //Brand model class Brand extends Eloquent { ... public function

Relationship HasManyThrough with Many to Many relationship

六眼飞鱼酱① 提交于 2020-01-11 14:40:28
问题 Each Article belongs to many Categories. Each User has many Categories. I want to retrieve all the Articles which has the User’s Categories. For each of these relationships below I have my own pivot tables. article_cat id | articleID | categoryId users_cats id | user_details_id | categories_id Relationships. Articles.php public function categories() { return $this->belongsToMany('App\Categories', 'article_categories', 'articleID', 'categoryID'); } UserDetails.php public function Categories(){

Laravel Nested Select + MySQL

自作多情 提交于 2020-01-11 13:44:06
问题 How can i do a nested select using Laravel raw query? SELECT day_of_week, AVG(order_count) average_order FROM ( SELECT DAYNAME(order_date) day_of_week, DAYOFWEEK(order_date) day_num, TO_DAYS(order_date) date, count(*) order_count FROM orders GROUP BY date ) temp GROUP BY day_of_week ORDER BY day_num This is what I tried so far: DB::table('( SELECT DAYNAME(order_date) day_of_week, DAYOFWEEK(order_date) day_num, TO_DAYS(order_date) date, count(*) order_count FROM orders GROUP BY date ) temp') -