how to get current entry info in laravel eloquent - laravel

i have a table in mysql
ques_id (auto increment , primary) ,
ques_title (string)
By using eloquent
$qinfo = new Question ;
$qinfo->ques_title = "xyz" ;
$qinfo ->save() ;
i want to know what will be ques_id of the above entry .
$qinfo->ques_id is not working .
Eloquent :
class Question extends Eloquent {
protected $table = 'questions';
public $timestamps = false ;
protected $primarykey = 'ques_id' ;
}
table schema
mysql> desc questions ;
+------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------------------+----------------+
| ques_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ques_title | varchar(200) | NO | UNI | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
+------------+------------------+------+-----+---------------------+----------------+
4 rows in set (0.01 sec)

Make sure your Question model is something like this:
class Question extends ELoquent {
//public $timestamps = false;
protected $table = 'questions';
protected $primaryKey = 'ques_id';
// ...
}
Now if you try this (that you have already tried) then it should work:
$qinfo = new Question;
$qinfo->ques_title = "xyz";
$qinfo ->save();
The ques_id of the newly created model could be retrieved using this:
dd($qinfo->ques_id); // an integer

Related

laravel eloquent error , I can not get created_at use groupBy method

I am trying to get max value and create at date with groupBy method.
But when I put the created_at column, I got an error.
$rankings = Ranking
::select(DB::raw('MAX(rankings.percentage_correct_answer) as percentage_correct_answer, rankings.user_id,rankings.created_at'))
->groupBy('rankings.user_id')
->get();
errors:
SQLSTATE[42000]: Syntax error or access violation: 1055 'app.rankings.created_at' isn't in GROUP BY (SQL: select MAX(rankings.percentage_correct_answer) as percentage_correct_answer, rankings.user_id,rankings.created_at from rankings where rankings.deleted_at is null group by rankings.user_id)
However, I did fine by pulling out the created_at column. Why?
And I want to get the created_at column, what should I do?
No error this code.
$rankings = Ranking
::select(DB::raw('MAX(rankings.percentage_correct_answer) as percentage_correct_answer, rankings.user_id'))
->groupBy('rankings.user_id')
->get();
Model code:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Ranking extends Model
{
use SoftDeletes;
protected $table = 'rankings';
protected $fillable = [
'user_id','percentage_correct_answer'
];
}
rankings table description:
+---------------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| user_id | bigint(20) unsigned | NO | | NULL | |
| percentage_correct_answer | int(11) | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+---------------------------+---------------------+------+-----+---------+----------------+
Thanks Tharaka!
I tried add database config in config/database.php:
'mysql' => [
....
'strict' => false,
//'strict' => true,
],
Then it works.
I referred to this question.
GroupBy Error in Laravel Eloquent Builder
But I don't really understand the cause of the error.

hasOneThrough with pivot table laravel eloquent

