Change the name of the column to be taken from the database - php

How can I change the questioned to the database which is automatically generated.I have in blade template this
{{\Illuminate\Support\Facades\Auth::user()->friends()->get()}}
and it generates
Unknown column 'friends.user_id' in 'where clause' (SQL: select * from `friends` where `friends`.`user_id` = 1 and `friends`.`user_id` is not null
because my column name is user_id1 insted user_id.
and I do not know why the query to the database takes the user_id value instead of user_id1. How can I change it ?

Define the custom foreign key in the friends relationship:
public function friends()
{
return $this->hasMany(Friend::class, 'user_id1');
}

Related

How to change id to uuid?

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from shops where username = csesumonpro and id 5d4cb970-197d-41db-90e1-26988d543935)
how to change id to uuid when execute query? i am using uuid in my model replace of id i already defined
protected $primaryKey = 'uuid'; // in my model
Show above error when use unique email validation, unique email validation execute a query query autonomic capture id but i need uuid .otherwise everything is ok.
'email' => 'unique:users,email,'.$user->id // validation code
You need to specify the uuid column name as you are not using numerical IDs.
So it should read:
'email' => 'unique:users,email,'.$user->uuid.',uuid'

How replace join query with whereHas?

I am using Laravel 5.6 and i have a standard query
Order::where('dict_statuses_id', 1)
->leftJoin('dict_statuses', 'dict_statuses_id', '=', 'dict_statuses.id')
->get();
So i have list of orders with details where status = 1 (new) after join table i see status as New or Complete. Is any way to change this query using whereHas ?
i tried use
Order::whereHas('status', function($q){
$q->where('dict_statuses_id', 1);
})->get();
But no results, in model Order i have
public function status()
{
return $this->hasMany('App\DictStatuses');
}
[Updated]
i have an error:
Column not found: 1054 Unknown column 'dict_statuses.orders_id'
in 'where clause' (SQL: select * from `orders` where exists
(select * from `dict_statuses` where `orders`.`id` = `dict_statuses`.`orders_id` and `dict_statuses_id` = 1)
and `orders`.`deleted_at` is null)
In my table i have table Orders where is field: dict_statuses_id
and table dict_statuses with fields: id, public_name
Why error is about dict_statuses.orders_id
Try Order::has('status')->get();
You can pass in variables to a relationship so you could potentially do.
public function status($id) {
return $this->hasMany(DictStatuses::class)->where('dict_statuses_id', $id);
}
Try defining the local key & foreign key.
$this->hasMany(DictStatuses::class, 'foreign_key', 'local_key');
I think your dict_statuses doesn't have a column called order_id.
Please include that column in your migration
then call
Order::whereHas('status', function($q){
$q->where('dict_statuses_id', 1);
})->get();
in your controller
or change the relation in Order model as
public function status(){
return $this->belongsTo('App\DictStatuses'):
}

SQL column not found although it exists

I'm using the Laravel framework to create a prototype and need to output an individual article from a database based on it's ID. Seems straightforward but I keep getting the following error when I query the database:
QueryException in Connection.php line 647:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'articles.id' in 'where clause' (SQL: select * from `articles` where `articles`.`id` = 1 limit 1)
I'm trying to retrieve a specific article based on the database column article_id via the articles table in my Article model. I am using a resource route thus the method name 'show' - so I can access via article/{$article_id} in the URL.
Here is my controller:
public function show($article_id) {
$article = Article::find($article_id);
$article->toArray();
return view('articles.view', array('article' => $article));
}
There is no articles.id. It should be articles_id. Am I missing something obvious here?
Thanks in advance, any pointers much appreciated.
The find command looks for whatever is set as the primary key, which by default is id. You can change the primary key in your model:
class Article {
protected $table = 'articles';
protected $primaryKey = 'articles_id';
}
find() searched by primary key, so change the query to:
$article = Article::where('article_id', $article_id)->first();

Laravel - Access relationship through other

I have the following tables:
entries
- id
- category_id
- name
field
- id
- category_id
- name
field_values
- id
- entry_id
- field_id
- value
category
- id
- name
Each Category has certain fields. If I add an entry, I choose the category and I can enter the values for the field for the entry.
Let's say we have the Category "Venues". Venues has the custom field named "Capacity". If I add a news entry, I need to fill the field "Capacity".
This is how I read the fields and their values for an entry:
#foreach($data->field_values as $values)
{{$values->field->name}}: {{$values->value}}
#endforeach
I can only access the field table through the "field_values" table. Isn't it possible to access field directly from entry maybe with an hasManyThrough ?
This (in Entry Model):
public function fields()
{
return $this->hasManyThrough('App\Field', 'App\FieldValues');
}
throws the following mysql error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'fields.field_values_id' in 'on clause' (SQL: select `fields`.*, `field_values`.`entry_id` from `fields` inner join `field_values` on `field_values`.`id` = `fields`.`field_values_id` where `field_values`.`entry_id` in (3))
Why I need to add a field_values_id to Field? Isn't there a different approach?
I just want to foreach through entry->field and not do it by entry_field_values->field
If you just want to access the Fields for an entry, why not do a hasMany relationship ?
Entry Model:
public function fields()
{
return $this->hasMany('App\Field');
}
Then you can get all the fields for an entry as:
$fields = $entry->fields()->get();

Create complex hasManyThrough Laravel 4 query

I have classes User, User_Role and Role_Permission with correspondent tables
users (id)
users_roles (user_id, role_id, one user_id has one role_id)
roles_permissions (role_id, permission_id, one role_id has many permission_id)
I want to get permissions for user via $user->permissions. For this I wrote function
public function permissions()
{
return $this->hasManyThrough('User_Role', 'Role_Permission', 'user_id','role_id');
}
I get the error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'roles_permissions.user_id' in 'field list' (SQL: select `users_roles`.*, `roles_permissions`.`user_id` from `users_roles` inner join `roles_permissions` on `roles_permissions`.`id` = `users_roles`.`role_id` where `roles_permissions`.`user_id` = 1)
What am I doing wrong? Thanks!
Switch the order of the first two arguments:
public function permissions()
{
return $this->hasManyThrough('Role_Permission', 'User_Role', 'user_id', 'role_id');
}
Eloquent is looking for the user_id column in the Role_Permission table, which it won't find..

Categories