In code Controller recieving data from model but failed to show in blade view file.
This is blade view code.
#for($id = 0;$id < 59;$id++)
#foreach($data[$id] as $data)
{{$data->name}}
#endforeach
#endfor
Here is the controller
public function cart(){
$data = array();
$data['flashSale'] = Product::flashSale();
$cartProdId = Session::get('prodId');
for ($id = 0;$id<sizeof($cartProdId);$id++){
$data[$id] = Product::getCartProduct($cartProdId[$id]);
}
return view('user.cart')->with('data',$data);
}
you are using two loops unnecessarily..you can achieve this by using only one loop if it is for or foreach loop...
You can do it by single foreach loop. It will save execution/loading time.
#foreach($data as $key => $value)
{{$value->name}}
#endforeach
Do smart work!! .
Related
In Laravel view Undefined offset: 0
I got problem before says
expecting array in parameter 1 but object given
and fix it through ->toArray. And then appeared this problem
public function index()
{
$products = Product::paginate(3)->toArray();
return view ('inventory.layout', [
'products' => $products
]);
}
And this is to show my products in view file
I've tried to replace the 0 key with ['data'] string but useless.
#if (isset($products))
#if ($arrkeys = array_keys($products[0]))
#foreach ($arrkeys as $key)
<th>{{$key}}</th>
#endforeach
#endif
#endif
If you need to display pagination use code like this
public function index()
{
$products = Product::paginate(3);
return view ('inventory.layout', [
'products' => $products
]);
}
And this is to show my products in view file
I've tried to replace the 0 key with ['data'] string but useless.
#if (isset($products))
#foreach ($products as $key)
<th>{{$key}}</th>
#endforeach
//for pagination
<div>{{$products->links()}}</div>
#endif
If you want to display the keys of the array, try this:
#if (isset($products))
#foreach ($products as $key => $value)
<th>{{$key}}</th>
#endforeach
#endif
But If you want to display the values:
#if (isset($products))
#foreach ($products as $key => $value)
<th>{{$value}}</th>
#endforeach
#endif
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'] }}
So I just learned how to utilize nested iterations to load data from a pivot table. Now I am trying to save the data but I am not able to .
I have a 'new' blade which the customer can create a new 'Order' with many 'products'.
The blade basically just iterates through all available Products. (As far as I can tell, this is how the blade needs to be setup)
new.blade.php
{!! Form::open(['url' => '/orders/store']) !!}
#foreach ($products as $product)
<h1>{{ $product->name }}</h1>
<p>{{ $product->description }}</p>
{{ Form::text('qty', null }}
#endforeach
So this is my attempt at utilizing a nested iteration in order to pass the 'qty' data to the pivot table.
OrderController.php
public function store(Request $request)
{
$user = Auth::user();
$user->load("orders.products"); //eager load pivot table
/*NEW ORDER*/
$order = new Order;
$order->user_id = Auth::user()->id;
$order->save();
$product_id_array = [];
/*PIVOT*/
$products_to_sync_ids = [ 1,2,3,4,5,6,7,8,9 ];
$sync_data = [];
for ($i = 0; $i < count($products_to_sync_ids); $i++) {
$qty = $request->input('qty');
$sync_data[$products_to_sync_ids[$i]] = ['qty' => $qty];
}
$order->products()->sync($sync_data);
The 'Order' saves, but the qty always defaults to the last Products inputted qty.
You are defining text input with same name 'qty' multiple times that's why when you submit the form, it will give you the value of last text field with name 'qty'.
In this case you should define the qty field as an array of like:
{{ Form::text('qty[]', null }}
And while fetching its value you need to iterate it:
$qty = $request->input('qty');
for ($i = 0; $i < count($products_to_sync_ids); $i++) {
$sync_data[$products_to_sync_ids[$i]] = ['qty' => $qty[$i]];
}
This may help you to achieve what you want.
I have a method in laravel that aims to pass data to views. I have data in variables, arrays and objects.
This is the method
public function view($id){
$id = request()->segment(2);
$items = Role::all(['name', 'display_name']);
return view('text.ServicesEditUser',compact('items',$items));
}
I also have this code
$uid = Auth::id();
$settings = Settings::where('user_id','=',Auth::id())->first();
return view('text.settings',compact('settings'));
that i want to incorporate to the code above. Once i pass the settings to the view, i can access data like $settings->language for instance. I also want to pass a variable which is
$title = 'degrees of tilt';
to the first code snippet's compact method.
This is the method now with all the data i want to pass to the view
public function view($id){
$id = request()->segment(2);
$title = 'Tilt in degrees';
$settings = Settings::where('user_id','=',Auth::id())->first();
$users = User::where('id','=',$id)->get();
$items = Role::all(['name', 'display_name']);
return view('text.ServicesEditUser',compact('items',$items));
}
How do i pass $settings ,$title,$users , $items to the view and how do i access each in view?
When returning a view you can pass multiple variables like this.
return view('text.ServicesEditUser')
->with(compact('items',$items))
->with('variable2', $variable2);
See here the documentation about this - Laravel Views.
In controller:
$variable = "Var";
$array = ["first" => "a", "second" => "b"];
$object = (object) $array;
return view('view',compact(['variable', 'array', 'object']));
In view:
{{$variable}}
{{$array['first']}}
{{$object->first}}
compact() doesn't need dollar at start.
You can just add your code to view by:
$title = 'My title';
$settings = ['my', 'settings',];
return view('text.settings', compact('title', 'settings'));
Anyway you can pass your variables by array:
$settings = ['my', 'settings',];
return view('text.settings', [
'title' => 'Title',
'settings' => $settings,
]);
The last option is with() method:
return view('text.settings')
->with('settings', $settings)
->with('title', 'My title');
Get access to variables in the view by {{ $title }} and
#foreach ($settings as $sett)
{{ $sett }}
#endforeach
This did also work
public function view($id){
$id = request()->segment(2);
$title = 'Degrees of tilt';
$settings = Settings::where('user_id','=',Auth::id())->first();
$users = User::where('id','=',$id)->get();
$items = Role::all(['name', 'display_name']);
return view('text.ServicesEditUser', compact('title', 'settings','users','items'));
}
in the blade view
<p>{{$title}}</p>
{{$settings->language}}
#foreach($items as $item)
<option value="{{$item->name}}">{{$item->display_name}}</option>
#endforeach
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->email}}</option>
#endforeach
In settings
#if($settings)
$settings->language
#endif
I have this following code in my controller. I want to print the data i have my array.. should it be double forloop or foreach?
CONTROLLER:
public function index()
{
$in_cart = array();
if (!isset($_SESSION['cartProducts'])){
$in_cart['list'] = "No Products";
}
else{
foreach ($_SESSION['cartProducts'] as $key => $value) {
$in_cart[$key] = $this->shopmod->get_one_data($key);
}
$cart['list'] = $in_cart;
}
$this->load->vars($cart);
$data['cart'] = $this->load->view('shop/cart', '', TRUE);
$this->load->view('layout/default', $data);
}
VIEW:
<?php if(is_array($list)): ?>
<?php foreach($list as $row):?>
<tr>
<td><?=$row->name?></td>
</tr>
<?php endforeach ?>
<?php endif;?>
but i have this following error:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: shop/cart.php
Line Number: 18
anyhelp guys? :(
I see you are using Code igniter (nice!)
I would do the following to ensure the $key is taking 'name'
public function index() {
foreach ($_SESSION['cartProducts'] as $key => $value) {
echo $key;
} }
Then
public function index() {
$in_cart = array();
if (!isset($_SESSION['cartProducts'])){
$in_cart['list'] = "No Products";
}
else{
foreach ($_SESSION['cartProducts'] as $key => $value) {
$in_cart[$key] = $this->shopmod->get_one_data($key);
}
}
$this->load->vars($cart);
$data['cart'] = $this->load->view('shop/cart', '', TRUE);
$data['list'] = $in_cart;
$this->load->view('layout/default', $data); }
PHP is telling you exactly what is wrong.
You are trying to access the name property of the $row array when the -> syntax is specifically reserved for objects.
To access array values, you use square brackets and keys: <?=$row['name']?>
Helpful hint to understand - What you're doing is quasi-equivalent to: 7->name, insofar as the left value (7) has no idea how to use the arrow syntax. It's reserved for when the left value is an object. Not an integer, or in your case, an array.
Update after your comment:
You would get at the data like this:
<? foreach($row['list'] as $r):?>
<tr>
<td><?=$r[0]->name;?></td>
</tr>
<? endforeach;?>