Is there a way to merge 2 relationships in laravel? this is the way it’s setup now, but Is there a way I could return both merged? public function CompetitionsHome() { return $this->HasMany(…
Tag: eloquent
Laravel Eloquent – Attach vs Sync
What is the difference between attach() and sync() in Laravel 4’s Eloquent ORM? I’ve tried to look around but couldn’t find anything! Answer attach(): Insert related models when working with many-to-many relations No array parameter is expected Example: sync(): Similar to the attach() method, the sync() method is used to attach related models. However, the main differences are: sync() accepts
How to specify Books and Authors relationship in Eloquent insertion?
I was figuring out how to insert a hasMany entry using Laravel 4 Author.php module Book.php Module My controller I’m getting one null record on authors table which pointed to the book and other 3 record that I inserted here without any pointing to the book. Answer First off I believe this is wrong schema, since probably one author could
Laravel Check If Related Model Exists
I have an Eloquent model which has a related model: When I create the model, it does not necessarily have a related model. When I update it, I might add an option, or not. So I need to check if the related model exists, to either update it, or create it, respectively: Where <related_model_exists> is the code I am looking
How to reload/refresh model from database in Laravel?
In some of my tests, I have a user model I have created and I run some methods that need to save certain attributes. In rails, I would typically call something like user.reload which would repopulate the attributes from the database. Is there a way in laravel to do that? I read through the api and couldn’t find a method
How do I get all of the results from a hasMany() relationship in Laravel?
For example, I have a Product, and I have a BaseProduct. In the model for the Product, I’ve specified the following: In the BaseProduct, I’ve specified the following relationship: If I were to select a product, like so: I could get the BaseProduct by doing the following: Instead of getting the array of the result from that, how would I
Laravel Eloquent inner join with multiple conditions
I have a question about inner joins with multiple on values. I did build my code like this in laravel. public function scopeShops($query) { return $query->join(‘kg_shops’, function($join) {…
Laravel Eloquent setting a default value for a model relation?
I have two models: This works fine, I can call $product->defaultPhoto->thumb and $product->defaultPhoto->full and get the path to the related image, and get all photos using $product->photos and looping through the values. The problem arises when the product does not have a photo, I can’t seem to figure out a way to set a default value for such a scenario.
How do I get the query builder to output its raw SQL query as a string?
Given the following code: I want to get the raw SQL query string that the database query builder above will generate. In this example, it would be SELECT * FROM users. How do I do this? Answer To output to the screen the last queries ran you can use this: I believe the most recent queries will be at the
Get previous attribute value in Eloquent model event
Is there a way to see the old/previous value of a model’s attribute in its saving or updating event? eg. Is something like the following possible: Answer Ok, I found this quite by chance, as it’s not in the documentation at present… There is a getOriginal() method available which returns an array of the original attribute values: Be careful, prior