How to output table data from controller to view? - laravel

I'm in the beginning stages of trying to make sense of Laravel and am having trouble displaying the 'illuminate collection object' passed to a blade from a controller.
My print_r is outputting "Illuminate\Support\Collection Object ( [items:protected] => Array ( ) ) 1" which I thought meant that it was seeing one item in the array (just one record in the table currently), but I'm hitting the #else statement so I'm guessing it's actually empty. I'm getting no errors, but I have not been able to display anything from $products despite $title outputting just fine.
public function shop(){
$products = DB::table('products')->get();
$data = array(
'title'=>'Shop',
'products' => $products
);
return view('pages.shop')->with($data);
}
#section('content')
<h1>{{$title}}</h1>
{{ print_r($products) }}
#if($products->count())
<ul class="list-group">
#foreach($products as $product)
<li class="list-group-item">{{$product->title}}</li>
#endforeach
</ul>
#else
<p>No products</p>
#endif
#endsection
Why is my array empty?

On your Controller:
$title = 'Shop';
$products = DB::table('products')->get();
return view('pages.shop', compact('title', 'products');
On your Blade:
I would also suggest to put your unordered list tag <ul> outside of the loop then use #forelse for a cleaner code, like so:
#section('content')
<h1>{{$title}}</h1>
<ul class="list-group">
#forelse($products as $product)
<li class="list-group-item">{{$product->title}}</li>
#empty
<li class="list-group-item">No products</li>
#endforelse
</ul>
#endsection

Try this..........
public function shop()
{
$products = DB::table('products')->get();
$title = "Shop";
return view('pages.shop', compact('products', 'title'));
}
#section('content')
<h1>{{ isset($title) ? $title : '-' }}</h1>
#if($products->count())
<ul class="list-group">
#foreach($products as $product)
<li class="list-group-item">{{ isset($product->title) ? $product->title : '-' }}</li>
#endforeach
</ul>
#else
<p>No products</p>
#endif
#endsection

You can display the data by first passing it as an array to the view
return view('pages.shop')->with('data', $data);
then in the blade
#section('content')
<h1>{{$data['title']}}</h1>
#if(count($data['products']))
<ul class="list-group">
#foreach($data['products'] as $product)
<li class="list-group-item">{{$product->title}}</li>
#endforeach
</ul>
#else
<p>No products</p>
#endif
#endsection

Pass both $products and $title to the view, I prefer to use compact feels cleaner.
$title = 'shop';
$products = DB::table('products')->get();
return view('pages.shop', compact('title', 'products');
Then in your view you can reference them directly. As you are doing now.
#section('content')
<h1>{{$title}}</h1>
#if($products->count())
<ul class="list-group">
#foreach($products as $product)
<li class="list-group-item">{{$product->title}}</li>
#endforeach
</ul>
#else
<p>No products</p>
#endif
#endsection

In your controller
$title = 'shop';
$products = DB::table('products')->get();
return view('pages.shop', compact('title', 'products');
OR
$products = DB::table('products')->get();
$data = array(
'title'=>'Shop',
'products' => $products
);
return view('pages.shop')->with('data',$data);
In your Blade
#section('content')
<h1>{{$title}}</h1>
#if(count($products))
<ul class="list-group">
#foreach($products as $product)
<li class="list-group-item">{{$product->title}}</li>
#endforeach
</ul>
#else
<p>No products</p>
#endif
#endsection

Related

Laravel Pagination To Certain Section

Guys
I'm using Laravel's default pagination with bootstrap as its styling framework.
I want to direct my page to certain html section after I click the pagination link, so far I only know how to paginate like the following codes:
Controller:
$data = Products::paginate(9);
return view('index', ['data' => $data]);
Blade:
{{ $data->links() }}
So, I want to direct my page to a certain section in html everytime I clicked the pagination link button. how can I achieve that?
I'm sorry for my poor explanation, hit me up if you need more detail.
Thank You
you should make a blade file like this:
pagination.blade.php
#if ($paginator->hasPages())
<ul class="pagination justify-content-center">
#if ($paginator->onFirstPage())
<li class="paginate_button page-item previous disabled">
قبلی
</li>
#else
<li class="paginate_button page-item previous">
pre
</li>
#endif
#foreach ($elements as $element)
#if (is_string($element))
<li class="paginate_button page-item active">
<a href="javascript:void(0)" class="page-link" >{{ $element }}</a>
</li>
#endif
#if (is_array($element))
#foreach ($element as $page => $url)
#if ($page == $paginator->currentPage())
<li class="paginate_button page-item active">{{ $page }}</li>
#else
<li class="paginate_button page-item ">{{ $page }}</li>
#endif
#endforeach
#endif
#endforeach
{{-- next page --}}
#if ($paginator->hasMorePages())
<li class="paginate_button page-item next ">
next
</li>
#else
<li class="paginate_button page-item next disabled">
بعدی
</li>
#endif
</ul>
#endif
then create a blade file like this:
table.blade.php
#if(isset($permissions))
#php
$paginator=$permissions;
#endphp
<table class="table table-striped">
<thead>
<tr>
heaers tags
</tr>
</thead>
<tbody id="table-data">
#foreach ($permissions as $permission)
tr tags
#endforeach
</tbody>
</table>
#endif
<div class="col-md-12">
{{ $paginator->links('sections.pagination') }}
</div>
then in controller method:
public function index(Request $request)
{
$permissions=Permission::paginate(5);
if($request->ajax()){
return view('sections.table',compact('permissions'));
}
return view('permission.index',compact('permissions'));
}

I want items to be under the category they are related to

I want to achieve this result, but I can't understand the logic of the sql implementation
here is my welcome.blade.php:
#extends('layouts/layout')
#section('content')
<div class="container-fluid">
#foreach ($categories as $category)
<h4>{{$category->category_name}}</h4>
<div class="row">
<div class="col-sm-4">
<h6>{{$category->item_name}}</h6>
</div>
</div>
#endforeach
</div>
#endsection
and my controller:
$categories = DB::table('items')
->join('categories', 'items.category_id', 'categories.id')
->select('items.item_name', 'items.item_price', 'categories.category_name', 'categories.description')
->groupBy('categories.category_name')
->get();
return view('welcome', 'categories'=> $categories]);
You can use the QueryBuilder here if you want, but I would suggest using Eloquent unless you have a particular reason to not use it.
Let's assume that you have a Category and Item model and that you've defined a relational function within the Category model back to the Item model.
class Category extends Model
{
public function items()
{
// this assumes you're using standard naming conventions
return $this->hasMany(Item::class);
}
}
Then in your Controller, you might do something like the following to obtain categories and their related items.
$categories = Category::with('items')->get();
return view('welcome', compact('categories'));
The above can be adapted when required to limit the query to a specific Category when you require it. We use the with method to eager load the related records so that we can avoid the n+1 query problem.
Finally, in your view you can iterate (loop) over the categories and loop over the related items (there could be more than one).
#forelse($categories as $category)
<h4>{{ $category->category_name }}</h4>
#forelse ($category->items as $item)
<p>{{ $item->item_name }}</p>
#empty
<p>No items</p>
#endforelse
#empty
<p>No results</p>
#endforelse
The above is purely illustrative and ideally, you would break the above into reusable components.
Since you're using Laravel, you could use Eloquent relationships:
class Category extends Model{
public function items{
return $this->hasMany(Item::class);
}
}
Then in your controller:
$categories = Category::with('items')->get();
return view('welcome', ['categories'=> $categories]);
Then in your view:
#foreach ($categories as $category)
<h4>{{$category->category_name}}</h4>
#foreach($category->items as item)
<div class="row">
<div class="col-sm-4">
<h6>{{$item->name}}</h6>
</div>
</div>
#endforeach
#endforeach
$categories = DB::table("categories")->select("id", "category_name", "description")->get();
$items = DB::table("items")->select("category_id", "item_name", "price")->get();
view('welcome',compact("categories", "items"));
/////////////////////// BLADE ///////////////////////
<div class="container-fluid">
#foreach ($categories as $category)
#foreach($items as $item)
#if($category->id === $item->category_id)
<h4>{{$category->category_name}}</h4>
<div class="row">
<div class="col-sm-4">
<h6>{{$category->item_name}}</h6>
</div>
</div>
#endif
#endforeach
#endforeach
</div>
Try this.

Laravel - Invalid argument supplied for foreach()?

I have this code in my controller:
public function contact(){
$people = ['Michael', 'martin', 'Peter', 'Marian'];
return view('contact', compact('people'));
}
and in my contact.blade.php:
#extends('layouts.app')
#section('content')
<h1>Contact Page</h1>
#if (count($people))
<ul>
#foreach(#people as $person)
<li>{{$person}}</li>
#endforeach
</ul>
#endif
#endsection
#section('footer')
#endsection
I am getting the error:
Invalid argument supplied for foreach() (View: /home/mao/Documents/blog/resources/views/contact.blade.php)
I do not see the error.. been rewriting it twice. this should be correct as far as i can see on online guides?
I think you just have a mistype in your foreach
Try this
#foreach($people as $person)
<li>{{$person}}</li>
#endforeach
Change the # in $
change #people to $people (# => $)
#extends('layouts.app')
#section('content')
<h1>Contact Page</h1>
#if (count($people))
<ul>
#foreach($people as $person)
<li>{{$person}}</li>
#endforeach
</ul>
#endif
#endsection
#section('footer')
#endsection

I am trying to click on a category and products will show accordingly, but its not working

Controller:
public function shopfront(){
$products=Product::all();
$categories=Category::all();
return view('shop.index',compact('products','categories'));
}
index.blade.php:
<div class="sidebar-widget">
<h4 class="pro-sidebar-title">Categories</h4>
<div class="sidebar-widget-list mt-30">
<ul>
#foreach ($categories as $category)
#foreach ($products as $product)
#if ($category->id==$product->category->id)
<li>
<div class="sidebar-widget-list-left">
<input type="checkbox"> <a href="">{{ $category->name }}
{{-- <span>4</span> --}}
</a>
<span class="checkmark"></span>
</div>
</li>
#endif
#endforeach
#endforeach
</ul>
</div>
</div>
I do not know how to achieve this, will highly appreciate some help or i sample that i can work with
Image
i don't know how you want to achieve it but let me give you an idea if you want it in this way:
when click on a category then it should show all products of that category:
in your blade file
#foreach($categories as $category)
<li class="nav-item">
<a class="nav-link" href="{{route('category.products',$category->name)}}"> {{$category->name}}</a>
</li>
#endforeach
you should create a route named category.products as below in web.php
Route::get('category/{name}','YourController#yourmethod')->name('category.products');
controller
public function CategoryProducts($name){
$category = Category::where('name',$name)->first();
$products = $category->products;
return view('front-end.categoryWise-products',compact('products'));
}
you can customize it according to your requirements.

Trouble rendering my variables to my view

For some reason I can't seem to render these variables to my index.
Here is my code on my SellerController.
public function index()
{
$id = Auth::user()->id;
$product = Product::where('user_id', $id);
return view('seller.index', compact('product','id'));
}
and here is my code on my view
<ul>
#foreach($product as $p)
<li>
<p>{{ $p->description }}</p>
<p>{{ $p->price }}</p>
</li>
#endforeach
</ul>
You should try this:
1) You need to update your controller function like:
public function index()
{
$id = Auth::user()->id;
$product = Product::where('user_id', $id)->get();
return view('seller.index', compact('product','id'));
}
OR
2) You need to update your view file like:
<ul>
#foreach($product->get() as $p)
<li>
<p>{{ $p->description }}</p>
<p>{{ $p->price }}</p>
</li>
#endforeach
</ul>
Use get() to execute the query:
$products = Product::where('user_id', $id)->get();
And you don't need to pass $id to a view since you can just use auth()->id() directly in the view to get currently authenticated user ID.

Resources