Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

Apply Validation and condition Using Laravel Query,array,collection and string

Some Important Laravel Query Screenshot

use of selectraw

  • get single value
  • count
  • sum

get all data using query

query is used for search

Booking.php

get_my_bookings
check_booking
get organizor booking
organiser_view_booking
Delete_booking
total_booking
total_revenue
update_payment_type
get_seat_availability_by_ticket
Category.php
get_categories
get_all_categories

city.php

get_countries_having_events()

Discount.php

get_discount()

Event.php

get_latest_events()
get_featured_events
get_top_selling_events
get_allevent_by_category
get_upcomming_events
get_cities_events
get_organizers||get_customers
search_customers
disable_event
get_all_events
total_event

iternary.php

get_days

mydiscount.php

insertselect_discount||insert_discount
get_discountweek||get_discountweek_second

rvsp.php

get_my_bookevents

Ticket.php

get_event_tickets
get_events_tickets

User.php

all_operator_user

get single value
Image description

count
Image description

Image description

sum
Image description

group by two field

Image description

get all data using query

is used for filter see Event.php
using query
Image description
oR using get

$query = Event::get();
Enter fullscreen mode Exit fullscreen mode
$query = DB::table('users'); 
        $query->select('name', 'id', 'email')
                ->where('role_id', 3);
Enter fullscreen mode Exit fullscreen mode

oR using select
Image description

Booking.php

  • use of left join
  • use of selectRaw
  • use of alias,orderby,paginate.

condition

  • condition based on customer_id and publish

get_my_bookings

Image description
Image description

make_booking

  • use of create Image description

check_booking

  • use of first
  • use of multiple condition inside where having multiple param .

condition
replacement of multiple where and
get data using first using multiple condition
update data using multiple condition
Image description

Image description

