$dept_name = DB::table('departments')->select('dept_name')->where('id', '=', $request->alloted_dept)->get();
strong text**dd ($dept_name);
I want to print only department name but in browser it's showing
output:
array:1 [▼
0 => {#269 ▼
+"dept_name": "IPS department"
}
]
On echo $dept_name it outputs
ErrorException in abcController.php line 34: Array to string conversion
I want to output only "IPS Department".
You have to loop your results
$dept_name = DB::table('departments')->select('dept_name')->where('id', '=', $request->alloted_dept)->get();
Controller
foreach($dept_name as $name) {
echo $name;
}
View
#foreach($dept_name as $name)
{{ $name }}
#endforeach
I did it like this:
$dept_name = DB::table('departments')->select('dept_name')->where('id', '=', $request->alloted_dept)->get();
$dept = "";
foreach($dept_name as $name) {
$dept = $name->dept_name;
}
echo $dept;
Output:
IPS department
Thanks for answering Saravanan Sampathkumar
Related
i'm try to foreach inside the foreach to get the count data with this code
Controller.php
$jadwals = DB::table('tb_jadwal')->get();
foreach ($jadwals as $key => $jadwal) {
$tgl = date("Y-m-d", strtotime($request->tgl));
$cek_tiket = array();
$cek_tiket = DB::table('tb_tiket')
->select(DB::raw('count( distinct id_tiket) as jumlah_kapasitas'))
->where('Tgl_Berangkat', '=', "'$tgl'")
->where('id_jadwal', '=', $jadwal->id_jadwal)
->get();
$jadwals[$key]->jumlah_kapasitas = $cek_tiket;
}
view.blade.php
#foreach($cek_tiket as $cek_tiket)
<?php
if ($cek_tiket->jumlah_kapasitas <= 70) {
echo "There data";
dd($cek_tiket);
}
?>
#endforeach
but the result of dd is 0
I am getting the problem when i fetch categorey name from categorey table ..
code here ..
Thanks.
public function viewProducts(){
$products = Product::get();
$products = json_decode(json_encode($products));
foreach($products as $key => $val){
$category_name = Category::where(['id'=>$val->category_id])->first();
$products[$key]->category_name= $category_name->name;
}
echo "<pre>";print_r($products);die;
return view('admin.products.view_products')->with(compact('products'));
}
ErrorException (E_NOTICE)
Trying to get property of non-objectenter image description here
The error is straight forward. You haven't gotten any objects of $category_name variable and you're trying to access to the value of the name property. Which is not defined because there's no object.
Add a simple condition to fix it like this
public function viewProducts(){
$products = Product::get();
$products = json_decode(json_encode($products));
foreach($products as $key => $val){
$category_name = Category::where(['id'=>$val->category_id])->first();
if($category_name != null){
$products[$key]->category_name= $category_name->name;
}
}
echo "<pre>";print_r($products);die;
return view('admin.products.view_products')->with(compact('products'));
}
To send data to your blade:
# Controller:
$data['category_name'] = $category_name;
$data['products'] = $products;
->withKeyName($data);
# Blade:
{{ $KeyName['category_name'] }}
{{ $KeyName['products'] }}
I want to work on a while loop in a foreach loop but I can't convert it to Laravel controller system. Please help.
foreach (range('a', 'z') as $i)
{
echo $i = strtolower($i);
$sql = "SELECT * FROM `list` Where name like '$i%' Limit 30";
$result = mysqli_query($con, $sql);
echo"<hr><h3>Name Starting with ".strtoupper($i)."</h3>";
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$id = $row['id'];
$name = $row['name'];
echo "<li>".$row['name']."</li>";
}
echo "<li>More</li>";
echo "</ul>";
}
The code in Laravel that I have is
foreach (range('a', 'z') as $i)
{
$list = DB::table('list')->where('name', 'LIKE', $i.'%')->limit(30)->get();
return view('pages.all', ['list' => $list]);
}
This code only gives data for names starting with alphabet "a" but won't proceed with the rest of the characters.
Store it on the array based on your search.
$list = array();
foreach (range('a', 'z') as $i) {
$list[$i] = DB::table('list')->where('name', 'LIKE', $i.'%')->limit(30)->get();
}
return view('pages.all', ['list' => $list]);
Customize your view, to showing the list against each character (a-z).
[Edit]
View file can be changed like below,
#foreach ($list as $keyI => $moredata)
<hr>
<h3>Author's Name Starting with "{{strtoupper($keyI)}}"</h3>
<ul class="tags-list">
#foreach ($moredata as $data)
<li>{{$data->name}}</li>
#endforeach
</ul>
#endforeach
SettingController.php
public function SendPicture(Request $request) {
$title = "Picture Purchase";
$domain = $_SERVER['SERVER_NAME'];
$email = DB::table('users') - > where('domain', $domain) - > get();
$content = "$email purchase your picture : ";
foreach($request - > input('pic') as $key => $value) {
$content. = "$value".".jpg ";
}
}
ErrorException in SettingController.php line 373: Array to string
conversion
line 373: $content = "$email purchase your picture : ";
If you've only one record you can use value method for getting direct string:
$email = DB::table('users')->where('domain', $domain)->value('email');
$content = $email ." purchase your picture : ";
Note : There is a logical error in your each loop too, remove "" from $value :
foreach($request->input('pic') as $key => $value) {
$content. = $value.".jpg ";
}
$email variable have more then one record. If you use $email as email address from users table then you can follow below code :
$email = DB::table('users')->where('domain', $domain)->first();
$content = $email."purchase your picture : ";
Hope this help for you!!!
I am using $data = $query->result_array(); but I would need to change my fields name from first_name to first name.
I looked up found this....
foreach ($query->result_array() as $row)
{
echo $row['title'];
echo $row['name'];
echo $row['body'];
}
would I want to do something like this?
foreach ($query->result_array() as $row)
{
$row = str_replace("_", " ", $row);
}
$data = $row;
if you need then a new array you can easly do:
foreach ($query->result_array() as $key=> $value)
{
$data[] = array(
str_replace("_", " ", $key)=>$value
);
}