eloquent

Joining Model with Table Laravel 5.2

我是研究僧i 提交于 2019-12-24 00:58:14
问题 I have created a model in Laravel. class deals extends Model { // protected $table = 'tbl_deal'; } Now I want to use this model to retrieve data in my Controller. However, the data extracted from this model also needs to be constrained by a column in another table. Below is the basic schema tbl_deal deal_id merchant_id deal_text tbl_merchant merchant_id merchant_url_text My current Controller has the following code to extract all deals from deals model. $deals = deals::all(); I want to

Laravel - Really struggling to understand eloquent

故事扮演 提交于 2019-12-24 00:42:45
问题 I'm fairly new to Laravel having come over from Codeigniter and for the most part I really like it, but I really can't get my head around Eloquent. If I want to do a simple query like this: SELECT * FROM site INNER JOIN tweeter ON tweeter.id = site.tweeter_id I try doing something like this (with a "belongs to"): $site = Site::with('tweeter')->find($site_id); But now I have two queries and an IN() which isn't really needed, like so: SELECT * FROM `site` WHERE `id` = '12' LIMIT 1 SELECT * FROM

Eloquent paginate function in Slim 3 project using twig

十年热恋 提交于 2019-12-24 00:20:13
问题 How can I use paginate function from Eloquent in Slim 3 project using twig ? This is in my controller : $posts = Sound::paginate(2); $this->container->view->render($response, 'admin/sounds/index.twig', [ 'posts' => $posts ]); This is the view : {{ posts.links() }} But it doesn't work as well as I expected : Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in **PATH_TO_PROJECT**\vendor\illuminate\pagination\AbstractPaginator.php on line 412 Fatal

Using relationship table's ID in other tables

守給你的承諾、 提交于 2019-12-24 00:05:55
问题 I have 2 entities which are related using a relationship table (ManyToMany) And then I have a pricing table which has pricing information for a service of a hospital. This table can have different pricing for the same service if the hospital linked to the service is different. hospitals: id name services: id name hospital_service: hospital_id service_id service_pricing: id hospital_id service_id weight_range_id LH_price SH_price service_duration: id hospital_id service_id weight_range_id LH

Laravel Eloquent - Do not run relationship query if column value is NULL or 0

北慕城南 提交于 2019-12-23 22:08:09
问题 I have various relationships within my Eloquent Models that look like this: public function main_image() { return $this->hasOne(Media::class, 'id', 'main_image_id'); } However, it will run a SQL query if the main_image_id is null or 0, so I end up with a number of queries like this: select * from `media` where `media`.`id` is null and `media`.`id` is not null limit 1 Which obviously will not return anything, but still wastes resources. Is there any way of automatically checking for that?

Laravel 5.1 Updated Model Relationships when Eagerly Loaded Don't Persist to Database

自古美人都是妖i 提交于 2019-12-23 22:05:48
问题 I'm updating a Rental that belongsTo a Location, and I can fill and persist the Rental data, but when I fill the location relationship data retrieved through eager loading if I try to save(), update(), or push(); It isn't persisted to the database. According to the docs push() should work as it apparently persists relationships. Update Action public function update($id, RentalUpdateRequest $request) { // Eager load the rental with relationships $Rental = Rental::with('location')->find($id); /

Get latest from different relationship eloquent laravel

眉间皱痕 提交于 2019-12-23 21:16:59
问题 I have 3 Tables Model Url class Url extends Model { public function users(){ return $this->belongsToMany(User::class); } public function url_status(){ return $this->hasMany(UrlStatus::class); } } Model UrlStatus class UrlStatus extends Model { public function url() { return $this->belongsTo(Url::class); } } Model User class User extends Authenticatable { use Notifiable, SoftDeletes, HasRoles; public function urls(){ return $this->belongsToMany(Url::class); } } In my controller I'm querying :

Eloquent nested WHERE statement

家住魔仙堡 提交于 2019-12-23 19:28:38
问题 For an online competition with the goal of photographing certain objects I've got users uploading pictures of objects , where the objects have a category and a subcategory . A user's dashboard should look something like this: ------------------------------------------------ | Category | ------------------------------------------------ | ----------------------------------------- | | | Subcat. | Subcat. | | | |------------------|--------------------| | | |Object1 | Object2 | Object1 | Object2 |

Laravel 5.1 use limit in with() method of eloquent

怎甘沉沦 提交于 2019-12-23 18:55:03
问题 Eloquent $staffGroup = StaffGroup::where('id', $id) ->with('staffGroupRight') ->first(); In StaffGroup Model : public function staffGroupRight() { return $this->hasMany('Modules\Staff\Http\Models\StaffGroupRight'); } what i have does is, public function staffGroupRight() { return $this->hasMany('Modules\Staff\Http\Models\StaffGroupRight')->take(5); } but it gives total 5 rows for all staff_group but i want it to limit for one staff_group For example There are 10 staff_group then it gives 5

Eloquent update method change created_at timestamp

核能气质少年 提交于 2019-12-23 16:09:49
问题 I'm using Eloquent for database handling, when I update a record with update method created_at time stamp is changed how to prevent this from happening ? $post = Post::update([ 'name' => $name ]); updated_at timestamp is working perfectly 回答1: In case you don't have any custom settings in your model. You can update record like this: $id = 5; // here put id of record you want to update $post = Post::findOrFail($id); $post->name = $name; $post->save(); Using this code only name should be