Count rows with model::count laravel [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last year.
Improve this question
I am trying to count the records in the table, but it's showing me nothing in the response, I am working on an API, can anybody help please ?
public function devicesCount()
{
$x = Device::count();
if ($x) {
return ['devices' => $x];
} else {
return ['message' => 'ranii karrezt ydyyyyyyk'];
}
}

use $x = Device::all()->count(); in place of $x = Device::count();
or use Laravel Aggregates like
$count = DB::table('devices')->count();

Related

Json data synchronization in database [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I keep json data from database. I want to sync with the name that comes with the post and get the price of the data I sync. How can I do that?
$video_tkn = "3D";
$reklam_json = json_decode($siteayar->reklam_json);
$video = json_decode($reklam_json->video);
$video_arr = array();
foreach ($video as $v) {
if ($v->video_tur==$video_tkn) {
$video_arr[] = $v->fiyat;
}else{
$video_arr[] = 0;
}
}
print_r($video_arr[0]);
//Output : 0
//$reklam_json->video
[
{
"video_tur":"2D",
"fiyat":"20"
},
{
"video_tur":"3D",
"fiyat":"80"
}
]
You can do something like. The problem with the code that you have written is that in the $video_arr[0] it is always writing 0 as per your else condition because $video_tkn= "3D" value is in second index in the array $video
<?php
$video_tkn = "3D";
$reklam_json = json_decode($siteayar->reklam_json);
$video = json_decode($reklam_json->video);
$video_arr = array();
foreach ($video as $v) {
if ($v->video_tur==$video_tkn) {
$video_arr[] = $v->fiyat;
}
}
print_r($video_arr[0]);

Foreach loop php conversion [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I execute on my main page some php code which includes :
foreach ($fbdata->feed->data as $fbpost)
{
...
}
How can we convert this , into a loop that goes from (i to z)(0 to 10) ?
Simple for loop
for($i = 0; $i < 10; $i++) {
$fbpost = $fbdata->feed->data[$i];
...
}
or if you like to use the as, try using a foreach but slicing the array before using it
$fbPosts = array_slice($fbdata->feed->data, 0, 10);
foreach($fbPosts as $fbpost) {
...
}

How to do maximum array intersection PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I ask a candidate : What do you like ?
His answer is an array [Math,Physics,Chemistry,IT,Sports]
I do have a list :
Software Analyst : [Math,IT,Programing,Stress,Computer]
Truck drive : [Sports,Driving,Traveling,Exchange]
Physic-chemistry teacher :[Math,Physics,Chemistry,Teaching]
What is the best way in PHP to get the jobs with the maximum common tags with the user input?
You use array_intersect() to find the common tags, then compare among the returned tags amount.
$jobs = array(
'software' => array('Math','IT','Programing','Stress','Computer')
'truck' => array('Sports','Driving','Traveling','Exchange')
'physic' => array('Math','Physics','Chemistry','Teaching')
);
$user_tags= array('Math','Physics','Chemistry','IT','Sports');
$max_common = 0;
$desired_job = '';
foreach($jobs as $key=>$job) {
$common = count(array_intersect($job,$user_tags);
if(max_common < $common) {
$desired_job = $key;
$max_common = $common;
}
}

Php Calculating two functions [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am writing a program in which I want to plus two functions together and in the end I want to calculate the total. if anyone can help with this it will be appreciated.
const PRICE_OF_Big_Mac = 2.50;
public $Big_Mac = 0;
public function calculate_dinein_price(){
$dine = 0.00;
$dine += $this->Big_Mac * self::PRICE_OF_Big_Mac;
return $dine;
}
public function calculate_dinein_total(){
$total_dine = 0.00;
$total_dine = $total_dine + $dine;
return $total_dine;
}
but this doesn't work.
You can't access $dine in your second function because it's nowhere defined. The $dine from your first function is only a local variable.
I would suggest this solution which uses the fact that calculate_dinein_price() also returns the value of $dine:
public function calculate_dinein_total(){
$total_dine = 0.00;
$total_dine = $total_dine + $this->calculate_dinein_price();
return $total_dine;
}

condition statement doesn't work [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
i'm trying to do a simple condition check like:
$test = 1;
$test_ = $test==-1?"Test--1":$test==1?"Test-1":$test==0?"Test-0":"Test";
echo $test_;
The output is Test-0 but must be Test-1.
Which could be the problem?
Try this : Put brackets for conditions.
$test = 1;
$test_ = ($test==-1)?"Test--1":($test==1?"Test-1":($test==0?"Test-0":"Test"));
echo $test_;
output :
Test-1
change this
$test_ = $test==-1?"Test--1":$test==1?"Test-1":$test==0?"Test-0":"Test";
to
$test_ = $test==-1?"Test--1":($test==1?"Test-1":($test==0?"Test-0":"Test"));
working example http://viper-7.com/msuCyk
// output Test-1
Ternary operator in PHP is left to right, therefore, its executed like this:
$test_ = (( ( $test==-1 ? "Test--1" :$test==1) ? "Test-1":$test==0)?"Test-0":"Test");

Categories