eloquent

Lumen error when using artisan migrate

我怕爱的太早我们不能终老 提交于 2020-01-02 05:50:28
问题 I'm new to Lumen and when I try to use: php artisan migrate I get the following error. [PDOException] SQLSTATE[HY000] [2002] No such file or directory If I change 'localhost' to '127.0.0.1' I get this error: [PDOException] SQLSTATE[HY000] [2002] Connection refused If I use: php artisan migrate --database=Lumen I get: [InvalidArgumentException] Database [Lumen] not configured. Here is my .env file DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=Lumen DB_USERNAME=root DB_PASSWORD

Set default database DateFormat to ISO 8601 with timezones in Laravel

若如初见. 提交于 2020-01-02 05:07:06
问题 I think the title says it: I want to use ISO 8601 with timezones as default DateTime-Format in Laravels Eloquent. I got this class mEloquent extends Eloquent { protected function getDateFormat() { return 'YYYY-MM-DDThh:mm:ss.sTZD'; } } All my models will extend mEloquent. But how should I build the tables? Just <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */

Method addEagerConstraints does not exist

不问归期 提交于 2020-01-02 04:47:12
问题 I have two models, User and Event. I made a pivot table, invitations between User and Event with a status column. In my Event model definition, I wrote this : public function invited() { return $this->belongsToMany(User::class, 'invitations', 'event_id', 'user_id') ->withTimestamps() ->withPivot('status') ->orderByDesc('invitations.updated_at'); } public function participants() { $event = $this->with([ 'invited' => function ($query) { $query->where('invitations.status', InvitationStatus:

Laravel Eloquent - firstOrCreate() on a relationship

痴心易碎 提交于 2020-01-02 03:47:09
问题 When I try the firstOrCreate() on a relationship of another model, it does not work: Client::find($id)->users()->firstOrCreate(array('email' => $email)); This returns an error saying Call to undefined method Illuminate\Database\Query\Builder::firstOrCreate() Running this directly on the model User will work though. 回答1: This won't work, you have to do it manually/directly using User model because users() should return a collection object This is correct, the users() function does not return a

Querying Relationship Existence using multiple MySQL database connections in Laravel 5.2

不羁岁月 提交于 2020-01-02 03:35:08
问题 I am dealing with the following situation: I have two models, an Employee with id and name fields and a Telephone with id , employee_id and flag fields. There is also an one-to-many relationship between these two models, that is an employee may have many telephones and a telephone may belong to a single employee. class Employee extends Model { public function telephones() { return $this->hasMany(Telephone::class); } } class Telephone extends Model { public function employee() { return $this-

'Where like' clause using the concatenated value of 2 columns with eloquent

不打扰是莪最后的温柔 提交于 2020-01-01 12:30:37
问题 I have a query that searches for a term in several columns and one of them has to be a full name. I have separated the full name in name and lastname, so I have to concat these 2 values when searching. I have this right now only searching for name. How I would add the concat to lastname? I was investigating over mutators but I don't know if it is the way to way. public function search($searchQuery) { $search = "%{$searchQuery}%"; return Order::with('customer') ->orWhereHas('customer',

Laravel 5 issue with wherePivot

。_饼干妹妹 提交于 2020-01-01 11:03:21
问题 I am working with Laravel 5 and I am having issue getting ->wherePivot() to work on a Many-to-Many relationship. When I dd() the SQL it looks like Eloquent is looking for records in the pivot table with a `pose_state`.`pose_id` is null`. I am hoping it is a simple error and not a bug. Any ideas are appreciated. Database Structure pose id name type state id name machine_name pose_state pose_id state_id status Models Pose <?php namespace App; use DB; use App\State; use Illuminate\Database

Laravel 5 issue with wherePivot

杀马特。学长 韩版系。学妹 提交于 2020-01-01 11:03:09
问题 I am working with Laravel 5 and I am having issue getting ->wherePivot() to work on a Many-to-Many relationship. When I dd() the SQL it looks like Eloquent is looking for records in the pivot table with a `pose_state`.`pose_id` is null`. I am hoping it is a simple error and not a bug. Any ideas are appreciated. Database Structure pose id name type state id name machine_name pose_state pose_id state_id status Models Pose <?php namespace App; use DB; use App\State; use Illuminate\Database

Laravel - Query Model if values contain a certain string (taken from search input)

瘦欲@ 提交于 2020-01-01 10:47:13
问题 I am implementing a search using Laravel and Ajax. So I have a Product which belongs to a Tag and a Subcategory. On the other hand the Subcategory belongs to a Category. I want to check all of their properties (field values) and check if they contain the given string. With some searching I found out that I have to use LIKE . Here is what I tried: $products = Product::where('name_en', 'LIKE', $search)->get(); However this will get the products if the search string matches exactly the value. I

How do I get a “select count(*) group by” using laravel eloquent

让人想犯罪 __ 提交于 2020-01-01 08:33:22
问题 I would like to execute the follow sentence using laravel eloquent SELECT *, count(*) FROM reserves group by day The only solution occurs to me is to create a view in the DB, but I am pretty sure there is a way to do it in laravel way. 回答1: You could use this: $reserves = DB::table('reserves')->selectRaw('*, count(*)')->groupBy('day'); 回答2: As you wish to do it with Laravel Eloquent I assume you have a model name Reserve . In this case you can use this $reserve = Reserve::all()->groupBy('day'