How to echo object name of an array in Laravel controller - php

how do I display an object name of an array that I fetched from the database in Laravel blade?
When I dump array in Laravel controller I get this "name" => "{"name":"Item 1"}". Now, how do extract Item 1 from array and display in a blade while looping using foreach.
Controller
$data = DB::table('items')
->select('name')
->where('id', $user->id)
->get();
$output = json_decode($data,true);
dd($output);
Results
"name" => "{"name":"Item 1"}
Kindly help.

You have to extract from each object separately.
Variant 1:
Controller:
$data = DB::table('items')->select('name')->where('id', $user->id)->get()
->map(function($item){
$item->name = json_decode($item->name, true);
return $item;
});
return view('some_view', ['data' => $data]);
View:
#foreach($data as $d)
<p>{{ is_array($d->name) ? $d->name['name'] : 'No Name' }}</p> {{-- Just Checking if Json is correctly decoded --}}
#endforeach
Variant 2:
Controller:
$data = DB::table('items')->select('name')->where('id', $user->id)->get();
View:
#foreach($data as $d)
#php($name = json_decode($d->name, true))
<p>{{ is_array($name) ? $name['name'] : 'No Name' }}</p> {{-- Just Checking if Json is correctly decoded --}}
#endforeach

Controller:
$dataes = DB::table('items')
->select('name')
->where('id', $user->id)
->get();
return view('viewName',compact('dataes'));
View:
#foreach($dataes as $data)
{{ $data->name }}
#endforeach

Related

Laravel 5.7 How to get data from Controller show($id) function? Used Query builder

StaffController
public function show($id){
$staffinfo = DB::table('staff')->where('user_id', $id)->get();
return view('staff.view')->with('staffinfo', $staffinfo);
}
view.blade.php
<h1>{{$staffinfo->name}}</h1>
<p>{{$staffinfo->user_id}}</p>
Is this code right to show data from staff table in view by show($id) function?
Getting error:
"Property [name] does not exist on this collection
instance. (View:
F:\xampp\htdocs\gchsc\resources\views\staff\view.blade.php)"
Switch ->get() to ->first(). $staffInfo is a Collection of database records, not a single one:
StaffController.php
$staffinfo = DB::table('staff')->where('user_id', $id)->first();
Then the following will work in your view:
staff/view.blade.php
<h1>{{ $staffinfo->name }}</h1>
<p>{{ $staffinfo->user_id }}</p>
Or, leave your code as is and iterate in your view:
StaffController.php
$staffinfo = DB::table('staff')->where('user_id', $id)->get();
staff/view.blade.php
#foreach($staffInfo AS $staff){
<h1>{{ $staff->name }}</h1>
<p>{{ $staff->user_id }}</p>
#endforeach

parse error when trying to display in laravel view

I get this error whenever I'm trying to display items from database in my views.
Undefined variable: manufacturer (View:.../index.blade.php)
in my controller:
public function index(Request $request){
$title = array('pageTitle' => Lang::get("website.Home"));
$result = array();
$manufacturers = DB::table('manufacturers')
->leftJoin('manufacturers_info','manufacturers_info.manufacturers_id', '=', 'manufacturers.manufacturers_id')
->select('manufacturers.manufacturers_id as id', 'manufacturers.manufacturers_image as image', 'manufacturers.manufacturers_name as name', 'manufacturers_info.manufacturers_url as url', 'manufacturers_info.url_clicked', 'manufacturers_info.date_last_click as clik_date')
->get();
$result['manufacturers'] = $manufacturers;
return view("index", $title)->with('result', $result); }
my views:
{{ $manufacturer->name }}
help would be much appreciated.
Thank You.
If you're putting the manufacturers array in the $result and passing the $result then you have to reference it by $result in your view.
$result['manufacturers'] = $manufacturers;
return view("index", $title)->with('result', $result);
Something like...
#foreach ($result['manufacturers'] as $manufacturer)
{{ $manufacturer->name }}
#endforeach
in the Controller:
$result['manufacturers'] = $manufacturers;
return view('index', [
$result => $result,
]);
in the view:
<ul>
#foreach ($result['manufacturers'] as $manufacturer)
<li>{{ $manufacturer->name }} </li>
#foreach
</ul>

display live json data on laravel blade

