Validation in laravel 5.1 - validation

as would do this in laravel ?
I need to pass this condition laravel
$query = mysql_query("SELECT * FROM citas WHERE
especialidad='".$especialidad."'AND doctor='".$doctor."'AND
fecha ='".$fecha."' AND hora = '".$hora."'" , $con);
if ($existe = mysql_fetch_object($query))
{
}else{}
this is my table in the database:
fecha | hora | especialidad | doctor |
12-07-2015 | 01:17:00 | General | Carlos |
12-07-2015 | 01:35:00 | General | Carlos |
This would be what shipping.
fecha | hora | especialidad | doctor |
12-07-2015 | 01:22:00 | General | Carlos | <--- It is saved by the time it is different
12-07-2015 | 01:35:00 | General | uriel | <--- It is saved by the dcotor it is different
12-07-2015 | 01:35:00 | General | carlos | <--- It is no, because
it already exists

Try this:
$query = DB::table('citas')
->where('especialidad',$especialidad)
->where('doctor',$doctor)
->where('fecha',$fecha)
->where('hora',$hora)
->get();
if($query != null){
// your code will be here
}else{
// you code will be here
}
Or you can try this by Eloquent Model. I assume you have a model named Cita
Add use App\Cita at top of your code after namespace in your controller
$query = Cita::where('especialidad',$especialidad)
->where('doctor',$doctor)
->where('fecha',$fecha)
->where('hora',$hora)
->get();
if($query != null){
// your code will be here
}else{
// you code will be here
}

Related

Laravel search by date plus time from releated table

I have table order related to warehouse with related to warehouse_delivery.
With following structure:
+--------------------------------------+ +-----------+
| Order | | Warehouse |
+----+--------------+------+-----------+ +----+------+
| id | warehouse_id | cost | send_date | | id | name |
+----+--------------+------+-----------+ +----+------+
+--------------------------------------------------------------------------+
| WarehouseDelivery |
+----+--------------+------------------------+-----------------------------+
| id | warehouse_id | domestic_delivery_time | international_delivery_time |
+----+--------------+------------------------+-----------------------------+
Delivery time is stored as days.
And now I would like to search by send_date to find Orders where send_date + domestic_delivery_time is today.
I don't have a idea how can I grab that domestic_delivery_time added to send_date.
Order::where('warehouse_id', $warehouseId)
->with(['product', 'warehouse.warehouseDelivery'])
->get();
How to write query?
Thank you.
I'm not sure there's a database-agnostic solution for this.
You'd need to use DB::raw. Here's a solution using a scope that covers MySQL, SQL Server and PostgreSQL:
// On Owner.php
public function scopeDueToday($query) {
$today = \Carbon\Carbon::now()->format('Y-m-d');
$driver = $query->getConnection()->getDriverName();
return $query->whereHas('warehouse.warehouseDelivery', function ($deliveries) use ($today, $driver) {
$raw = [
'mysql' => "CAST(start_date + INTERVAL domestic_delivery_time days AS date)",
'pgsql' => "(start_date + (domestic_delivery_time || ' day')::interval)::date",
'sqlsrv' => "CONVERT(date, DATEADD(day, domestic_delivery_time, start_date))",
];
return $deliveries->where(\DB::raw($raw[$driver]), '=', $today);
});
}
// On your controller
Order::dueToday()->where('warehouse_id', $warehouseId)
->with(['product', 'warehouse.warehouseDelivery'])->get();

Performance of whereHas laravel

I have read many articles warning about whereHas(it use subquery in where exists) performance when the data is big(Here: 18415, 5328, ...). In my case below will I need to replace it? And how?.
Table products
+----+---------------------+
| id | created_at |
+----+---------------------+
| 1 | 2020-10-10 10:10:10 |
| 2 | 2020-10-10 10:10:10 |
+----+---------------------+
Table product_translations(indexes: map_id, language_code, slug)
+----+--------+---------------+-----------+-----------+
| id | map_id | language_code | name | slug |
+----+--------+---------------+-----------+-----------+
| 1 | 1 | en | name en 1 | name-en-1 |
| 2 | 1 | es | name es 1 | name-es-1 |
| 3 | 2 | en | name en 2 | name-en-2 |
| 4 | 2 | es | name es 2 | name-es-2 |
+----+--------+---------------+-----------+-----------+
Product.php
function translation(){
return $this->hasOne('App\Models\ProductTranslation', 'map_id', 'id');
}
ProductController.php
function view($slug){
$currentItem = Product::with(['translation' => function($q) use($slug){
$q->where(['slug' => $slug, 'language_code' => \App::getLocale()]);
}])->whereHas('translation', function($q) use ($slug){
$q->where(['slug' => $slug, 'language_code' => \App::getLocale()]);
})
if ($currentItem == null) {
abort(404);
}
return view('product.view', compact('currentItem'));
}
I'm using laravel 8x.
You can replace whereHas with whereIn.
$currentItem = Product::with(['translation' => function($q) use($slug){
$q->where(['slug' => $slug, 'language_code' => \App::getLocale()]);
}])->whereIn(
'id',
ProductTranslation::select('map_id')
->where(['slug' => $slug, 'language_code' => \App::getLocale()])
);
Select the foreign key from a subselect and uses it with where in.

How to convert into array in this condition?

In Student model, i have made attribute like this
protected $appends = ['present'];
public function getPresentAttribute(){
return $this->getTotalDays()-$this->getAbsentDays();
}
My Database looke like this
| id | student_name | created_at |
*----*----------------------*-------------------*--
| 1 | Adam | 2020/11/10 |
| 2 | Annie | 2020/11/10 |
|3 | Paul | 2020/11/10 |
if i do like this in controller then
$students=Student::get();
foreach($students as $stu){
echo "<pre>"; print_4($stu->student_name,$stu->present); die;
}
My result will be like this
Adam -> 32
Annie ->34
Paul->33
But i need to convert like this
['Students', 'Present Day',],
['Adam' , 32],
['Annie' , 34],
['Paul' , 33],
So in controller, I tried like this
public function attendancePerformance(){
$students=Student::get();
$data=[];
$student_data=[];
$arrayHeader = ["Students","Present Day"];
array_push($data,$arrayHeader);
foreach ($students as $stu) {
array_push($student_data, $stu->student_name,$stu->present);
}
array_push($data,$student_data)
}
But it not working.How should i do it ?

Codeigniter query top store by price and and field by today

I am the beginner of codeigniter.
I have a query like this, I want to use this query in codeigniter
I would like to get top store_name by total_price
+---+------------+-------------+-------------+---------------------+
| id| store_name | quality | total_price | time |
+---+------------+-------------+-------------+---------------------+
| 1 | store_a | 2 | 300 | 2019-06-19 08:20:57 |
| 2 | store_b | 1 | 100 | 2019-06-20 08:30:57 |
| 3 | store_c | 4 | 50 | 2019-06-20 08:33:57 |
| 4 | store_b | 2 | 300 | 2019-06-20 08:35:57 |
+---+------------+-------------+-------------+---------------------+
I have try
$this->db->select('store_name, SUM(total_price) AS t_price', false);
$this->db->group_by('store_name');
$query = $this->db->get($this->table_name);
return $query->result();
i need output by date time today
+------------+-------------+-------------+---------------------+
| store_name | quality | total_price | time |
+------------+-------------+-------------+---------------------+
| store_b | 3 | 400 | 2019-06-20 08:30:57 |
| store_c | 4 | 50 | 2019-06-20 08:33:57 |
Update
work for me
$date = new DateTime("now");
$curr_date = $date->format('Y-m-d ');
$this->db->select('store_name, SUM(total_price) AS total_price ,SUM(quality) AS quality', false);
$this->db->group_by('store_name');
$this->db->where('DATE(time)',$curr_date);
$this->db->order_by('total_price', "DESC");
$query = $this->db->get($this->table_name);
return $query->result();
You can try this solution for your problem :
$date = new DateTime("now");
$curr_date = $date->format('Y-m-d ');
$this->db->select('store_name, SUM(total_price) AS total_price ,SUM(quality) AS quality', false);
$this->db->where('DATE(time)',$curr_date);
$this->db->group_by('store_name');
$this->db->order_by('total_price', "DESC");
$query = $this->db->get($this->table_name);
return $query->result();
It will helpfull to you.

I need to write eloquent query that return messages having specific tag

messages Table
+----+----------+
| id | text |
+----+----------+
| 1 | message1 |
| 2 | message2 |
+----+----------+
tags table
+----+-----------+
| id | name |
+----+-----------+
| 1 | valentine |
| 2 | funny |
| 3 | santa |
+----+-----------+
message_tag
+----+------------+--------+
| id | message_id | tag_id |
+----+------------+--------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
+----+------------+--------+
I have written this query so far to get all messages but i cant return the
message where tag_id = 1;
Message::with([
'tags'=>function($q){
$q->select("name","tag_id");
}
])->select("id", "text")->paginate(10);
i have used where clause inside function($q){$q->where('tag_id',1)}. but it didn't worked.
You have to include use Illuminate\Support\Facades\DB; at the top of class
$tagId = 1;
$return = DB::table('message_tag')
->join('messages', 'messages.id', '=', 'message_tag.message_id')
->join('tags', 'tags.id', '=', 'message_tag.tag_id')
->where("message_tag.tag_id", "=", $tagId)
->select('messages.text', 'tags.text')
->get();
Try using whereHas(), assuming you set a many-to-many relationship properly:
$tag = 'Madhab452';
$messages = Message::whereHas('tags', function ($query) use ($tag) {
$query->where('name', '=', $tag)
->select('name', 'tag_id');
})->get();

Resources