eloquent

Create new eloquent instance with relationships

扶醉桌前 提交于 2020-01-15 03:40:14
问题 How do I create a new instance of an eloquent model with relationships. This is what I am trying: $user = new User(); $user->name = 'Test Name'; $user->friends()->attach(1); $user->save(); But I get Call to undefined method Illuminate\Database\Query\Builder::attach() 回答1: Try to attach friend after save since the attach() method needs an ID to exist on the parent model. ID's aren't (usually) generated until model is saved (when a primary key or other identifier for that model is created in

Does Eloquent ORM(laravel 5) take care of SQL injection?

北慕城南 提交于 2020-01-15 02:40:07
问题 I couldn't find it online, but does Eloquent ORM take care of SQL injection like PDO prepared statements do? 回答1: As per your question all the eloquent queries are taken care of for SQL injection, because they use the PDO driver in core. So you don't have to worry, but the input are stored as they are so you might want to sanitize as per your application's needs (HTML formatting, etc.) 回答2: No framework "takes care of" SQL injection. You take care of SQL injection. A framework may provide

Unset/Remove relation object from Laravel Eloquent collection

可紊 提交于 2020-01-14 23:27:27
问题 I've a fetch a Laravel Eloquent collection by: $product = Product::query()->with(['merchant', 'picture'])->where('id', $id)->first(); and get the dump of $product is Product { #casts: ... #dates: ... #connection: "mysql" #table: null #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:1 [ "id" => 27 ] #original: ... #changes: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables:

Laravel4 Model primary key value is lost after insert

ぐ巨炮叔叔 提交于 2020-01-14 22:32:13
问题 <?php // Model class ProfileDelivery extends \Eloquent { protected $table = 'profile_delivery'; protected $guarded = array(); public $timestamps = FALSE; } // Somewhere $deliveryGuy->id = 1; print $deliveryGuy->id; // Prints 1 if (!$deliveryGuy->save()) { throw new \Exception('Cant save .'); } print $deliveryGuy->id; // Prints 0 Can anyone explain me why the ID value is lost? 回答1: Not sure if you solved this for your situation but in Laravel 5.1 this just happened to me - the primary key of

Laravel4 Model primary key value is lost after insert

有些话、适合烂在心里 提交于 2020-01-14 22:31:57
问题 <?php // Model class ProfileDelivery extends \Eloquent { protected $table = 'profile_delivery'; protected $guarded = array(); public $timestamps = FALSE; } // Somewhere $deliveryGuy->id = 1; print $deliveryGuy->id; // Prints 1 if (!$deliveryGuy->save()) { throw new \Exception('Cant save .'); } print $deliveryGuy->id; // Prints 0 Can anyone explain me why the ID value is lost? 回答1: Not sure if you solved this for your situation but in Laravel 5.1 this just happened to me - the primary key of

Laravel4 Model primary key value is lost after insert

╄→尐↘猪︶ㄣ 提交于 2020-01-14 22:31:18
问题 <?php // Model class ProfileDelivery extends \Eloquent { protected $table = 'profile_delivery'; protected $guarded = array(); public $timestamps = FALSE; } // Somewhere $deliveryGuy->id = 1; print $deliveryGuy->id; // Prints 1 if (!$deliveryGuy->save()) { throw new \Exception('Cant save .'); } print $deliveryGuy->id; // Prints 0 Can anyone explain me why the ID value is lost? 回答1: Not sure if you solved this for your situation but in Laravel 5.1 this just happened to me - the primary key of

What are all those SQL operators in Laravel?

拈花ヽ惹草 提交于 2020-01-14 09:44:08
问题 I was looking through Laravel's source code and I've found a lot of SQL operators for Eloquent and I was wondering what are some of them and how can they be used. I haven't managed to find any documentation unfortunately. Here's the operators I've found in vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php : protected $operators = [ '=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'like binary', 'not like', 'between', 'ilike', '&', '|', '^', '<<', '>>', 'rlike', 'regexp', 'not

Inheritance in Laravel framework

回眸只為那壹抹淺笑 提交于 2020-01-14 06:47:05
问题 Is possibile to use inheritance in laravel model? I explain: is possible to exend a model, which extends eloquent class? class A extends Eloquent { } class B extends A { } A e B are also 2 different tables and B has A_id as foreignkey and other fields. How could be the constructor of class B? is it a ragionable solution or better use hasOne relationship? no every A object are also B object. Es. user and teacher Thank you 回答1: I’m having difficulty understanding your supplementary details, but

Pluck Eager Loading Laravel 5.4

一世执手 提交于 2020-01-14 05:56:15
问题 How would I pluck an eager loading content while querying with Laravel 5.4? I tried this way: $something = Something::with(array('something_else' => function($query){ $query->pluck('field'); }))->first(); And $query->select('field') too, but without luck. Is this possible in Laravel 5.4? 回答1: You wouldn't be able to use pluck on the query but you can use select if you want to limit the fields returned with eager loading. You just need to make sure you include the id so that Eloquent can match

Eloquent WHERE LIKE clause with multiple columns

一笑奈何 提交于 2020-01-14 04:55:40
问题 I'm implementing a search bar that can search customers by first name, last name, or both. So, for example, Mike Hizer would be matched by Mike , Hizer , zer , Mike Hizer , etc. Here's what I came up with: Customer::where( DB::raw('concat(first_name," ",last_name)'), 'like', "%{$request->search}%" )->get() It works. But is there a more Eloquent approach to combine these two columns ( first_name and last_name ) without resorting to the DB facade? Would something like ->where(['first_name',