I tried a bunch of different things first but don't seem to be getting a result.
Controller:
public function all()
{
$projects = Project::all();
foreach($projects as $project) {
$pid = $project->id;
}
$am = DB::table('projects')
->join('employees', 'projects.am_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.am_id', 'employees.name')
->first();
$pm = DB::table('projects')
->join('employees', 'projects.pm_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.pm_id', 'employees.name')
->first();
return view('projects/all', [
'projects' => $projects, 'am' => $am, 'pm' => $pm
]);
}
View:
<h1 class="text-center">All Projects</h1>
#foreach ($projects as $project)
<div class="row">
<div class="col-md-6 text-center">
<h3>{{ $project->company }}</h3>
</div>
<div class="col-md-6 text-center">
<p>Account Manager: {{ $am->name }}</p>
<p>Project Manager: {{ $pm->name }}</p>
</div>
</div>
<hr>
#endforeach
So I want to view the company name and the project manager and account manager for each project. What I'm seeing though is the same person for all projects. It's not picking up the correct managers for each project.
I tried doing the for loop in the controller to get the project ID and passing that in the where clause but that didn't work.
Any ideas how to fix this?
Thanks!
Your queries for $am and $pm are not inside the foreach loop. So you only gets the $am and $pm values for last project($pid)
public function all()
{
$projects = Project::all();
foreach($projects as $project) {
$pid = $project->id;
$am = DB::table('projects')
->join('employees', 'projects.am_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.am_id', 'employees.name')
->first();
$pm = DB::table('projects')
->join('employees', 'projects.pm_id', '=', 'employees.id')
->where('projects.id', '=', $pid)
->select('projects.id', 'projects.pm_id', 'employees.name')
->first();
$project->am = $am;
$project->pm = $pm;
}
return view('projects/all', [
'projects' => $projects
]);
}
View
<h1 class="text-center">All Projects</h1>
#foreach ($projects as $project)
<div class="row">
<div class="col-md-6 text-center">
<h3>{{ $project->company }}</h3>
</div>
<div class="col-md-6 text-center">
<p>Account Manager: {{ $project->am->name }}</p>
<p>Project Manager: {{ $project->pm->name }}</p>
</div>
</div>
<hr>
#endforeach
Related
I have problem with display data and form on one page.
Display data from database disappear after write in textarea any word. The same problem ist when i click on submit. In debugger is only data from first query, from second is cleared.
I try with one query (second), but it's the same.
My example code
route web:
Route::middleware(['auth:sanctum', 'verified'])->get('/tickets/show/{slug}', Tickets\Show::class)->name('tickets.show');
Livewire:
public $ticket;
public $slug;
public $posts;
public $content_post;
public $ticket_id;
public function mount($slug){
$this->ticket = Tickets::where('tickets.id','=', $slug)->join('users', 'users.id', '=', 'tickets.user_id')
->select('users.email','users.phone','users.username','users.first_name','users.last_name','tickets.user_id','tickets.created_at',
'tickets.id', 'tickets.subject','tickets.status_id','tickets.admin_id')->first();
$this->posts = TicketsPost::where('tickets_posts.ticket_id','=', $slug)->join('users', 'users.id', '=', 'tickets_posts.user_id')
->select('users.email','users.username','users.first_name','users.last_name','tickets_posts.user_id','tickets_posts.created_at',
'tickets_posts.ticket_id','tickets_posts.post')->get();
}
public function addPost()
{
$this->validate([
'content_post' => 'required'
]);
$user_id = Auth::user()->getAuthIdentifier();
TicketsPost::create([
'ticket_id' => $this->ticket_id,
'user_id' => $user_id,
'post' => $this->content_post,
]);
}
public function render()
{
return view('livewire.tickets.show');
}
View:
<p >#{{$ticket->id}}</p>
<p >{{$ticket->subject}}</p>
#foreach($posts as $post)
<p >{{$post->first_name}} {{$post->last_name}}</p>
<p >{{ date('d.m.Y H:i',strtotime($post->created_at)) }}</p>
<p > {{$post->post}}</p>
#endforeach
<form >
<input type="hidden" name="ticket_id" value="2" wire:model="ticket_id">
<textarea name="content_post"
class=""
rows="5" required="" placeholder="Treść wiadomości" wire:model="content_post"></textarea>
#error('content_post') <span class="text-red-500">{{ $message }}</span>#enderror
<button wire:click.prevent="addPost()" type="submit" class="">
<span id="btn_text" class="ml-2">Dodaj</span>
</button>
</form>
In Livewire, make sure your component has a single-level root element only.
And, don't forget to add wire:key in your element inside every loop in a blade.
Your loop should be as;
#foreach($posts as $key => $post)
<div wire:key="{{'post'.$key}}">
<p>{{$post->first_name}} {{$post->last_name}}</p>
<p >{{ date('d.m.Y H:i',strtotime($post->created_at)) }}</p>
<p > {{$post->post}}</p>
</div>
#endforeach
I want to show user orders and use this code:
#foreach($orderHistory as $order)
<div class="with-spacing three-col orders" id="works-grid">
<div class="work-item branding ui-ux">
<div class="work-detail">
<a href="#">
<img src="images/portfolio-1.jpg" alt="">
<div class="work-info">
<div class="centrize">
<div class="v-center">
<h3>{{ $order->product_name }}</h3>
<p> {{ $order->product_price }}</p>
</div>
</div>
</div>
</a>
</div>
</div>
</div>
#endforeach
controller:
$orderHistory = DB::table('cart')
->where('user_id', auth()->id())
->join('singleproducts', 'singleproducts.id', '=', 'cart.product_id')
->select('singleproducts.name', 'cart.qty', 'cart.product_price')
->get();
return view('UI.UserProfile.profile' , compact('orderHistory'));
and the error is: " Undefined property: stdClass::$product_name "
what is the problem?
Typo, you are selecting the column "name" from the database.
#foreach($orderHistory as $order)
<div class="with-spacing three-col orders" id="works-grid">
<div class="work-item branding ui-ux">
<div class="work-detail">
<a href="#">
<img src="images/portfolio-1.jpg" alt="">
<div class="work-info">
<div class="centrize">
<div class="v-center">
<h3>{{ $order->name }}</h3>
<p> {{ $order->product_price }}</p>
</div>
</div>
</div>
</a>
</div>
</div>
</div>
#endforeach
or change the query
$orderHistory = DB::table('cart')
->where('user_id', auth()->id())
->join('singleproducts', 'singleproducts.id', '=', 'cart.product_id')
->select('singleproducts.name as product_name', 'cart.qty', 'cart.product_price')
->get();
return view('UI.UserProfile.profile' , compact('orderHistory'));
The problem is that $orderis not an object and you're trying to access it like an object. Perhaps it's an array - try accessing it as such.
We can't be sure unless we see where $order is defined in your controller maybe. One quick way is to place a #dd($order) just after initializing the for loop. That dumps the variable so you can see it's format.
Your query should be like this:
$orderHistory = DB::table('cart')
->where('user_id', auth()->id())
->join('singleproducts', 'singleproducts.id', '=', 'cart.product_id')
->select('singleproducts.name as product_name', 'cart.qty as quantity', 'cart.product_price as product_price')
->get();
return view('UI.UserProfile.profile' , compact('orderHistory'));
So you end up with an array of objects in this format:
[
{
product_name: 'name',
product_price: 555.00,
quantity: 2
}
...
{}
]
so i am trying to implement search bar, in the blog i'm working on and when i search it gives me undefined variable post in search.blade.php and i have tried all i can . please help
heres my search.blade file
#extends('layouts.main')
#section('content')
<div class="col-lg-8">
#foreach($posts as $post)
<h2>
{!! $post->title !!}
</h2>
<p class="lead">
by {!! $post->author !!}
</p>
<p><span class="glyphicon glyphicon-time"></span> Posted on {!! $post->created_at !!}</p>
<hr>
{!! Html::image('/img/posts/'. $post->image, $post->title, array('style'=>'width: 675px; height:225px;')) !!}{
<hr>
<p>{!! $post->short_desc !!}</p>
<a class="btn btn-primary" href="/store/view/{{ $post->id }}">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
#endforeach
</div>
#endsection
And the controller function
public function getSearch(Request $request) {
$keyword = $request->input('keyword');
$categories = Category::all();
if( $keyword != ""){
return view('store.search')->with('posts', Post::where('title', 'LIKE', '%'.$keyword.'%')->get())
->with('keyword', $keyword)
->with('categories', $categories);
}
else {
return redirect('/') ;
}
}
I'm trying to show products related to a category. But it is showing all products which are also related to other categories.
Blade:
<div class="row">
#foreach($categories as $category)
#foreach ($category->products as $product)
<div class="col-sm-12 col-md-6 col-lg-4 p-b-50">
<!-- Block2 -->
<div class="block2">
<div class="block2-img wrap-pic-w of-hidden pos-relative block2-labelnew">
<img src="{{ URL::to('/') }}/images/backend_images/product_images/{{ $product->product_image }}"
class="img-thumbnail" style="width: 270px; height: 360px;" />
<div class="block2-overlay trans-0-4">
</div>
</div>
<div class="block2-txt p-t-20">
<a href="product-detail.html" class="block2-name dis-block s-text3 p-b-5">
{{ $product->product_name }}
</a>
<span class="block2-price m-text6 p-r-5">
$75.00
</span>
</div>
</div>
</div>
#endforeach
#endforeach
</div>
Controller:
public function products(Request $request, Product $product)
{
$categories = Category::with('products')->distinct()->get();
return view('product.listing', compact('product', 'categories'));
}
Route:
Route::get('/product/{id}','Admin\ProductController#products')->name('product.products');
Edit your controller:
public function products(Request $request, Product $product)
{
$categories = $product->categories;
return view('product.listing', compact('product', 'categories'));
}
Also you should have the categories relationship method on Product Model.
public function products(Request $request, Category $category)
{
$products = Product::where('category_id',$category->id)->get();
$categories = Category:all();
return view('product.listing', compact('products','categories'));
}
Your route is
Route::get('/product/{id}','Admin\ProductController#products')->name('product.products');
//{id} it means you're getting category id from route right? so you can directly access it in controller.
Controller
//$id is from route.
public function products($id)
{
$products = Product::with('category')->where('category_id',$id)->get();
return view('product.listing', compact('products'));
}
In your blade file
#foreach($products as $product)
//here your all product which belongs to that categories.
and now if you want to access categories then may do as.
categories :- {{ $product->category->name }} //make sure it belongsTo in product
#endforech
public function products(Request $request)
{
$categories = Category::where('id',$request->id)->with('products')->get();
return view('product.listing', compact('categories'));
}
#foreach($categories as $category)
#foreach ($category as $product)
Assuming a product has one category and you have a category_id column in your products table change products method in your ProductController like below.
public function products(Request $request, Product $product)
{
$relatedProducts = Product::where('category_id', $product->category_id)
->where('id', '!=', $product->id) // ignore current product
->get();
return view('product.listing', compact('product', 'relatedProducts'));
}
Replace id in your route withproduct`. Read More about route model binding.
Route::get('/product/{product}','Admin\ProductController#products')->name('product.products');
Then in your view your you can iterate through relatedProducts
controller:
public function products(Request $request, Category $category)
{
$category->load('products');
return view('product.listing', compact('category'));
}
route:
Route::get('/product/{category}','Admin\ProductController#products')->name('product.products');
blade file:
#foreach ($category->products as $product)
<div class="col-sm-12 col-md-6 col-lg-4 p-b-50">
<!-- Block2 -->
<div class="block2">
<div class="block2-img wrap-pic-w of-hidden pos-relative block2-labelnew">
<img src="{{ URL::to('/') }}/images/backend_images/product_images/{{ $product->product_image }}" class="img-thumbnail" style="width: 270px; height: 360px;" />
</div>
</div>
</div>
#endforeach
I am trying to create a search where user can search for book title. There is a field in 'books' table called 'title'.
however if I do dd($books); I get:
Collection {#667 ▼
#items: []
}
If I do dd($searchTerms); output seems to be correct
Here's my controller:
function search()
{
$books = Book::all();
return view('layouts/search',['books' => $books]);
}
function details() {
$searchTerms = explode(' ', \Request::input('book'));
$books = Book::where(function($q) use($searchTerms) {
foreach ($searchTerms as $book) {
$q->orWhere('title', '%'.$book.'%');
}
})->get();
dd($books);
return view('layouts/details', compact('books'));
}
Search blade:
#extends('layouts.master')
#section('title')
#section('content')
<h1>Search for books</h1>
<form action="{{url('details')}}" method="POST">
{{ csrf_field() }}
<div>
<input type='text' name='book' placeholder='Enter any character' />
</div>
<input type="submit" name="submitBtn" value="Search">
</form>
#endsection
Details blade:
#extends('layouts.master')
#section('title')
#section('content')
<h1>User Details</h1>
<form>
<p>
<ul>
#foreach ($books as $book)
<a href= "{{url('reader/'.$book->title)}}">
{{$book->title}}</a>
</p>
</form>
#endforeach
</ul>
#endsection
You forgot to tell the eloquent builder that your OR's should have LIKE as the operator:
$q->orWhere('title', 'like', '%'.$book.'%');
Please change the function of details to this
function details(Request $request) {
$searchTerms = $request->book;
$books = Book::where(function($q) use($searchTerms) {
foreach ($searchTerms as $book) {
$q->orWhere('title', '%'.$book.'%');
}
})->get();
dd($books);
return view('layouts/details', compact('books'));
}