I have the following tables
mysql> describe records;
+-----------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| event_school_id | bigint(20) unsigned | NO | MUL | NULL | |
| athlete_id | bigint(20) unsigned | NO | MUL | NULL | |
| year | int(10) unsigned | NO | | NULL | |
| place | int(10) unsigned | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-----------------+---------------------+------+-----+---------+----------------+
8 rows in set (0.01 sec)
mysql> describe event_school;
+------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| event_id | bigint(20) unsigned | NO | MUL | NULL | |
| school_id | bigint(20) unsigned | NO | MUL | NULL | |
| notes | text | NO | | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+------------+---------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
mysql> describe events;
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(191) | NO | | NULL | |
| category_id | bigint(20) unsigned | NO | MUL | NULL | |
| created_at | timestamp | YES | | NULL | |
| updated_at | timestamp | YES | | NULL | |
| deleted_at | timestamp | YES | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
From my Record Eloquent model, how can I get an event from a record. It needs to go though event_school pivot table; but I don't have a model for EventSchool. Do I need to make one? Is there a way to do that without EventSchool model?
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class EventSchool extends Model
{
use SoftDeletes;
protected $guarded = ['id'];
protected $table = 'event_school';
public function event()
{
return $this->belongsTo('App\Event');
}
public function school()
{
return $this->belongsTo('App\School');
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Record extends Model
{
use SoftDeletes;
protected $guarded = ['id'];
public function athlete()
{
return $this->belongsTo('App\Athlete');
}
function event_school()
{
return $this->belongsTo('App\EventSchool','event_school_id');
}
function event()
{
//What can I put here to complete method
//right now have to do \App\Record::find(1)->event_school->event
//Want to do \App\Record::find(1)->event
}
}
EDIT:
Here is output from one of the answers
>>> \App\Record::find(3)->event_school->event
=> App\Event {#3035
id: 1,
name: "100 Meter",
category_id: 1,
created_at: null,
updated_at: null,
deleted_at: null,
}
>>> \App\Record::find(3)->event
=> null
Every time I got into this situation I end up creating a model for the pivot table. An additional benefit of doing this, is that you can access directly to that table without bringing the other end of the relationship.
You already have additional information to the foreign keys (notes). If you have a School and need the notes from an Event, but you DON'T need the Event, you can do that if the pivot table has a model.
Of course you could do it with a join without the need of a model.
The key here is to extend from Pivot instead of Model. One of the things it does is to get the table name as singular. But you can't use SoftDeletes (do you really need it? It doesn't makes much sense)
Note: Pivot models may not use the SoftDeletes trait. If you need to soft delete pivot records consider converting your pivot model to an actual Eloquent model.
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class EventSchool extends Pivot
{
protected $guarded = ['id'];
public function event()
{
return $this->belongsTo(Event::class);
}
public function school()
{
return $this->belongsTo(School::class);
}
}
Now, hasOneThrough is the inverse of what you need. You need something like belongsToThrough, but it doesn't exists. What you could use is an accessor:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Record extends Model
{
use SoftDeletes;
protected $guarded = ['id'];
public function athlete()
{
return $this->belongsTo(Athlete::class);
}
function eventSchool()
{
return $this->belongsTo(EventSchool::class);
}
function getEventAttribute()
{
return $this->eventSchool->event;
}
}
Doing \App\Record::find(1)->event will do exactly what you expect it to do.
I create a fresh install of laravel on my computer, set up the models and relations according to the information that you provide, and the simplest solution i came up with is this one:
public function event()
{
return $this->hasOneThrough(Event::class, EventSchool::class,
'event_id', 'id', 'id', 'event_id'
);
}
This is the test that i made:
// Within some controller:
$record = Record::find(2);
dd($record->event);
And this is the result, which i think is the result you are looking for:
Event {#303 ▼
#connection: "pgsql"
#table: "events"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:6 [▼
"id" => 2
"name" => "Event 2"
"category_id" => 2
"created_at" => "2019-09-16 03:15:07"
"updated_at" => "2019-09-16 03:15:07"
"laravel_through_key" => 2
]
#original: array:6 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}
Hope it helps.

Laravel 5 - Eloquent JOIN confusion

I have two simple tables, cats and breeds:
mysql> select * from cats;
+----+--------+---------------+----------+---------------------+---------------------+
| id | name | date_of_birth | breed_id | created_at | updated_at |
+----+--------+---------------+----------+---------------------+---------------------+
| 1 | Rita | 2008-07-06 | 1 | 2015-05-09 20:40:49 | 2015-05-09 20:50:20 |
| 2 | Muni | 1992-05-15 | 3 | 2015-05-09 20:50:54 | 2015-05-09 20:50:54 |
| 3 | Hector | 2005-01-23 | 4 | 2015-05-09 21:08:23 | 2015-05-09 21:08:23 |
+----+--------+---------------+----------+---------------------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from breeds;
+----+------------+
| id | name |
+----+------------+
| 1 | Domestic |
| 2 | Persian |
| 3 | Siamese |
| 4 | Abyssinian |
+----+------------+
4 rows in set (0.00 sec)
All I want to do is get a list of the cats that are of a certain breed. In the cats table, breed_id is a FK that points to the PK of the breeds table. The example code in my book which is supposed to return a view with all cats of a specific breed returns no results:
The url I am trying is /cats/breeds/Domestic
The view that is generated uses this logic in the routes.php file:
Route::get('cats/breeds/{name}', function($name)
{
$cats = Furbook\Cat::with('breeds')
->whereName($name)
->get();
dd($cats);
});
When I dd($cats) I get zero results even though I have a cat with a domestic breed_id of 1. What have I done wrong here?
EDIT:
I can get it to work with the following hacky code, which first gets the id from the breeds table by querying against the name field, then queries the cats table with that id:
$breed = Furbook\Breed::whereName($name)->get();
$id = $breed[0]['attributes']['id'];
$cats = Furbook\Cat::whereId($id)->get();
How do I make this into one Eloquent query? I see no examples for this kind of query on the Laravel site.
For more information this is how the models look:
class Breed extends Model {
public $timestamps = false;
public function cats()
{
return $this->hasMany('Furbook\Breed');
}
}
class Cat extends Model {
protected $fillable = [
'name',
'date_of_birth',
'breed_id',
];
public function breed()
{
return $this->belongsTo('Furbook\Breed');
}
}
You're asking the system to give you all cats with a name of Domestic - not all cats of the breed name Domestic.
Assuming your model relationships are in order, you could do e.g.
$cats = Furbook\Breed::whereName($name)->first()->cats;
Also, my Burmese cat just hit the monitor; I think she's upset that she's not in the breeds list.

Laravel 4 BelongsToMany sync() method doesn't work properly

I have problem when using sync() method. Please help me out here.
I have three tables as below.
Table: tbl_roles
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id_role | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(60) | NO | UNI | NULL | |
| description | text | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
Table: tbl_permissions
+----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------+----------------+
| id_permission | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| description | text | YES | | NULL | |
+----------------+------------------+------+-----+---------+----------------+
Table: tbl_link_roles_permissions
+---------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| id_role | int(10) unsigned | NO | MUL | NULL | |
| id_permission | int(10) unsigned | NO | MUL | NULL | |
+---------------+------------------+------+-----+---------+-------+
I have created many to many relationship between roles and permissions models.
Role model:
class Role extends Eloquent
{
/**
* Permissions
*/
public function permissions()
{
return $this->belongsToMany('Permission', 'tbl_link_roles_permissions', 'id_role', 'id_role');
}
}
Permission model:
class Permission extends Eloquent
{
/**
* Roles
*/
public function roles()
{
return $this->belongsToMany('Role', 'tbl_link_roles_permissions', 'id_permission', 'id_permission');
}
}
When I run the following code to sync roles' permissions. It give me an Integrity constraint violation error.
The Code:
$role = Role::find($id_role);
$permissions = Input::get('role_permissions');
/*
* Permissions array is like array('1','2','3')
* So I convert the value to integer.
*/
$parameters = array();
foreach($permissions as $permission) {
$parameters[] = intval($permission);
}
$role->permissions()->sync($parameters);
According to the error message, I found the SQL query is not correct.
Error meesage:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:
a foreign key constraint fails (`quidz_host_laravel`.`tbl_link_roles_permissions`,
CONSTRAINT `tbl_link_roles_permissions_id_permission_foreign` FOREIGN KEY
(`id_permission`) REFERENCES `tbl_permissions` (`id_permission`)) (SQL: insert into
`tbl_link_roles_permissions` (`id_role`) values (?)) (Bindings: array ( 0 => 1, ))
It only inserts id_role. This is not what I want. Can anyone tell me what I did wrong?
Thanks a lot.
I have solved my problem. It's caused by passing wrong parameters when building the relationships.
The relationship should be like the code below:
class Role extends Eloquent
{
/**
* Permissions
*/
public function permissions()
{
return $this->belongsToMany('Permission', 'tbl_link_roles_permissions', 'id_role', 'id_permission');
}
}
class Permission extends Eloquent
{
/**
* Roles
*/
public function roles()
{
return $this->belongsToMany('Role', 'tbl_link_roles_permissions', 'id_permission', 'id_role');
}
}

Unexpected doctrine validation error about length when setting the field null

I have a field that is defined as follows:
class Subcategory extends BaseSubcategory {}
abstract class BaseSubcategory extends Doctrine_Record
{
public function setTableDefinition()
{
// ...
$this->hasColumn('meta_description', 'string', 255);
// ...
}
// ...
}
Here's what the table looks like:
mysql> DESCRIBE subcategory;
+----------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
[...]
| meta_description | varchar(255) | YES | | NULL | |
[...]
+----------------------+------------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
Here's my code to save a record
$m = new Subcategory;
// ...
$m->meta_description = null;
$m->save();
I'm getting the following validation error
* 1 validator failed on meta_description (length)
Why is this happening?
The code samples above do not tell the whole story. I was being misled by an earlier save, in which the meta_description field was being overloaded with over 255 characters. False alarm!

Resources