INITIAL NOTE: I AM WILL AWARE OF HOW TO IGNORE ROWS BY PRIMARY KEY! THAT IS NOT THE ISSUE AT HAND!
First, let me give you the structure of my database table...
+-------------+------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------+------+-----+---------------------+-----------------------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | text | NO | | NULL | |
| description | text | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | on update CURRENT_TIMESTAMP |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| created_by | int(11) | NO | | NULL | |
| public | tinyint(1) | NO | | 1 | |
+-------------+------------+------+-----+---------------------+-----------------------------+
I want it so that if I am storing a new topic(row) in the database, it will only check rows where public = 1 so if a post is "deleted"(or public = 0), it will not count in the unique verification.
This is how I currently handle NEW posts:
'title' => 'required|unique:topics',
This is how I currently handle UPDATING posts:
'title' => 'required|unique:topics,title,'.Request::input('id'),
Any help is appricatied. Thank you!
Based on the Validation documentation at Laravel:
unique:table,column,except,idColumn
Also, from: http://brianretterer.com/quick-tip-laravel-unique-validation/
'title' => 'required|unique:articles,title,NULL,NULL,public,0'
Related
I have two tables as below:
Table Job_Announcement used to store information about Job, defined as below:
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| job_id | int(11) | NO | PRI | NULL | auto_increment |
| job_title | varchar(255) | NO | | NULL | |
| category | varchar(255) | NO | | NULL | |
| term | varchar(255) | NO | | NULL | |
| num_experiences | int(11) | NO | | NULL | |
| num_hiring | int(11) | NO | | NULL | |
| Salary | varchar(255) | NO | | NULL | |
| qualification | varchar(255) | NO | | NULL | |
| location | varchar(255) | NO | | NULL | |
| gender | varchar(255) | NO | | NULL | |
| job_content | text | NO | | NULL | |
| job_requirement | varchar(255) | NO | | NULL | |
| publish_date | date | NO | | NULL | |
| close_date | date | NO | | NULL | |
| contact_info | varchar(255) | NO | | NULL | |
| userid | varchar(255) | NO | | NULL | |
| publish | tinyint(1) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
And Table job_announcement_deleted, used to store deleted record from table job_announcement
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| job_id | int(11) | NO | PRI | NULL | auto_increment |
| job_title | varchar(255) | NO | | NULL | |
| category | varchar(255) | NO | | NULL | |
| job_content | text | NO | | NULL | |
| publish_date | date | NO | | NULL | |
| close_date | date | NO | | NULL | |
| userid | varchar(255) | NO | | NULL | |
| publish | tinyint(1) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
In order to move deleted record from job_announcement table to job_announcement_deleted table, I defined trigger definition in phpmyadmin like this:
Trigger Name: before_delete_job
Table: job_announcement
Time: BEFORE
Event: Delete
Definition:
BEGIN
INSERT INTO job_announcement_deleted VALUES(old.job_id,old.job_title,old.category,old.job_content,old.publish_date,old.close_date,old.userid,old.publish);
END
Definer: root#localhost
The trigger event before_delete_job is working fine that the deleted record moved to table job_announcement_deleted.
My problem is if I want to restore deleted record back to table job_announcement, I define similar trigger definition event like above, ex. job_announcement_restore for tale job_announcement_deleted however, how can I do if I want to delete record permanently from job_announcement_deleted? because I want user have options either to restore it or to deleted permanantly.
Thanks.
For the restore just do the reverse process you did for delete (sending data from job_announcement_deleted to job_announcement)
For the perma delete you just have to get the job_id field, since it's auto-incremeneted making it unique for every job.
After you acquire job_id from the user (e.g if he clicks delete from this row you extract job_id from that row), just use DELETE FROM like this :
DELETE FROM job_announcement_deleted WHERE job_id = #job_id
(where #job_id will be the one you acquired earlier in the process)
Also, you shouldn't be sending job_id to the job_announcement_deleted table, since the field is auto-increment, meaning it'll add a new value (incremented by 1 by default) for that field, so there are no duplicates for that field. You just destroy the meaning of auto-increment by doing that, specially since primary key must be a unique value. You could have trouble later on with the database if you get duplicate values for that field, so I suggest you just leave that field unfilled so auto-increment can do it's job.
I am using Entrust's default table structure:
permissions table:
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| display_name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| created_at | timestamp | NO | | NULL | |
| updated_at | timestamp | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
permission_role table:
+---------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| permission_id | int(10) unsigned | NO | PRI | NULL | |
| role_id | int(10) unsigned | NO | PRI | NULL | |
+---------------+------------------+------+-----+---------+-------+
roles table:
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| display_name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| created_at | timestamp | NO | | NULL | |
| updated_at | timestamp | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
Now, given a role_id I'd like to get select the following from this database:
permissions.id
permissions.display_name
whether the permission_role table contains an entry with the permission_id and the given role_id
The last one turned out to be a bit tricky in Eloquent.
This SQL query accomplishes exactly what I need (ID is obviously replaced by a valid role ID):
SELECT p.id, p.display_name, IF(pr.role_id = ID, 1, 0) AS has_role
FROM permissions p
LEFT OUTER JOIN permission_role pr ON p.id = pr.permission_id;
Example output:
+----+--------------+----------+
| id | display_name | has_role |
+----+--------------+----------+
| 1 | Edit users | 1 |
| 2 | View users | 0 |
| 3 | Delete users | 0 |
+----+--------------+----------+
Can anyone help me out here, on how to do this using Eloquent?
I've tried this, but it always returns 1 (true) in the third column, unlike the SQL query (as seen above).
$result = DB::table('permissions')
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permission_role.role_id')
->select(DB::raw('permissions.id, permissions.display_name, IF(permission_role.role_id = ID, 1, 0) AS has_role'))
->get();
Ideally, I'd like to do this without using DB::raw, although it is completely fine if that is what it takes.
Thanks in advance for any help!
Structurally, the Query Builder query you've shown looks fine.
What does not look fine is the left join. Shouldn't this:
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permission_role.role_id')
be this:
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
?
So i'm trying to get my head around using eloquent for many to many relationships in my application.
I have three tables as followed
user
+----------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| email | varchar(255) | NO | UNI | NULL | |
| password | varchar(60) | NO | | NULL | |
| remember_token | varchar(100) | YES | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| active | enum('yes','no') | NO | | NULL | |
| last_login | timestamp | NO | | 0000-00-00 00:00:00 | |
+----------------+------------------+------+-----+---------------------+----------------+
user_has_address
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| address_id | int(10) unsigned | YES | MUL | NULL | |
| users_id | int(10) unsigned | YES | MUL | NULL | |
+------------+------------------+------+-----+---------+-------+
address
+---------------+----------------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+----------------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name_number | varchar(45) | NO | | NULL | |
| first_line | varchar(45) | NO | | NULL | |
| second_line | varchar(45) | NO | | NULL | |
| town_city | varchar(45) | NO | | NULL | |
| state_country | varchar(45) | NO | | NULL | |
| post_zip | varchar(45) | NO | | NULL | |
| type | enum('delivery','billing') | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| deleted_at | timestamp | YES | | NULL | |
+---------------+----------------------------+------+-----+---------------------+----------------+
in my user repository i have the following
namespace App\Libraries\Repositories\Core\Users;
use Schema;
use App\Models\Core\User;
use Bosnadev\Repositories\Eloquent\Repository;
use Symfony\Component\HttpKernel\Exception\HttpException;
class UserRepository extends Repository
{
public function getUsersAddresses()
{
return $this->userModel->hasManyThrough('App\Models\Bundle\Addresses\Address','App\Models\Bundle\Addresses\UserHasAdress','id','address_id');
}
}
Im returned an object that shows parent and related classes but im not actually returned my users address. Is there something im missing?
Alexrussell made some good points in his comment that you could possibly address, however I believe your immediate problem is a missing ->get() at the end of your return line.
Without it, you would be required to call your method like:
$repository->getUsersAddresses()->get();
As hasManyThrough will return an instance of Illuminate/Database/Eloquent/Relations/HasManyThrough not the actual results
For reference:
https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Relations/HasManyThrough.html
Note in the example here: https://laravel.com/docs/5.1/eloquent-relationships#many-to-many
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
The example usage includes a get(): $roles = App\User::find(1)->roles()->orderBy('name')->get();
Hope this helps
I have two tables. This one is called teams:
+------------+--------------+------+----------------------+----------+
| Column | Type | Null | Default | Comments |
+------------+--------------+------+----------------------+----------+
| id | int(10) | No | | |
| apikey | varchar(255) | Yes | NULL | |
| name | varchar(255) | Yes | NULL | |
| logo | varchar(255) | Yes | NULL | |
| url | varchar(255) | Yes | NULL | |
| hashtag | varchar(255) | Yes | NULL | |
| created_at | timestamp | No | 0000-00-00 00:00:00 | |
| updated_at | timestamp | No | 0000-00-00 00:00:00 | |
+------------+--------------+------+----------------------+----------+
The other one is called streamers:
+--------------+--------------+------+----------------------+----------+
| Column | Type | Null | Default | Comments |
+--------------+--------------+------+----------------------+----------+
| id | int(10) | No | | |
| apikey | varchar(255) | Yes | NULL | |
| name | varchar(255) | Yes | NULL | |
| team | int(11) | Yes | NULL | |
| type | varchar(255) | Yes | NULL | |
| twitch | varchar(255) | Yes | NULL | |
| mlg | int(11) | Yes | NULL | |
| url | varchar(255) | Yes | NULL | |
| twitter | varchar(255) | Yes | NULL | |
| status | int(11) | Yes | NULL | |
| viewers | int(11) | Yes | NULL | |
| created_at | timestamp | No | 0000-00-00 00:00:00 | |
| updated_at | timestamp | No | 0000-00-00 00:00:00 | |
| last_live | timestamp | No | 0000-00-00 00:00:00 | |
| last_edit_by | int(11) | Yes | NULL | |
+--------------+--------------+------+----------------------+----------+
As you can see, the streamers table has a team field which is only the team ID.
My Team.php model already has this:
public function streamers()
{
return $this->hasMany('Streamer', 'team');
}
Here's what I want to do
I want to search the streamers by team, HOWEVER the end user should only have to use the name not the ID. How do I make the table relationship so that I can query the streamers table for the team name instead of the ID? Is this possible at all?
I would've assumed this is exactly what relationship hasOne is for, but apparently I was wrong. Since I obviously don't have the streamer ID's in the team table because every streamer ID on a team would be awful. Laravel relationships seems kinda wrong here... Because hasOne would fit on streamers but it only works with teams, but essentially the streamers have a team and the team belongs to many streamers and that would mean belongsToMany would make sense.
Thanks
What you're looking for is the belongsTo relationship.
In streamers include:
public function team()
{
$this->belongsTo('Team','team');
}
You can then query streamers by team like so:
$streamer = Streamer::whereHas('team', function($q)
{
$q->where('name', 'like', $teamName);
})->get();
More details can be found in the docs: http://laravel.com/docs/eloquent#querying-relations
So i have this two tables, the operators_payments AS op is populated with data, but the op.date_paid will be NULL, till payment date arrives, when this happens, the payment_process AS pp table is used to initialize the payment (pp.date_started is set to NOW()), then for payment completion the op.date_paid is set to pp.date_started. The shown query, is used to do this, all is good, but when all records are updated, one of the records and only one gets the op.date_paid with different time, specifically the second part e.g.(time set to all but one: 2012-07-05 17:28:14, time set to one: 2012-07-05 17:28:02).
Im using Mysql 5.5, the columns have the same type (TIMESTAMP).
I need this because i need the date to be exact as the one in pp.date_started.
My question is, Why does this happens, and what can i do to have this as espected?
UPDATE operators_payments AS op
JOIN payment_process AS pp
ON op.operator_id = pp.operator_id
AND pp.type = 0
AND pp.status = 1
SET op.date_paid = pp.date_started, pp.status = 2, pp.message=CONCAT(SUBSTRING_INDEX(message, '|', 1), '| was completed successfully!')
WHERE op.operator_id = {$this->operator_id}
AND op.date_paid IS NULL
AND op.date_end <= pp.date_accounted
+---------------+-----------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-----------------------+------+-----+-------------------+----------------+
| payment | int(10) unsigned | NO | PRI | NULL | auto_increment |
| operator_id | int(10) unsigned | NO | MUL | 0 | |
| date_paid | timestamp | YES | MUL | NULL | |
| date_start | timestamp | YES | | NULL | |
| date_end | timestamp | YES | MUL | NULL | |
| amount | decimal(6,4) unsigned | NO | | 0.0000 | |
+---------------+-----------------------+------+-----+-------------------+----------------+
+----------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+-------------------+-----------------------------+
| operator_id | int(11) | NO | PRI | NULL | |
| type | tinyint(4) | NO | PRI | NULL | |
| date_started | timestamp | YES | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| date_accounted | timestamp | YES | | NULL | |
| amount | decimal(6,4) | YES | | NULL | |
| status | tinyint(4) | YES | MUL | 0 | |
| message | varchar(255) | YES | | NULL | |
+----------------+--------------+------+-----+-------------------+-----------------------------+
I have a suspicious eye toward that on update CURRENT TIMESTAMP clause on the date_started on payment_process... I'm not actually sure what it could be doing in this query, but you are updating that table in this query, and using that value. I also don't like the semantic discord of a column called date_started which has it's value changed on every update... but I don't know how it's used. I would evaluate if that clause is necessary on that column, and see if you get this strange behavior without it,