laravel-eloquent

Laravel eloquent query on a relational table

对着背影说爱祢 提交于 2019-12-24 22:31:55
问题 i am fairly new to laravel eloquent and i am trying to write a nested query to get data of food and food of resturent I have two table one is 'food_list' which mainly contain 'title','type' Type is int 0/1, 0 means veg and 1 means non veg Another table i have resturent _food Which has restaurant_id, food_id,price, image Now i want to get food list of tge resturent which is only veg So i wrote a query like this RestaurantFood:with(['foodDetail'=>function($q){ $q->where('type',0);}])->where('id

Need some help to convert SQL query to Laravel eloquent

大兔子大兔子 提交于 2019-12-24 12:12:03
问题 I have this SQL query: SELECT * FROM books WHERE id IN (SELECT book_id FROM `author_book` WHERE author_book.author_id IN (SELECT id FROM authors WHERE self_name like "%ори%" OR father_name LIKE "%ори%" OR family_name LIKE "%ори%")) It works, but I need convert to Laravel eloquent. I try this: DB::select("SELECT * FROM books WHERE id IN " . "(SELECT book_id FROM `author_book` WHERE author_book.author_id IN " . "(SELECT id FROM authors WHERE self_name like '%" . $attributes['search'] . "%' " .

Access relational data of many to many relationship with multiple database connection with different server

瘦欲@ 提交于 2019-12-24 03:22:06
问题 I'm working with multiple database connection on different servers. i.e, host1 and host2 . My default database connection is host2. My project have two tables. users exists on host1 and tasks exists on host2 . There is a many to many relationship on both tables. Pivot table for this relationship is task_users which exists on host2 . My model files are here. User.php class User extends Authenticatable { protected $connection = 'host1'; public function tasks() { return $this->belongsToMany(Task

Laravel (HasMany) doesnt rectrieve values

戏子无情 提交于 2019-12-20 03:35:11
问题 I have the following models: namespace App; use Illuminate\Database\Eloquent\Model; class forum_category extends Model { // protected $table = 'forum_category'; public function posts() { $this->hasMany('App\forum_post'); } } And namespace App; use Illuminate\Database\Eloquent\Model; class forum_post extends Model { // protected $table = 'forum_post'; public function category() { $this->belongsTo('App\forum_category'); } } in my controller i attempt to get all categories with their posts:

Laravel Eloquent query JSON column with Where In?

别等时光非礼了梦想. 提交于 2019-12-20 02:26:09
问题 I am having trouble querying a json column. Previously my code was $query->whereIn('user', $users); Now i have changed the db type to JSON column and am trying to modify the code for the new version. In RAW MYSQL this works JSON_CONTAINS(user, '"tom","Bill"') But when i put it in eloquent / php it keeps failing or returning noting. Here i am passing in an array of Users, previous using an WhereIn which works in raw SQL $leads->whereRaw("JSON_CONTAINS(user, '"$users"')") Any idea how to make

Distinct most recent data from database

♀尐吖头ヾ 提交于 2019-12-20 02:01:31
问题 I am storing data in my database. The data being stored looks like this id | upload_month | created_at ----------------------------------------- 1 | January | 2017-01-30 13:22:39 ----------------------------------------- 2 | Febuary | 2017-01-30 13:23:42 ----------------------------------------- 3 | January | 2017-01-30 13:25:33 Within my Controller I am trying to retrieve the distinct upload_month, but get the latest inserted version for each. At the moment I am trying $uploadedFile =

Eloquent MYSQL statement : WHERE NOT(A OR B)

三世轮回 提交于 2019-12-13 15:16:06
问题 I`m working on date-range-overlap functionality that can be written in 13 positive conditions to check if the date intervals overlap : https://en.wikipedia.org/wiki/Allen%27s_interval_algebra Or more elegantly in 2 negative conditions: http://baodad.blogspot.nl/2014/06/date-range-overlap.html In MYSQL : WHERE NOT(`last_day` <= '2001-06-01' OR `first_day` >= '2022-12-01'); This would require something like this, which I can`t find in the docs: $query = $query->whereNot(function ($query) use (

How to solve Missing argument 3 for Rinvex\Repository\Repositories\BaseRepository::join()?

醉酒当歌 提交于 2019-12-12 05:37:52
问题 I use this : https://github.com/rinvex/repository My query is like this : $query = $this->store_repository ->join('favorites', function ($join) { $join->on('stores.id', '=', 'favorites.favoritable_id') ->where('favorites.favoritable_type', 'like', 'App\\\Models\\\Store'); }) ->where('stores.status', '=', 1) ->select('stores.id', 'stores.name', 'stores.photo','stores.address'); if($location) $query->where('stores.address', 'like', "%$location%"); if($q) { $query->where('stores.name', 'like', "

Polymorphic relation - morphTo save() not recognizing custom primary key

£可爱£侵袭症+ 提交于 2019-12-12 03:29:05
问题 User model: public function userable() { return $this->morphTo(); } Mentor model: public function user() { return $this->morphOne('App\Models\User', 'userable'); } The student model looks the same as the mentor model. The tables for Students and Mentors contain a custom PK called user_id which references the primary key on the users table. So what I'm trying to do is the following: $user = new User(); $user->first_name = 'Trajce'; $user->last_name = 'Petkoski'; $user->nickname = 'ads'; $user-

Getting locale language at provider class in Laravel

匆匆过客 提交于 2019-12-12 01:29:42
问题 I'm new at Laravel and trying to make the variable of content at the header shared with all views, but the issue is with getting the language which backing with me with null value at the provider (AppServiceProvider) class. Here's my code : public function boot( ) { // $language=App::setLocale($locale); $locale = App::getLocale(); \Session::put('language', 'en'); \Config::get('app.locale'); \Config::get('languages') ; \Session::get('languages', 'en'); $lang = Session::get ('locale');