Laravel - Conditional not working on blade - laravel

this problem is killing my head and I can not get a solution
Laravel version: 5.5.47
I have the most simple if statement:
#foreach($order->ship as $os)
#if($os->destination_info != 0)
{{ $os->destination_info }}
#endif
#endforeach
For some reason this does not print anything. I have tried all the options. If I just print the variable without the #if it gives me Barcelona-Spain so the the conditional is correct it should print
This is my $order->ship:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[ship_id] => 92
[order_id] => 593
[origin_id] => 11
[origin_info] => Shenzhen - China
[destination_id] => 22
[destination_info] => Barcelona - Spain
[cbm] => 3.4151
[container] => C
[container_load_date] => 1566489600
[container_arrival_date] => 1567180800
[container_number] => 10009000
[price] => 53.5
[status] => 1
[time] => 1565842730
)
[1] => stdClass Object
(
[ship_id] => 93
[order_id] => 593
[origin_id] => 15
[origin_info] => Guangzhou - China
[destination_id] => 22
[destination_info] => Barcelona - Spain
[cbm] => 4.736
[container] => C
[container_load_date] => 1566489600
[container_arrival_date] => 1566576000
[container_number] => 10009000
[price] => 52.85
[status] => 1
[time] => 1565842730
)
)
)

As per your array representation, destination_info is a string and you are comparing it as a number. you can try using isset or is_null function with a not operator. it will help you
#foreach($order->ship as $os)
#if(!is_null($os->destination_info))
{{ $os->destination_info }}
#endif
#endforeach
You can use PHP isset
#foreach($order->ship as $os)
#if(isset($os->destination_info))
{{ $os->destination_info }}
#endif
#endforeach
You can read more about isset and is_null from php official document.

Related

Laravel Trying to Get Property of Non-Object - Display Data from Different Table using API

