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
Related
My data set and coding are listed below.
[
{
"color":[
{
"id":1,
"color":"yellow"
},
{
"id":2,
"color":"red"
}
]
},
{
"color":[
{
"id":3,
"color":"blue"
},
{
"id":4,
"color":"green"
}
]
}
]
#foreach ($items as $item)
#foreach($item->color as $color)
<p>{{$color->color}}</p>
#endforeach
#endforeach
What I want:
yellow 1/4
red 2/4
blue 3/4
green 4/4
How can I count the total in every row?
First you need to take an array :
#php
$color_counts = array();
#endphp
Than in for loop you can count colors#foreach ($items as $item)
#foreach($item->color as $color)
#php
if(isset($color_counts[$color->color]) && $color_counts[$color->color]!=null){
$color_counts[$color->color]['count'] += 1;
}
else {
$color_counts[$color->color]['count'] = 1;
$color_counts[$color->color]['color_name'] = $color->color;
}
#endphp
#endforeach
#endforeach
Now you can get colors and its count in $color_counts array.
Note : You can do this thing in a controller and pass it to view
you can You Laravel collection count method
$collection = collect([1, 2, 3, 4]);
$collection->count();
like that
for more information follow this link :
https://laravel.com/docs/5.8/collections#method-count
I created a similar JSON code just to show you the way of how to make it work.
[{"id":1,"color":"blue"},{"id":2,"color":"red"},{"id":3,"color":"red"},{"id":4,"color":"green"},{"id":5,"color":"red"},{"id":6,"color":"blue"}]
$colors = Color::all(); // Model contains 6 random colors
$countAllColors = count($colors);
$colorNames = $colors->countBy('color');
foreach ($colorNames->all() as $key => $value) {
$values = $key . $value . '/' . $countAllColors . "\n";
echo $values; // output will be: blue2/6 red3/6 green1/6
}
you might noticed that I'm using countby method to see how much do we have repeated color item in the array.
however, if you want to do it in blade template, just copy the code below and paste it in your blade template
#php
$colors = '[{"id":1,"color":"blue"},{"id":2,"color":"red"},{"id":3,"color":"red"},{"id":4,"color":"green"},{"id":5,"color":"red"},{"id":6,"color":"blue"}]';
$colors = json_decode($colors, true);
$colors = collect($colors);
$countAllColors = count($colors);
$colorNames = $colors->countBy('color');
#endphp
<div>
#foreach ($colorNames->all() as $key => $value)
<b> {{ $key . $value . '/' . $countAllColors .' ' }} </b> <br>
#endforeach
</div>
output
blue2/6
red3/6
green1/6
for the nesting problem, you might want to take a look of this pulck method.
I hope you find it useful
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!! .
I cannot get attach() to work in my setup.
Each User can have many Orders which can have many Products.
User.php
public function orders()
{
return $this->hasMany(Order::class);
}
Order.php
public function users()
{
return $this->belongsTo(User::class);
}
public function products()
{
return $this->belongsToMany(Product::class)
->withTimestamps()
->withPivot('qty');
}
Product.php
public function orders()
{
return $this->belongsToMany(Order::class)
->withTimestamps()
->withPivot('qty');
}
I have a create.blade.php which is meant to show all available products and the quantity for each can be chosen, this is to be saved on the pivot table.
create.blade.php
{{ Form::open(array('url' => '/orders/store')) }}
#foreach ($products as $product)
<div>
<span class="mealname">{{ $product->name }}</span>
<hr>
<p>{{ $product->description }}</p>
</div>
<div class="qty">
{{ Form::text( 'qty', 0, [ 'type' => 'tel' ]) }}
</div>
#endforeach
{{ Form::select('delivery_day', ['M' => 'Monday', 'W' => 'Wednesday'],
null, ['placeholder' => 'Delivery Day'])
}}
{{ Form::submit('Place Order') }}
{{ Form::close() }}
When I submit the request only the fields to the Order table are saved,
public function store(Request $request)
{
// Validate
$request->validate([
'qty'=> 'integer',
]);
# Create New Order
$order = new Order;
$id = Auth::user()->id;
$order->user_id = $id;
// passed in parameters of form (not qty)
auth()->user()->orders()->save($order); // save order
# Pivot attach()
HERE I AM LOST
return redirect('complete')->with('success', 'Order has been created');
}
I believe it is the fact that I am trying to pass multiple products in one form, (which I believe i should be able to just pass as an arry while I use attach().
I have tried various solutions and I am still unable to ever get the pivot table to populate.
My last attempt was to pass the product_id through a hidden field and then running this.
$attach_data = [];
for ($i = 0; $i < count($product_ids); $i++);
$attach_data[$product_ids[$i]] = ['qty' => $qtys[$i]];
$order->product_ids()->attach($attach_data);
However, this did not work.
According to the docs (https://laravel.com/docs/5.7/eloquent-relationships#updating-many-to-many-relationships) this is one way to attach multiple items:
$user->roles()->attach([
1 => ['expires' => $expires],
2 => ['expires' => $expires]
]);
So you have to modify this:
# Create New Order
$order = new Order;
$id = Auth::user()->id;
$order->user_id = $id;
$order->save();
// change this for your array of ids
$products_to_sync_ids = [1,3,23];
$sync_data = [];
$qty = 1; <----- I dont know if you are inserting them with the same qty
for($i = 0; $i < count($products_to_sync_ids); $i++))
$sync_data[$products_to_sync_ids[$i]] = ['qty' => $qty];
$order->products()->sync($sync_data);
Try and check if the products are inserting correctly on the pivot table and then modify the code to insert every code with his quantity.
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