All Questions
638
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()->...
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 ...
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.
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
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 ...
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 ...
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 |
...
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 ...
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
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
7
answers
15k
views
Update the table name at runtime not working - laravel Eloquent ORM
This is my model:
class Product extends \GlobalModel {
protected $table = 'product';
}
I want to update the table name oops_product instead product at runtime.
I found getTable(); to get the ...
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 ...
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;
}...
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
17k
views
Laravel Dynamic Fillable in Models
Got stuck in a issue with laravel 5.2.
Following is the error during eloquent create operation(post call),
Mass Assignment Exception in Model.php 453: column_name
Following are the prerequisites, ...
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
3
answers
2k
views
How to write a NotMapped entity in Laravel Model?
I am novice in laravel, therefor my question may be odd to someone. Well, my question is how can I write a entity in Laravel Model class which will not be created any field in database after my ...
7
votes
3
answers
3k
views
How can I know if a table column is a foreign key in Laravel through Model?
So as the title says, how can I know if a field of a Model is a foreign key in Laravel ?
Supose I have a FK column called show_type_id and a model named Event and I want to know if there is a function ...
7
votes
2
answers
675
views
Laravel model scope activated_at and deactivated_at check if has 7 day difference
Using a model scope:
public function scopeApproved($query)
{
return $query->where('activated_at', '>=', 'deactivated_at' -7 days);
}
I know that this is invalid syntax but thought it might ...
7
votes
4
answers
22k
views
LARAVEL 5.5 - Insert Data into Database from FORM with Eloquent Model
I have a problem with inserting Data into Database.
All i have done till now is :
Create a Model with Controller and Migration via:
php artisan make:model Cars -mcr
So, now all of my files looks ...
7
votes
1
answer
3k
views
How do I get the current model from custom method in laravel
I'm not sure I am asking the questions correctly, but this is what I am trying to do.
So we can get the current from
$model = Model::find($id)
Then we can get it's relationships like:
$model->...
6
votes
1
answer
15k
views
How to disable updated_at and created_at when saving data in Laravel
I have tried to save data in the model function. When using save() function of the model, I see some error such as below.
Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: ...
6
votes
1
answer
7k
views
Laravel 4 many to many relationship not working, pivot table not found
I'm currently having problems with a n:n relationship with Laravel 4, I'm having an error wiht pivot table which is quering on a table with with both components on singular name. I create a pivot ...
6
votes
3
answers
6k
views
Undefined method File::save() (Laravel model)
I am using Laravel to build a new web project. I am using Eloquent (its ORM) to do all the database related stuff. I have a SQLite database with two tables: 'images' and 'files'. Therefore, I have two ...
6
votes
3
answers
6k
views
Is there a way to access relationship attributes as own in laravel model?
I have 2 tables: users and users_details
-- users has 3 columns [id,username,password]
-- users_details has [id,user_id,name,address]
I have models for each of them with a relation from users to ...
6
votes
2
answers
5k
views
Laravel 5 - Updating model does not update relationships immediately
I ran into an interesting issue today while trying to update a model with a simple hasOne relationship. I was doing the following:
public function update(MyRequest $request, $id)
{
$project = ...
6
votes
1
answer
5k
views
Laravel Eloquent Attribute Casting not work
I have a Laravel Models that return string value instead of integer in my staging server.
I decided to cast attribute's values to get always the integer value.
I report my Model:
<?php
namespace ...
5
votes
2
answers
42k
views
Accessing Eloquent relationship attributes
I have three models all related by one-to-many. Category, Subcategory and Style. I have relationships working both ways - though I seem to have a problem accessing related attributes.
After my ...
5
votes
2
answers
3k
views
Get posts by followers laravel
I want to display a feed page for an authenticated user which shows the latest posts from users that they follow. I have a follow system set up already which has the following:
Tabels:
Posts
users
...
5
votes
2
answers
12k
views
how to use model function from view ? - laravel 5.4
i made a function into model class which is
public function scopetest($query)
{
return $query->pluck('name');
}
}
and my controller code is
public function index()
{
$...
5
votes
1
answer
3k
views
laravel 4 updateExistingPivot with where clause not working
I have two models:
user and
resource
And relation table is resource_user.
the fields in resource_user are:
id | resource_id | user_id | another_id
I have this relation in user:
public ...
5
votes
4
answers
3k
views
Laravel eloquent relationships 3rd level
tables :
divisions
id name
districts
id division_id name
subdistricts
id district_id name
class Division extends Model
{
//
public function ...
5
votes
1
answer
2k
views
Laravel: conflict between model name and built-in facade
I have a Model in my Laravel app called Event. As I just discovered, this creates a conflict between my model and Illuminate\Support\Facades\Event, a built-in facade. The obvious solution here is to ...
5
votes
1
answer
2k
views
How to catch model events in pivot tables
I want to track (record) the changes made to each database row. This means, saving a log of each action (insert, update, delete) made to each record of each table.
This issue is solved for models, as ...
4
votes
1
answer
3k
views
Handling events only when model properties/columns are updated - Laravel Observer
I am dealing here with two events of a model, updating and updated as you can see in the below code. My concern is that I want to do some task in the updated event only if the teacherId has been ...
4
votes
4
answers
9k
views
Laravel can't find class in namespace
I using Laravel, I have a Model class under App/Models
<?php
namespace App\Models;
class TodoList extends \Eloquent{
public function listItems(){
return $this->hasMany('TodoItem');
}
...
4
votes
2
answers
19k
views
Laravel count column of related model
I want to count the number of upvotes and downvotes from ScripRating for a certain Script.
Script.php:
public function ratings()
{
return $this->hasMany('ScriptRating');
}
ScriptRating.php:
...
4
votes
2
answers
937
views
Replace the related model collection array with a new formatted one
I have an order, lamp and lamp_order table. Currently I fetch the Orders and it's Lamp relationships. I also fetch the Images that are related to the Lamps like this:
$orders = Order::with('lamps....
4
votes
1
answer
15k
views
Join two Models using Eloquent in laravel 5.3
I have two tables.
users table:
id (integer)
name (string)
email (string)
phone (string)
codeMeli (string)
and user_admin table:
userId (integer)
adminId (integer)
Note: adminid field keeps ...
4
votes
1
answer
6k
views
Laravel Repositories and Eloquent Models
After reading a few tutorials and mostly watching videos on Laracasts, I was considering adding an abstraction layer in my website using Repositories which would be injected in my Controllers through ...
4
votes
1
answer
2k
views
Laravel - Save on multiple models/relationships
I have 3 models: User, Campagin, CampaginType
User has many Campagin
Campagin belongs to CampaginType
(User does not related with CampaginType)
When I save a campaign (contains user_id and ...
3
votes
2
answers
34k
views
How to join 3 tables using laravel eloquent
How can I apply this query in laravel 5?
I have 3 table, Post, Profile, Comments
Post{id,title,body,user_id}
profile{user_id,last_name,first_name}
comments{comments,user_id,post_id}
I want to get ...