I have two table:
1) ip status table
2) status table
In the database, status's id is the foreign key for ip status table. I want to display 'whitelist' instead of '2'
ip status table:
+==============+===============+============+
| ip_status_id | ip_address | status_id |
+==============+===============+============+
| 1 | 192.108.1.1 | 2 |
+--------------+---------------+------------+
status table:
+============+================+
| status_id | status |
+============+================+
| 1 | Blacklist |
+------------+----------------+
| 2 | Whitelist |
+------------+----------------+
and I call it through API with query like this:
$stmt = $db->prepare("SELECT * FROM tbl_ip_statuses left join
tbl_status on tbl_ip_statuses.status_id = tbl_status.status_id
WHERE tbl_ip_statuses.is_active = ?
AND tbl_ip_statuses.is_deleted = ?
group by tbl_ip_statuses.ip_status_id");
$stmt->bind_param("ii", $_GET["is_active"], $_GET["is_deleted"]);
And I do checking at controller, the data fetch exactly like I want
Here is my controller:
$listing = json_decode($ip_status);
//dd($listing);
return view('ip_status.listing')->with("ip_status", $listing);
Here is my view:
#if($ip_status)
#foreach($ip_status as $data)
<td class="uk-width-1-2">{{ $data->s_ip }}</td>
<td class="uk-width-1-2">{{ $data->s_status }}</td>
But then it show trying to get property s_ip of non object.
when I print_r($listing)
stdClass Object (
[ip_statuses] => Array (
[0] => stdClass Object (
[ip_status_id] => 1
[s_ip] => 192.108.1.1
[status_id] => 2
[is_active] => 1
[ip_status_id_create] => 0
[dt_create] => 2020-04-15 17:16:06
[ip_status_id_edit] => 0
[dt_edit] =>
[is_deleted] => 0
[ip_status_id_delete] => 0
[dt_delete] =>
[s_status] => Whitelist
[status_id_create] => 0
[status_id_edit] => 0
[status_id_delete] => 0
)
[1] => stdClass Object (
[ip_status_id] => 2
[s_ip] => 1922.131.100.1
[status_id] => 1
[is_active] => 1
[ip_status_id_create] => 0
[dt_create] => 2020-04-15 17:15:29
[ip_status_id_edit] => 0
[dt_edit] =>
[is_deleted] => 0
[ip_status_id_delete] => 0
[dt_delete] =>
[s_status] => Blacklist
[status_id_create] => 0
[status_id_edit] => 0
[status_id_delete] => 0
)
[2] => stdClass Object (
[ip_status_id] => 3
[s_ip] => 1.1.1.1
[status_id] => 2
[is_active] => 1
[ip_status_id_create] => 0
[dt_create] => 2020-04-15 17:16:06
[ip_status_id_edit] => 0
[dt_edit] =>
[is_deleted] => 0
[ip_status_id_delete] => 0
[dt_delete] =>
[s_status] => Whitelist
[status_id_create] => 0
[status_id_edit] => 0
[status_id_delete] => 0
)
)
)
ip_statuses in an array as I see from dd result,
You can access s_ip an other elements like this $listing[‘s_ip’]
Update:
Your data is reaching blade as one ‘ip_statuses’ which have array items inside, so you need to make a change in controller where you send data to view:
return view(...)->with(‘ip_status’, $listing->ip_statuses);

how to make multi dimension array from sql return rows in codeigniter

how to make multi dimension array from sql return rows in codeigniter.
return values from model holding all values in $res.
$res = $this->user_model->get_room_book_join(['rooms.hotel_id' => 1]);
if ($res) {
echo '<pre>';
print_r($res);
}
I getting this type of array.this type can not helping me.
Array( [0] => Array
(
[room_id] => 1
[room_no] => 101
[room_desc] => Double Bed Deluxe Non Air Conditioned
[status] => available
[category_id] => 1
[hotel_id] => 1
[tariff_type] => normal
[room_rate] => 1000
[persons] => 0
[date_start] => 0000-00-00
[date_end] => 0000-00-00
[overview_id] => 1
[rom_id] => 1
[hot_id] => 1
[cus_id] => 2
[bok_id] => 2
[dates] => 2017-04-12
)
[1] => Array
(
[room_id] => 2
[room_no] => 101
[room_desc] => Double Bed Deluxe Non Air Conditioned
[status] => available
[category_id] => 1
[hotel_id] => 1
[tariff_type] => normal
[room_rate] => 1000
[persons] => 0
[date_start] => 0000-00-00
[date_end] => 0000-00-00
[overview_id] => 1
[rom_id] => 1
[hot_id] => 1
[cus_id] => 2
[bok_id] => 2
[dates] => 2017-04-13
)
)
Actually I want this type of array.how can i make it.please help advance thanks.
array(
[0]=>array(
[room_id]=>1
[dates]=>array(
[0]=>2014-04-12
[1]=>2014-04-13
)
)
[1]=>array(
[room_id]=>2
[dates]=>array(
[0]=>2014-04-12
[1]=>2014-04-13
)
)
)
Hi, you can try this code, this may help you
$query = $this->db->get();
$result = $query->result_array();
$main_array=array();
$actual_array=array();
foreach($result as $res){
$main_array['room_id']=$res['room_id'];
$main_array['dates']=array($res['date_start'],$res['date_end']);
$actual_array[]=$main_array;
}
//echo ""; print_r($actual_array);
return $actual_array;
try this
// after if ($res) {
for ($i=0; $i < count($res); $i++) {
$res[$i] = array(
'room_id' => $res[$i]['room_id'],
'dates' => array($res[$i]['date_start'],$res[$i]['date_end'])
);
}

How can I sort an array by specific value containing class object inside it?

I want to sort this array by product price in ascending order. How can I do this?
Array
(
[0] => stdClass Object
(
[product_id] => 16
[product_name] => Ferrari 4802
[product_details] => I know this car is good. Because I have driven this car 10 year.
[product_quantity] => 1
[product_price] => 2560
[shipping_cost] => 1000
[product_image] => ./admin_assets/image/uploads/product_image/ea28444f93d1fea413c95861bf306fbf.jpg
[product_details_image] => ./admin_assets/image/uploads/product_details/ea28444f93d1fea413c95861bf306fbf.jpg
[thumbnails_image] => ./admin_assets/image/uploads/thumbnails/ea28444f93d1fea413c95861bf306fbf.jpg
[slidder_image] => ./admin_assets/image/uploads/slidders/ea28444f93d1fea413c95861bf306fbf.jpg
[status] => 1
[slidder] =>
[category_id] => 4
)
[1] => stdClass Object
(
[product_id] => 15
[product_name] => Laborghini urus
[product_details] => I know this car is good. Because I have driven this car 10 year.
[product_quantity] => 2
[product_price] => 500
[shipping_cost] => 1000
[product_image] => ./admin_assets/image/uploads/product_image/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[product_details_image] => ./admin_assets/image/uploads/product_details/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[thumbnails_image] => ./admin_assets/image/uploads/thumbnails/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[slidder_image] => ./admin_assets/image/uploads/slidders/68a7eb1c6bf5b3b43f0c3fd4ed143c68.jpg
[status] => 1
[slidder] =>
[category_id] => 4
)
[2] => stdClass Object
(
[product_id] => 14
[product_name] => Laborghini hurican023
[product_details] => I know this car is good. Because I have driven this car 10 year.
[product_quantity] => 4
[product_price] => 50
[shipping_cost] => 1000
[product_image] => ./admin_assets/image/uploads/product_image/65e660ef6e3ebcb0773d87c048c93e1b.png
[product_details_image] => ./admin_assets/image/uploads/product_details/65e660ef6e3ebcb0773d87c048c93e1b.png
[thumbnails_image] => ./admin_assets/image/uploads/thumbnails/65e660ef6e3ebcb0773d87c048c93e1b.png
[slidder_image] => ./admin_assets/image/uploads/slidders/65e660ef6e3ebcb0773d87c048c93e1b.png
[status] => 1
[slidder] =>
[category_id] => 4
)
)
Because the array is generated by a database query you can easily have the query do the sorting for you.
You don't show your model code but assuming you are using Query Builder (a.k.a. Active Record) you need the following in your model before you execute $this->db->get().
$this->db->order_by('product_price', 'ASC');
Here is some example code showing a full query. It assumes the table is named 'products', you want to select all the table's fields, and you want only a particular 'category_id'.
In the Model file:
public function product_by_category($category)
{
$query = $this->db
->where('category_id', $category)
->order_by('product_price', 'ASC')
->get('products');
//return all records or NULL if no records are found
return $query->num_rows() > 0 ? $query->result() : NULL;
}

Storing booking items information in session array of codeigniter

I wanted to store booking items such as
--> pid =1, qty=3
--> pid=5, qty=4
and so on
in codeigniter session.
I am finding it difficult and i am running out of deadline. Helping hands are very much appreciated.
I have made an example special for you. Suppouse it will helps
$data = array();
for ($i = 1; $i<=5; $i++)
{
$data[] = array('pid' => $i, 'qty' => $i);
}
$this->session->set_userdata('order_data', $data);
and output your result will be shown in session like these :
[order_data] => Array
(
[0] => Array
(
[pid] => 1
[qty] => 1
)
[1] => Array
(
[pid] => 2
[qty] => 2
)
[2] => Array
(
[pid] => 3
[qty] => 3
)
[3] => Array
(
[pid] => 4
[qty] => 4
)
[4] => Array
(
[pid] => 5
[qty] => 5
)
)
If you want to call only these data (booking data) then call
$this->session->userdata['order_data']
you can free use it in foreach for view like
foreach ($this->session->userdata['order_data'] as $key => $value)
{
}

count number of foreign key in doctrine

This is follow up of this question: count number of foreign keys
I am using doctrine 1.2 and I want to count the number of business that belongs to subcategory.
Following are the mysql tables.
1.fi_category
+----+-----------------+-----------------+
| id | name | slug |
+----+-----------------+-----------------+
2.fi_subcategory
+----+-----------------+-----------------+-------------+
| id | name | slug | category_id |
+----+-----------------+-----------------+-------------+
3.fi_business_subcategory
+----+-------------+----------------+
| id | business_id | subcategory_id |
+----+-------------+----------------+
I am using this DQL.
$q = Doctrine_Query::create()
->select('c.name, c.slug, sc.name, sc.slug')
->from('Model_Category c')
->leftJoin('c.Subcategory sc')
->leftJoin('sc.BusinessSubcategory bsc');
which gives me something like this.
Array
(
[0] => Array
(
[id] => 1
[name] => Entertainment & Lifestyle
[slug] => entertainment-lifestyle
[Subcategory] => Array
(
[0] => Array
(
[id] => 1
[name] => Arts and Crafts
[slug] => arts-and-crafts
)
[1] => Array
(
[id] => 2
[name] => Family
[slug] => family
)
[2] => Array
(
[id] => 3
[name] => Fashion
[slug] => fashion
)
)
)
)
I am looking to fetch the number of business, i.e the returned result should be something like this depending on the business it belongs.
Array
(
[0] => Array
(
[id] => 1
[name] => Entertainment & Lifestyle
[slug] => entertainment-lifestyle
[Subcategory] => Array
(
[0] => Array
(
[id] => 1
[name] => Arts and Crafts
[slug] => arts-and-crafts
[business_count] => 35
)
[1] => Array
(
[id] => 2
[name] => Family
[slug] => family
[business_count] => 10
)
[2] => Array
(
[id] => 3
[name] => Fashion
[slug] => fashion
[business_count] => 27
)
)
)
)
Tried various ways using DQL, but nothing seems to work out. Any idea how should I go with what I want?
Did you try:
$q = Doctrine_Query::create()
->select('c.name, c.slug, sc.name, sc.slug, COUNT(sc.BusinessSubcategory.id) AS business_count')
->from('Model_Category c')
->leftJoin('c.Subcategory sc')
->groupBy('c.id, sc.id');
with HYDRATE_ARRAY

Resources