laravel-eloquent

Laravel Eloquent `take` and `orderBy`

爱⌒轻易说出口 提交于 2020-01-15 08:20:46
问题 When I try to use each of "take" and "orderBy" query, the Model return some records: $this->hasMany("App\User")->take(3) $this->hasMany("App\User")->orderBy("id", "desc") But when I combine them, it return a null array: $this->hasMany("App\User")->take(3)->orderBy("id", "desc") I run the original sql (from toSql() function) and it return 3 records as I expect. What mistake I get? 回答1: You need to change the order, it will be: $this->hasMany("App\User")->orderBy("id", "desc")->take(3) This -

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

How can I update pivot table on laravel?

喜你入骨 提交于 2020-01-10 06:06:12
问题 I use laravel 5.3 I have 3 table : table product, table category and table products_categories table product : id, name, etc table category : id, name, etc table products_categories : id, product_id, category_id In model product, I have method this : public function categories() { return $this->belongsToMany(Category::class, 'products_categories', 'product_id', 'category_id') ->withPivot('id') ->withTimestamps(); } So 1 product have many category My code like this For example $param['category

How can I update pivot table on laravel?

99封情书 提交于 2020-01-10 06:06:06
问题 I use laravel 5.3 I have 3 table : table product, table category and table products_categories table product : id, name, etc table category : id, name, etc table products_categories : id, product_id, category_id In model product, I have method this : public function categories() { return $this->belongsToMany(Category::class, 'products_categories', 'product_id', 'category_id') ->withPivot('id') ->withTimestamps(); } So 1 product have many category My code like this For example $param['category

Show Product Name in Laravel Model Relationship

冷暖自知 提交于 2020-01-06 06:03:43
问题 Can you help me again? I have 3 tables Product Table id product_name Productquantity Table id item_id price Orders id req_id quantity_id quantity amount How can I get the product name in my CartView? I only have this in my code Order Model public function product() { return $this->belongsTO('App\Productquantity', 'item_id', 'id'); } My Controller $dataReqorder = Reqorder::where('req_id','=', $shoppingId)->with('product')->get(); My View @foreach($dataReqorder as $order) <tr class="item{{

laravel wherebetween in relations not working

ⅰ亾dé卋堺 提交于 2020-01-05 07:18:16
问题 I have two tables in mysql, 1. Vehicles and 2.Locations i am using eloquent relations to define relation of the vehicle table to locations. here is how my vehicles modle looks. namespace App; use Illuminate\Database\Eloquent\Model; class VehicleModel extends Model { function locations(){ return $this->hasMany("App\locations", "vehicle_id", "id"); } } i am trying to run a query that can get me locations of specific vehicle between two given times. here is what i am trying to do: App

Laravel / Eloquent WHERE NOT SUBQUERY

混江龙づ霸主 提交于 2020-01-04 01:31:12
问题 I`m having trouble to setup a negative condition like such: WHERE NOT( "last_day" "<=" $first_day OR "first_day" "<=" $last_day) My query builder looks like this atm: $query = $query->where(function ($query) use ($first_day, $last_day) { $query->where('last_day', '<=', $first_day); $query->orWhere('first_day', '<=', $last_day); }); I would like it to be as such : $query = $query->whereNot(function ($query) use ($first_day, $last_day) { $query->where('last_day', '<=', $first_day); $query-

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

How can I make sub query in laravel eloquent?

别等时光非礼了梦想. 提交于 2019-12-31 05:26:10
问题 When I use db raw, it works My query is using db raw like this : $products = DB::select(DB::raw('SELECT * FROM ( SELECT a.*, b.name AS store_name, b.address FROM products a JOIN stores b ON b.id = a.store_id WHERE a.category_id = '.$category_id.' ORDER BY a.total_sold DESC, a.updated_at DESC LIMIT '.$num.' ) AS product GROUP BY store_id')); It works. But I want to change it use laravel eloquent I try like this : $products = Product::where('category_id', '=', $category_id) ->with('store') -

Eloquent wherein with join multiple tables

依然范特西╮ 提交于 2019-12-25 08:48:03
问题 I have 4 tables, items, listings, catitem_item, and item_listing. items and listing is many to many relationship. items and catitems is also many to many relationship. catitems contains list of item's categories. listings is like location where items located. example listing shop A can have item chair and item chair have multiple catitem categories. My goal is to get items which under list of categories such as category 1 AND 2 AND 3 ( $cats ) and with listing information where this item