All Questions
1,528
questions
114
votes
9
answers
251k
views
Eloquent ORM laravel 5 Get Array of ids
I'm using Eloquent ORM laravel 5.1, and I want to return an array of ids greater than 0, my model is called test.
I have tried :
$test=test::select('id')->where('id' ,'>' ,0)->get()->...
67
votes
9
answers
91k
views
How I can put composite keys in models in Laravel 5?
I have in my DataBase a table with two primary keys (id and language_id) and I need put it in my models. The default primaryKey in Models (Model.php in Laravel 5) is id, and I want that the ...
61
votes
14
answers
72k
views
PHPStorm is not recognizing methods of my Model class in Laravel 5.0 [duplicate]
failed insert data into database, and all query class and Model class's method not found show in IDE (phpStrom) how can I solve it?
here is my extended class (Post.php) here show error in latest and ...
59
votes
5
answers
92k
views
How to set a default attribute value for a Laravel / Eloquent model?
If I try declaring a property, like this:
public $quantity = 9;
...it doesn't work, because it is not considered an "attribute", but merely a property of the model class. Not only this, but also I ...
57
votes
7
answers
185k
views
Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model
I have very basic problem. In L4 thes below methods worked out of the box, so now I am lost. Please help. A few days ago I started a Laravel 5.0 project. I have now fresh, clean installation.
Problem ...
48
votes
3
answers
111k
views
Create Models from database in Laravel
Is there some way to generate models from database in Laravel?
The generators package only create an empty model.
47
votes
4
answers
107k
views
Check if laravel model got saved or query got executed
I've seen alot of people using this way to check if a laravel model got saved. So now I wonder if it is a safe way.
And also can I check if the queries bellow got executed like this
Check if model ...
43
votes
1
answer
33k
views
Instance the query builder directly from model
When I do something like SomeModel::with('user') it returns a Query\Builder instance. How can I get this instance without need call the with() (or similar)?
For instance, I tried it: new SomeModel, ...
37
votes
6
answers
97k
views
Get Laravel Models with All Attributes
Is there a way to retrieve a model in Laravel with all the attributes, even if they're null? It seems to only return a model with the attributes that aren't null.
The reason for this is that I have a ...
36
votes
7
answers
49k
views
In Laravel, how do I retrieve a random user_id from the Users table for Model Factory seeding data generation?
Currently, in my ModelFactory.php, I have:
$factory->define(App\Reply::class, function (Faker\Generator $faker) {
return [
'thread_id' => 1,
'user_id' => 1,
'body' => $faker-...
36
votes
5
answers
114k
views
Update Table Using Laravel Model
I've got a table for a sports team. The record shows the team selection and some other information. I want to update the record with the team selection. My model is thus:
class Selection extends ...
33
votes
6
answers
49k
views
Laravel 5 Models Folder
I have a directory called models inside app/ where I place all my model classes. Now I want to change the directory where the following command outputs the generated classes
php artisan make:model ...
31
votes
5
answers
111k
views
Laravel how to add a custom function in an Eloquent model?
I have a Product model
class Product extends Model
{
...
public function prices()
{
return $this->hasMany('App\Price');
}
...
}
I want to add a function which will ...
31
votes
6
answers
22k
views
Laravel models: Where are model properties?
(I come from Visual Studio + Entity Framework background and trying to locate equivalent functionality in Laravel + Eloquent)
In EF and Visual Studio, we add a new Model to our application and just ...
27
votes
4
answers
27k
views
Model Inheritance in Laravel
i'm currently working with Laravel and I'm struggling with the fact that every model needs to extend from Eloquent and I'm not sure how to implement a model Hierarchy (with different tables)
For ...
26
votes
4
answers
18k
views
What are the Laravel naming conventions for controllers/models/views?
I remember hearing you should name your controllers, models and views in a special way. Either singular or plural. I don't remember which ones to name what though, and i can't find anything about it ...
21
votes
3
answers
16k
views
Laravel: Whenever I return a model always return a relationship with it
I have 2 tables:
User | Doctor
---------- | ----------
id | id
email | user_id
name | signature
last_name | photo
password | description
date_birth |
...
21
votes
4
answers
26k
views
Laravel Generate Migration from existing Model
How can I create migration file from existing model in Laravel or model from existing migration?
18
votes
3
answers
15k
views
Laravel ClassLoader trying to load an old version of my model
So a while back I decided to redo one of my models in Laravel completely, but I wanted to hold on to the original copy of the file just in case. So I renamed it to something like MyModel (OLD).php and ...
18
votes
1
answer
73k
views
laravel: eloquent example insert data to database
I am sorry to ask this but I come from codeIgniter and have a really hard time understanding eloquent model insertion. This is a new way of working with models for me.
i have read this article and ...
18
votes
4
answers
13k
views
Laravel: Does every table need a model?
I am building an occasion system with Laravel. I have several tables like:
occasions
occasion_categories (cars, trikes and other)
Now i have a model called Occasion which represents the table ...
17
votes
5
answers
28k
views
Check user's age with laravel validation rules
How can I check the age of a user upon registration? I want to set the minimum age to be 13 years old. I ask for the user's date of birth and when I validate the other credentials, I want to check ...
16
votes
3
answers
3k
views
Why laravel model duplicates set of data and how (if possible) to have only one set of data?
It is convenient that laravel model provides a method that it can return results from another associated table.
For example, I have a table called item and another table called feedback, where the ...
15
votes
4
answers
36k
views
How to get fillable variable as array in laravel 4.2
When we create any Model class we create protected variable fillable
I use this like
$model = new Demo;
$model->fillable // but now working
$model->fillable() // but now working.
Actually i ...
15
votes
1
answer
25k
views
Extending Eloquent Models in Laravel (use different tables)
I’m building a Laravel application that involves tracking different types of leads. For example, there are Refinance leads and Purchase leads.
Since the leads share a lot of information and ...
14
votes
2
answers
5k
views
laravel create model from custom stub when using php artisan
When I use php artisan make:model CustomNamespace\TestModel, I get a model based on default stub as this :
namespace App\Models\CustomNamespace;
use Illuminate\Database\Eloquent\Model;
class ...
14
votes
1
answer
16k
views
Laravel what is use HasFactory?
I created a model in Laravel. I always noticed the default would be use HasFactory. May I know what exactly does it work. In my understanding from reading documentation, it is for linking to database (...
13
votes
2
answers
23k
views
Laravel eloquent: Update A Model And its Relationships
With an eloquent model you can update data simply by calling
$model->update( $data );
But unfortunately this does not update the relationships.
If you want to update the relationships too you ...
13
votes
2
answers
7k
views
The difference between an Eloquent Model and a Model?
So, i'm confused with this :
In Laravel's official documentation, they say :
The Eloquent ORM included with Laravel provides a beautiful, simple
ActiveRecord implementation for working with your ...
13
votes
3
answers
3k
views
Laravel saving ordered list of eloquent models
I'm creating a food menu which the administrator can order/sort by dragging and dropping. This menu consists of multiple categories (ProductCategory) and products (Product).
I'm using HTML5Sortable on ...
12
votes
1
answer
6k
views
Exact place to register observer in Laravel 4
When using a separate class for a model observer, where exactly should I register the observer? The documentation says to call User::observe(new UserObserver); but I’m not sure where the best place to ...
11
votes
4
answers
82k
views
Laravel get model attribute: difference between two methods
I found out that there are two ways to get/display a model's attribute with Laravel. I could create a function in User.php for example:
public function getUsername() {
return $this->username;
}...
11
votes
3
answers
32k
views
How to use Request->all() with Eloquent models
I have a lumen application where I need to store incoming JSON Request. If I write a code like this:
public function store(Request $request)
{
if ($request->isJson())
{
$data = $...
10
votes
3
answers
11k
views
Laravel Relationship availability after save()
I'm building a simple timetracking App using Laravel 5 where I have two Laravel models, one called "User" and one called "Week"
The relationships are pretty simple:
Week.php:
public function user()
{...
10
votes
3
answers
4k
views
Laravel Auth::user() has no code completion for User model functions
I am using Laravel and PHPStorm.
The auto completion works for all models, just not for the 'User', when I call it with Auth::user().
When I call the Auth::user() I get the right user object, but the ...
10
votes
3
answers
5k
views
Route Model Binding and Soft Deletes - Laravel 4
When using soft deletes and route to model binding their arises a circumstance when you cannot view the injected model if it has been "soft deleted".
e.g.
I have a Job model. if i "trash" one of ...
10
votes
2
answers
12k
views
what is the difference between 'extends Authenticatable' vs 'extends Model' in Laravel?
I have a user model class that has class User extends Authenticatable and another model class that I also created that has class Foo extends Model
it's causing some issues in displaying data from the ...
9
votes
3
answers
16k
views
Laravel 4 - Trouble overriding model's save method
I'm trying to override my Post class's save() method so that I can validate some of the fields that will be saved to the record:
// User.php
<?php
class Post extends Eloquent
{
public ...
9
votes
2
answers
22k
views
Laravel Argument 1 passed to HasOneOrMany::save() must be an instance of Model array given
I'm new at Laravel and I have a problem when I try to save array of data to database. Here's the error I get
Argument 1 passed to
Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must ...
9
votes
2
answers
7k
views
Laravel model return Undefined property::$attribute
I have model Post with attributes: id, content, image, publish_date.
In the Post model I can get all attributes but image when using $this->$attr it shows me Undefined property when calling $this->...
9
votes
1
answer
4k
views
laravel do I need a Model and Controller for every single table?
I know this is a duplicate question but i think it will help others because there are a lot of similar apps that have these kind of table relationships:
So the question is what would be the optimal ...
9
votes
2
answers
19k
views
Laravel: Hide attributes from model just in some routes
how do I hide some attributes from the model in just some routes, for example:
I'm using protected $hidden to hide elements but this hide in all my functions or restful routes (index, show)
$hidden ...
8
votes
3
answers
24k
views
Laravel: Create an API Controller, model and migration in one line
This is what I use at the moment to create Controller and Model
php artisan make:controller API/name_of_controller --api --model=name_of_model
then create a migration
php artisan make:migration ...
8
votes
2
answers
23k
views
Model and table name conventions Laravel
I want to know model and table conventions in Laravel.
I have table users and user_address now I do not want to use
protected $table = "user_address"
property. Please suggest me the right ...
7
votes
4
answers
28k
views
Laravel Excel , exporting from a model, styling issues
I am trying to export some data from my model to excel scheet with "Laravel excel", I have done it, but my result is far away from what I really need
That is what I`ve got in downloaded file:
And ...
7
votes
3
answers
43k
views
Laravel get users by id in user model and store them in an array
I'm trying to make a simple "friends" system, by storing each friend id in a single column as a string separated with a comma.
I want to get the users by id and store them in an array:
public ...
7
votes
2
answers
4k
views
Laravel Global Settings Model
I'm working on building a small CRUD app in Laravel 5. I have a few app wide settings such as "SiteTitle" and "BaseURL" I'd like to give my admins the ability to change from the UI. My whole app uses ...
7
votes
6
answers
4k
views
How to determine if a model uses soft deletes in Laravel 4.2
How do I determine if a model uses soft deletes in Laravel?
In the Laravel API I found the function isSoftDeleting(), but apparently that was removed from Laravel 4.2 now that it uses the ...
7
votes
1
answer
9k
views
Laravel Eloquent Model inheritance
I'm a new bit who is trying to build an app using Laravel 5.5, and the Eloquent model.
I have two classes: (1)
Customer
and (2)
VIPCustomer which extends Customer.
You may immediately tell ...
7
votes
1
answer
3k
views
Laravel use custom USER-Model in package
I´ve developed a package stored in LaravelRoot/packages/myname/project/ in Laravel.
Inside my package i´ll have to use an extended user-model containing a relationship not mentioned in the default ...