eloquent

Convert DATE_FORMAT to Y-m-d? Using Larave Eloquent Raw Select

徘徊边缘 提交于 2021-02-11 12:29:00
问题 Currently I have this eloquent select in laravel $queryBooking = Booking::where('id',$id)->select('starts_at','ends_at')->first(); Now I want to put specific format to starts_at and ends_at using this format Y-m-d Data should be shown somethings like this 2020-05-29 (Y-m-d) How can I do that using laravel eloquent? UPDATE tried the answer of spiny did some modification. $queryBooking = Booking::where('id',$id)->select('starts_at','ends_at')->first(); $booking = PropertyBooking::where(

Eloquent Count nested relationships

萝らか妹 提交于 2021-02-11 12:20:30
问题 | Data | DataChildren | Category ---------------------------------- | id | id | id | name | data_id | name | | category_id | | | name | Data Model: public function data_children() { return $this->hasMany(DataChildren::class, 'data_id', 'id'); } DataChildren Model: public function category() { return $this->belongsTo(Category::class, 'category_id', 'id'); } I want to get count of Category based on Data id through DataChildren . I just want to take the Category records from Data so the result

Getting an error when associating role to a user for one to many relationship

有些话、适合烂在心里 提交于 2021-02-10 16:17:34
问题 I have a one to many relationship between users and roles . I am unable to associate a role to a user while saving or updating a user, as its throwing me an error while saving the model. Please spare some time to have a look below. SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '[3]' for column management . users . role_id at row 1 (SQL: update users set role_id = [3], users . updated_at = 2021-01-29 04:44:19 where id = 3) Database Structure Users ---------- * id *

Getting an error when associating role to a user for one to many relationship

帅比萌擦擦* 提交于 2021-02-10 16:14:55
问题 I have a one to many relationship between users and roles . I am unable to associate a role to a user while saving or updating a user, as its throwing me an error while saving the model. Please spare some time to have a look below. SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '[3]' for column management . users . role_id at row 1 (SQL: update users set role_id = [3], users . updated_at = 2021-01-29 04:44:19 where id = 3) Database Structure Users ---------- * id *

Laravel Show Resource searching wrong column [closed]

醉酒当歌 提交于 2021-02-10 14:27:52
问题 Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 1 year ago . Improve this question I have created a model and controller for my spaces (properties) which has the database structure of; space_id , space_address , space_owner , space_price etc... However when I visit localhost:8000/project/space/1 I am getting this error: Column not found: 1054

Laravel PIVOT table with extra fields and withCount('table') not working as expected

廉价感情. 提交于 2021-02-10 14:14:53
问题 I've the following structure in my database. users -- id ... products -- id ... licenses (pivot table with some extra informations these data are grabbed via API) -- user_id product_id license_key license_type url purchased_at (datetime) supported_until (datetime) ... And here are codes in my model: # User has many licenses # User has many products through licenses User // User has many licenses. public function licenses() { return $this->hasMany(License::class); } Product Model. # Product

Applying string function in column Laravel 5.4

∥☆過路亽.° 提交于 2021-02-10 06:29:46
问题 I'm using the latest Laravel 5.4 I am trying to make a simple query to search users by name. The query written for MySQL looks like this: SELECT * FROM users WHERE upper(name) LIKE '%FOO%'; I'm trying to make it work with Eloquent. Things I've tried but failed: User::where('upper(name)', 'LIKE', '%FOO%')->get() DB::table('users')->where('upper(name)', 'LIKE', '%FOO%')->get() Both fail with the following error: Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found:

How to return one instance of a eloquent many to many relation with eager loading?

老子叫甜甜 提交于 2021-02-10 03:54:48
问题 class Parent extends Model { public function kids() { return $this->belongsToMany('App\Models\Kid') ->orderBy('age') ->withTimestamps(); } public function oldestKid() { return $this->belongsToMany('App\Models\Kid') ->orderByDesc('age') ->take(1); } } Problem with this approach is that $parent->oldestKid returns an array. It would feel more logical if it would return an object. $parent = App\Models\Parent::with('oldestKid')->first(); 回答1: This is what we ended up with, and its working.

Method orWhere does not exist. Laravel 5.3

Deadly 提交于 2021-02-08 13:16:17
问题 BadMethodCallException in Macroable.php line 74: Method orWhere does not exist. $category = $categories->where('Node_ID', (explode('.', $cat{$title_id})[0])) ->orWhere('Node_Path', $cat->{$category_name}) ->first(); If I try without "orWhere" works, if I use it, throws an Error. Someone knows where is the mistake? 回答1: You are trying to use orWhere on collections, thats why its showing you the error. You should use this on model like this (taking Category as a Model): $category = Category:

Laravel query multiple tables using eloquent

你说的曾经没有我的故事 提交于 2021-02-08 12:20:33
问题 hi sorry bit of a newbie here but I am have three tables users, profiles, friends. they all have the user_id fields within them and I want fetch all of the fields in one statement using Eloquent and not DB::statement and doing the table joins. How can I achieve this? 回答1: Try this use the User class and the with method that laravel has to query model relationships $user = User::with(['profile', 'friend'])->get(); Ensure your models has the correct relationships as follows: app/models/User.php