i have live json and i want to display it on laravel blade
my controller code is :
public function index()
{
$json = json_decode(file_get_contents('link_of_json_file'),true);
return view('show' , compact('json'));
}
and blade code is :
#foreach ($json as $p)
{{ $p->name }}
#endforeach
and this erros is display:
(2/2) ErrorException
Trying to get property of non-object (View:
E:\work-landingpage\test\JSON\resources\views\show.blade.php)
you can remove the "true" to use it as object :
public function index()
{
$json = json_decode(file_get_contents('link_of_json_file'));
return view('show' , compact('json'));
}
#foreach ($json as $p)
{{ $p->name}}
#endforeach
so if you want to use it as array you can use it like:
#foreach ($json as $p)
{{ $p['name']}}
#endforeach

Getting data out of array column in laravel 5.5

I save my order data to notifications table as my notification using laravel Notification and I have problem with returning my data.
here is my notification method: App\Notifications\OrderSubmited.php
public function toArray($notifiable)
{
return [
'order_id' => $this->order->id,
'title' => $this->order->title,
'buyer' => $this->order->user_id,
'status' => $this->order->orderstatus_id,
];
}
My method to get data:
View::composer('admin.dashboard', function ($view) {
$notifications = DB::table('notifications')->orderby('id', 'desc')
->latest()
->take(5)
->get();
$notifs = $notifications;
$view->with('notifs', $notifs);
});
here is my blade code:
#foreach($notifs as $notif)
{{dd($notif)}}
#foreach($notif->data as $data)
{{$data['order_id']}} <br>
#endforeach
#endforeach
this is my {{dd($notif)}} output:
{#721 ▼
+"id": "46546547464415474884"
+"type": "App\Notifications\OrderSubmited"
+"notifiable_id": 2
+"notifiable_type": "App\Order"
+"data": "{"order_id":2,"title":"tsdfg", "user_id":"1", "orderstatus_id":"11"}"
+"read_at": null
+"created_at": "2018-01-18 00:00:00"
+"updated_at": "2018-01-18 00:00:00"
}
as you can see data i try to return are stored in data column.
If I remove my dd i will get this error:
Invalid argument supplied for foreach()
On this line:
#foreach($notif->data as $data)
Any idea?
Each notification is an object containing an array called data containing the values you return from your toArray method when creating the notification. You would normally iterate through your notifications and access their data like so:
#foreach ($notifications as $notification)
{{ $notification->data['order_id'] }}
#endforeach
Laravel converts the data property (which is stored in the database as JSON) into an array.
However because you are retrieving the notifications yourself from the database without making use of the built in methods you will need to convert that data to an array from JSON yourself, like so:
View::composer('admin.dashboard', function ($view) {
$notifications = DB::table('notifications')->orderby('id', 'desc')
->latest()
->take(5)
->get();
$view->with('notifications', $notifications);
});
Then from within your view:
#foreach ($notifications as $notification)
#php $data = json_decode($notification->data, true); #endphp
{{ $data['order_id'] }}
#endforeach
Regarding your follow up question, something like this:
View::composer('admin.dashboard', function ($view) {
$notifications = DB::table('notifications')->orderby('id', 'desc')
->latest()
->take(5)
->get()
->map(function ($item, $key) {
$item->data = json_decode($item->data);
$item->data->user = User::find($item->data->user_id);
return $item;
});
$view->with('notifications', $notifications);
});
Then from within your view:
#foreach ($notifications as $notification)
{{ $notification->data->user->name }}
#endforeach

Laravel Paginate not working

I was trying to paginate some data with Query builder with the following query.
public function productSearch(Request $request)
{
$name = $request->name;
$products = DB::table('products')
->where('name', 'LIKE', "%$name%")
->paginate(4);
return view('product',compact('products');
}
But when I tried to show the pagination in the view page with the following line
{{ $products->links() }}
It shows
Method links does not exist
What could the possible error be since the pagination does not show?
2 types to print pagination link in laravel -
Try with -
use Illuminate\Support\Facades\DB;
public function productSearch(Request $request)
{
$name = $request->name;
$products = DB::table('products')
->where('name', 'LIKE', "%$name%")
->paginate(4);
return view('product',['products' => $products]);
}
In View -
<div class="container">
#foreach($products as $product)
<h4>{{ $product['name'] }}</h5> <br>
#endforeach
</div>
1st type to defined laravel pagination link -
{{ $products->links() }}
And 2nd type to defined laravel pagination link -
{{ $products->render(); }}
Note - Also you missed in your code -> return view('product',compact('products')); in return view('product',compact('products');
Here is your solution.
Just change your return view option
return view('product')->with('products', $products);
Hope this helps you.
Change to :
return view('product',compact('products'));
In View page try :
{{$products->render()}}
In your Controller:
return view('product',compact('products');
In your View:
#foreach($products as $product)
{{ $product->name }}
#endforach
{{ $products->links() }}
try with:
{{ $products->render() }}

Categories