get organizor booking || get_organiser_tripbookings(rvsp.php

  • whereDate
  • greater or equal to
    condition

  • in case of searching by between two dates

  • in case of searching by start_date

  • in case of searching by event_id

in case of searching by between two dates
Image description
in case of searching by start_date
Image description

in case of searching by event_id
Image description
Image description

Full Code

Image description

Image description

organiser_view_booking

  • use of selcet,where,first
  • how to use DB::table($this->tb).

condition
Image description

Delete Example

  • use of Delete
  • how to use DB::table($this->tb).

condition

Image description

count Example

  • use of count with where and without where just attach with model.

condition

  • total_bookings
  • count total no of booking created by that organizors id Image description

sum Example

  • use of sum with where and without where just attach with model

condition

  • total_revenue

  • total money collected by that oranizors id

Image description

update_payment_type

  • how to use $key and $value pair inside foreach
  • take model::get() inside one variable use it inside foreach
  • apply if condition on $value on (key,value) pair

condition
update multiple row using get() and take key,value pair inside foreach and then if stmt

Image description

get_seat_availability_by_ticket

  • select,
  • selectRaw,
  • where,
  • groupBy two field,
  • orderBy,
  • get

condition

  • to get seat_availability_by_ticket first find total booked using sum through selectRaw

to get seat_availability_by_ticket first find total booked using sum through selectRaw
selectRaw("SUM(quantity) as total_booked")

Image description

Category.php

get_categories

  • offset,
  • limit,
  • take,
  • skip,

condition

Image description
Image description

get_all_categories

  • leftJoin
  • select
  • use count selectRaw
  • groupBy.

condition
count total no of event belongs to categories using selectraw

Image description

Image description

city.php

get_countries_having_events()

  • leftJoin
  • select
  • where and not equal
  • use count selectRaw
  • groupBy.

condition

Image description

Discount.php

get_discount()

  • get dynamic dropdown data like week 1,week 2.. using condition on days greater than or equal to
  • empty condition on first
  • condition on take.

condition

** get dynamic dropdown data like week 1,week 2.. using condition on days greater than or equal to**

 public function get_discount($dayss,$event_id)
    {
        log::info('yeh mera days hai naa');
        log::info($dayss);
     $tickets=   Myticket::where('ticket_event_id',$event_id)->first();
     $trip=Trip::where('id',$event_id)->first();
     $publish=$trip->publish;

     if(empty($tickets)|| $publish==0 ||$publish==1)
     {
        if($dayss>=126)
        {
        $result = Discount::take(17)->get();
        log::info($result);
        return to_array($result);
    }
    elseif($dayss>=119)
    {
    $result = Discount::take(16)->get();
    log::info($result);
    return to_array($result);
}
elseif($dayss>=112)
{
$result = Discount::take(15)->get();
log::info($result);
return to_array($result);
}

elseif($dayss>=105){
    $result = Discount::take(14)->get();
    log::info($result);
    return to_array($result);

}

elseif($dayss>=98){
    $result = Discount::take(13)->get();
    log::info($result);
    return to_array($result);

}

elseif($dayss>=91){
    $result = Discount::take(12)->get();
    log::info($result);
    return to_array($result);

}

elseif($dayss>=84){
    $result = Discount::take(11)->get();
    log::info($result);
    return to_array($result);

}
elseif($dayss>=77){
    $result = Discount::take(10)->get();
    log::info($result);
    return to_array($result);

}
elseif($dayss>=63){
    $result = Discount::take(9)->get();
    log::info($result);
    return to_array($result);

}
elseif($dayss>=56){
    $result = Discount::take(8)->get();
    log::info($result);
    return to_array($result);

}
elseif($dayss>=49){
    $result = Discount::take(7)->get();
    log::info($result);
    return to_array($result);

}
elseif($dayss>=42){
    $result = Discount::take(6)->get();
    log::info($result);
    return to_array($result);

}
elseif($dayss>=35){
    $result = Discount::take(5)->get();
    log::info($result);
    return to_array($result);

}
elseif($dayss>=28){
    $result = Discount::take(4)->get();
    log::info($result);
    return to_array($result);

}
elseif($dayss>=21){
    $result = Discount::take(3)->get();
    log::info($result);
    return to_array($result);

}
elseif($dayss>=14){
    $result = Discount::take(2)->get();
    log::info($result);
    return to_array($result);

}
elseif($dayss>=7){
    $result = Discount::take(1)->get();
    log::info($result);
    return to_array($result);

}
else{
    log::info('else 1st $result');  
    $result = Discountmsg::take(1)->get();
    log::info($result);
    return to_array($result);

}

     }
     else{
        if($dayss<7)
        {
        log::info('else 1st $result');  
        $result = Discountmsg::take(1)->get();
        log::info($result);
        return to_array($result);
    }


    }

}
Enter fullscreen mode Exit fullscreen mode
  • empty condition on first.

condition
Image description

  • condition on take.

condition
Image description

Event.php

get_latest_events()

  • leftJoin
  • select
  • orWhere
  • selectRaw
  • first.

condition
Image description

save_event()

updateOrCreate

condition
Image description

How to Store Array in Database Laravel?

Refer here

event_tags

one event has many tag

  • use of sync in laravel relationship.

condition
Image description

events()

  • Search based on event name,state,city,category(dropdown),date ,price (dropdown),country (dropdown)

  • whereRaw

  • whereDate

  • !empty($params['search']

  • LIKE.

condition

  • Incase of input type search
  • Incase of dropdown country,state,city
  • Incase of price

  • Incase of input type search
    Image description

  • Incase of dropdown country,state,city
    Image description

  • Incase of price
    Image description

 public function events($params  = [])
    {   
        $query = Event::query(); 
        log::info('yaha evenst fun');

        log::info($params['search']);

        $query
        ->leftJoin("categories", "categories.id", '=', "events.category_id")
        ->select(["events.*", "categories.name as category_name"]);

        if(!empty($params['search']))    
        {
            $query
            ->whereRaw("( title LIKE '%".$params['search']."%' 
                OR venue LIKE '%".$params['search']."%' OR state LIKE '%".$params['search']."%' OR city LIKE '%".$params['search']."%' OR slug3 LIKE '%".$params['search']."%')");
        }


        $query
        ->selectRaw("(SELECT CN.country_name FROM country CN WHERE CN.country_id = events.country_id) country_name")
        ->selectRaw("(SELECT SN.state_name FROM states SN WHERE SN.state_id = events.state_id) state_name")
        ->selectRaw("(SELECT CITYN.city_name FROM cities CITYN WHERE CITYN.city_id = events.city_id) city_name")
        ->selectRaw("(SELECT SD.repetitive_type  FROM schedules SD WHERE SD.event_id = events.id limit 1 ) repetitive_type");

        if(!empty($params['category_id']))
            $query->where('category_id',$params['category_id']);

        if(!empty($params['country_id']))
            $query->where('country_id',$params['country_id']);

            if(!empty($params['state_id']))
            $query->where('state_id',$params['state_id']);

            if(!empty($params['city_id']))
            $query->where('city_id',$params['city_id']);

        if(!empty($params['start_date']) && !empty($params['end_date']))
        {
            $query->whereDate('start_date', '>=' , $params['start_date']);
            $query->whereDate('start_date', '<=' , $params['end_date']);
        }

        if(!empty($params['start_date']) && empty($params['end_date']))
            $query->whereDate('start_date', $params['start_date']);


        if(!empty($params['price']))
        {
            if($params['price'] == 'free')
                $query->where('price_type', "0" );

            if($params['price'] == 'paid')
                $query->where('price_type', 1);    
        }

        $query
        ->where(["events.status" => 1, "events.publish" => 1, "categories.status" => 1]);

        // if hide expired events is on


    if(!empty(setting('booking.hide_expire_events')))
    {
        $today  = \Carbon\carbon::now(setting('regional.timezone_default'))->format('Y-m-d');    
        $query->whereRaw('(IF(events.repetitive = 1, events.end_date >= "'.$today.'", events.start_date >= "'.$today.'"))');
    }

    return $query->orderBy('events.updated_at', 'DESC')->paginate(9);
}
Enter fullscreen mode Exit fullscreen mode

get_featured_events

whereDate and Carbon::today()->toDateString()
Image description

get_top_selling_events

orderBy(total_booking),
SUM(BK.quantity) as total_booking
whereDate and Carbon::today()->toDateString()

Image description

if user id in request parameter then use query methods

get_allevent_by_category

  • double where Image description

get_upcomming_events

  • whereDate with Carbon::now() with respect to start and end date Image description

get_cities_events

Image description
Image description
Image description

get_organizers||get_customers

$query->select('name', 'id', 'email')
LIKE condition with where and orwhere inside !empty($search)
Image description

search_customers

Image description

disable_event

Image description

get_all_events

get all my evenst for admin using
$query->where and $query->get()
Image description

total_event

model with count with or without where
Image description

iternary.php

** get_days**
Image description

mydiscount.php

insertselect_discount||insert_discount
Image description

discount_title.php

get_discountweek||get_discountweek_second
Image description
Image description
Image description
Image description

rvsp.php

get_my_bookevents

  • use of select Image description

Ticket.php

add_ticket

  • at same time update from two model
  • update Image description

get_event_tickets

  • use of with
  • use of where and wherein Image description

get_events_tickets

  • whereIn Image description

User.php

all_operator_user

  • double where (both condition should be true) Image description

total_customers||total_organizers

  • count with where Image description

Top comments (0)