How to use multiple "where like" in a relationship - Laravel - mysql

I have 4 tables in my DB. User , user_details, tags and taggables. By using the user table I am getting user along with user detail and tags. Following is table schema
Table user:
id, name, email, password
Table user_details
user_id, about, vision, picture
Table tags:
id, name
Table taggables
user_id, tag_id
here is my query:
User::with('userDetails','tags')->get();
I want to use where like in user name or tag name, How can I use multiple where like on user name and tag name????

Do you want something like this?
$users= User::with('userDetails','tags')
->where('name', 'LIKE',"%{$search}%")
->orWhereHas('tags', function($query) use($search) {
$query->where('name','LIKE',"%{$search}%");
})
->get();
$search is a variable here.

Related

how to group the fields inside in some addselect in querybuilder?

what I want is simple, I want to group specific fields of a query with querybuilder in 3 different levels of relationship with an alias, to be clearer, I have 3 related tables, consulta (medical consultation), paciente(pacient), doctor (doctor), usuario (user), so with all what I get in my query is the medical consultation with its pacient data, plus doctor data and with its related user data, what I want is to omit irrelevant fields within those related tables, I'm using addSelect to include all the data among the levels of relationship but what I get is all the fields, I want to retrieve specific fields of the "paciente" table, and in the user table (which is the user related of the doctor), I will show how the scheme would be:
Consulta :{
// omit the other fields
{paciente: name and last name}
{doctor: { usuario: name and last name}}}
$queryBuilder = $em->getRepository('MedicoBundle:Consulta')
->createQueryBuilder('e')
->leftJoin('e.paciente', 'a')
->addSelect('a')
->leftJoin('e.doctor', 'o')
->addSelect('o')
->leftJoin('o.usuario', 'u')
->addSelect('u')
->where('e.fecha =:fecha')
->orderBy('e.id', 'ASC')
->setParameter('fecha', $fecha)
->getQuery();
it retrieves all the fields which is not so bad, but I want to retrieve is some of them and omit others as I show in the scheme
Just select what you want instead of selecting everything :
$queryBuilder = $em->getRepository('MedicoBundle:Consulta')
->createQueryBuilder('e')
->leftJoin('e.paciente', 'a')
->addSelect('a.name, a.lastname')
->leftJoin('e.doctor', 'o')
->addSelect('o.name, o.lastname')
->leftJoin('o.usuario', 'u')
->where('e.fecha =:fecha')
->orderBy('e.id', 'ASC')
->setParameter('fecha', $fecha)
->getQuery();
Though I do not know how you called your name and lastname attribute, so be careful with it.

Is there a way to make my credential information not return after I insert it into my database

Lets say if I wanted to insert an user infomation into Users table in my database, the following is a json format of the information I want to insert:
{
'id': 1,
'username': 'asd123',
'password': 'abc123'
}
How can I make it return only the id and the username when I do a SELECT * FROM Users WHERE id = 1
Instead of
SELECT *
use
SELECT id, username
Explanation: * is used when we want all columns, otherwise use the specific column names. Use * only when it is required.

Mixing string into the mysql query result

I have 2 tables in my database. Table users contains user_id, first_name, last_name, email. Table tickets contains information about sold tickets and there is column user_email.
What I want to do now is to take all the users who bought the ticket and join it with the users table. The problem is that as a result I need to have a list of links that look like this:
<a href='http://example{IdNumberHere}.com'> {FirstNameHere} {LastNameHere} </a>
Is it possible to have a result like this?
You can use the CONCAT() function to join strings together, so something like
SELECT CONCAT("<a href='http://example", user_id, "'> ", first_name,
" ", last_name, " </a>") AS addr
FROM ...
Should do the trick. You would then reference this concatenation as $dbRow['addr'] in your PHP output.

how select row in MySQL by check when column data include delimiter

I have question . If i have user table and a column name be freinds .
I want use some user id inside a column friends with this value : 1|2|45|18 .
This means for example user id =5 have 4 freinds with these id 1|2|45|18.
I don't want use two table for this situation.
How can select each users data when friends with user id = 5 ?
In php we have explode function for slice each delimeter .
Can i set delimeter for this target or i should use two table user and friends in MySQL ?
It's an awful case, especially if you inherit some DB with such a scheme, but to find friends you can use:
SELECT friend.*
FROM user u
left join user friend on concat('|', o.friends, '|') like concat('%|', friend.id, '|%')
where u.id = 5;

Laravel Eloquent: order results by field in related table

How do I order the results of a query by a field in a related table?
I have two tables,
table users:
id, first_name, last_name, etc
table videos:
id, user_id, title, etc
Model: Video
public function user()
{
return $this->belongsTo('App\Models\User');
}
For example I am querying all videos where title = "video 1" and using skip & take for pagination/pager.
I now want to order this list of videos by the users first name.
$query = \App\Models\Video::where('title','=','Video 1')->skip(0)->take(10);
What is the best/efficient way of doing this? I can't order the array/collection after the query as then the pagination won't work.
Thanks.
Why don't you use:
$query = \App\Models\Video::where('title','=','Video 1')
->join("users","users.id","=","user_id")
->orderBy("users.first_name")->skip(0)->take(10);