How to display all Jobs that belongs to Company in Laravel - php

I need to display only jobs that belogs to company in companies/show.blade.php
This is show method in CompanyController:
public function show(Company $company)
{
$jobs = Job::where('company_id', $company->id)
->latest()
->get();
return view('pages.companies.show', [
'jobs' => $jobs
]);
}
This is foreach in show.blade:
#foreach($jobs as $job)
<div class="col-12 col-md-5">
<h2>{{ $job->job_name }}</h2>
</div>
<div class="col-12 col-md-7">
<p>{{ $job->description }}</p>
</div>
#endforeach
But it doesnt display anything. Where is mistake?

first of all you have define relations as below:
Company Model
public function jobs(){
return $this->hasMany('App\Job');
}
Job Model
public function company(){
return $this->belongsTo('App\Company');
}
Controller
public function show(Company $company)
{
$jobs = $company->jobs;
return view('pages.companies.show', [
'jobs' => $jobs
]);
}
Blade View
#foreach($jobs as $job)
<div class="col-12 col-md-5">
<h2>{{ $job->job_name }}</h2>
</div>
<div class="col-12 col-md-7">
<p>{{ $job->description }}</p>
</div>
#endforeach

Related

Function Show CRUD not working in Controller.php

I'm using laravel 8 for CRUD.. i try using create and store function in controller and it's perfecly fine then the data will store in database. Now, i try to use show function which the data that i created will show to user once they click the 'show' button. The most error i found is undefined $product
here is my blade for index.blade
<a class="btn btn-info" href="show-index">Show</a>
Here is my blade for show.blade.php
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2> Show Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="pro_index"> Back</a
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{{ $product->name }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Details:</strong>
{{ $product->detail }}
</div>
</div>
</div>
Here is my ProductController.php
class ProductController extends Controller
{
public function index()
{
$products = Product::all();
return view('products.pro_index',compact('products'));
}
public function create()
{
return view('products.create');
{
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'detail' => 'required',
]);
$product= Product::create([
'name' => $request->name,
'detail' => $request->detail,
]);
return redirect('/pro_index') ->with('success','Your record has been updated');
}
public function show (Product $product)
{
$product = Product::find($product);
return view('products.show');
}
}
here is my web.php
Route::get('/show-index', [App\Http\Controllers\ProductController::class, 'show'])->name('show');
You don't need this line of code because you already have model binding in place
$product = Product::find($product);
Change anchor href from href="show-index" to href="{{ route('show', $product) }}
In controller
public function show (Product $product)
{
return view('products.show', compact('product'));
}
in web.php
Route::get('/show/{product}', [App\Http\Controllers\ProductController::class, 'show'])
->name('show');
For more : https://laravel.com/docs/8.x/routing#route-model-binding

Laravel 7.6 Call to undefined method Illuminate\Database\Eloquent\Builder::appends()

I'm not being able to append the result with query. I'm filtering the result with category on the page and trying to add pagination. When a user clicks on category only category related products should be displayed and on pagination I want to keep the selected category. I'm getting this error:
Facade\Ignition\Exceptions\ViewException
Call to undefined method Illuminate\Database\Eloquent\Builder::appends() (View: D:\Xampp\htdocs\ProjectName\resources\views\products\index.blade.php)
I have tried this from laravel docs
{{ $users->appends(['category' => 'query'])->links() }}
Here's my Controller:
public function index(Request $request)
{
// If the category is selected
if(request()->category) {
$products = Product::with('categories')->whereHas('categories', function ($query){
$query->where('slug', request()->category);
});
$categories = Category::all();
$categoryName = optional($categories->where('slug', request()->category)->first())->name;
}else {
$products = Product::paginate(9);
$categories = Category::all();
$categoryName = 'All Products';
}
return view('products.index')->with([
'products' => $products,
'categories' => $categories,
'categoryName' => $categoryName,
]);
}
This is my blade file:
<h4>{{ $categoryName }}</h4>
#forelse ($products as $product)
<div class="card mb-3 shadow" style="">
<div class="row no-gutters">
<div class="col-md-4 d-flex justify-content-around p-2 border">
<img src="/storage/products/{{ $product->image }}" class="card-img">
</div>
<div class="col-md-8 p-2">
<div class="card-body">
<h5 class="card-title">{{ $product->title}}</h5>
<p class="card-text"> {!! \Illuminate\Support\Str::limit($product->text, $limit = 100) !!} </p>
<div class=""><button class="btn btn-outline-warning float-left">View Cataloge</button>
</div>
</div>
</div>
</div>
</div>
#empty
<div class="card">
<div class="card-title p-5 shadow bg-light">
<h5>No products found</h5>
</div>
</div>
#endforelse
<div class=" d-flex justify-content-around">
{{ $products->appends(request()->input())->links() }}
</div>
Here you are missing paginate(9) also pass ($request) in closure function
public function index(Request $request)
{
// If the category is selected
if (!empty($request)) {
$products = Product::whereHas('categories', function ($query) use ($request) {
$query->where('slug', $request->category);
})->paginate(9);
$categories = Category::all();
$categoryName = optional($categories->where('slug', request()->category)->first())->name;
return view('products.index')->with([
'products' => $products,
'categories' => $categories,
'categoryName' => $categoryName,
]);
}
else {
$products = Product::paginate(9);
$categories = Category::all();
$categoryName = 'All Products';
return view('products.index')->with([
'products' => $products,
'categories' => $categories,
'categoryName' => $categoryName,
]);
}
}

Display data in the blade from the database

How could I display data from the database in certain tables in my view? Without foreach, because it would overlap me. I want to take an id and that will be displayed and if I click on another id, it will display what I want from the other id. Here's how I did it.
Controller:
public function viewUserQuestion() {
if(Auth::check()) {
$posts = Post::all();
return view('viewQuestion', compact('posts'));
}
else {
return redirect('register');
}
}
Blade:
<div class="card-body p-0">
<div class="mailbox-read-info">
<h5 align="center">{{ $posts->title }}</h5>
<h6> From userID: {{ $posts->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $posts->created_at }}</span></h6>
</div>
<div class="mailbox-read-message">
<p>{{ $posts->content }}</p>
</div>
I must say that I have looked at other issues like this, but I can't get a tip?
#foreach($posts as $post)
<div class="card-body p-0">
<div class="mailbox-read-info">
<h5 align="center">{{ $post->title }}</h5>
<h6> From userID: {{ $post->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $post->created_at }}</span></h6>
</div>
<div class="mailbox-read-message">
<p>{{ $post->content }}</p>
</div>
</div>
#endforeach
EDIT: If you want to return only 1 post, use this in your controller:
public function viewUserQuestion() {
if(Auth::check()) {
$posts = Post::where('id', 1)->first();
return view('viewQuestion', compact('posts'));
}
else {
return redirect('register');
}
}
$posts is a collection so you have to iterate over it - example:
<div class="card-body p-0">
#foreach($posts as $post)
<div class="mailbox-read-info">
<h5 align="center">{{ $post->title }}</h5>
<h6> From userID: {{ $post->user_id }}</h6>
<span class="mailbox-read-time" align="center">Created at: {{ $post->created_at }}</span></h6>
</div>
<div class="mailbox-read-message">
<p>{{ $post->content }}</p>
</div>
#endforeach
</div>

PHP Laravel how to sort by category

I'm building a Laravel application and I have now added two tables, an "employees" and "employees_category" table. Both of them have an "order" column so that I can set the order of how the categories and employees will be displayed on the website.
When I add an employee, I can assign them to a category. So far so good, I'm able to display the employees in order they have been set, but not the categories.
This is what I have so far:
My EmployeesController:
public function index()
{
$employees = Employee::all()->groupBy('category');
return view('app.employee.index', compact('employees'));
}
And in my blade view:
#foreach ($employees as $category => $workers)
<div class="col text-center mb-6">
<h2>{{ $category }}</h2>
</div>
<div class="row px-4 px-xl-10">
#foreach($workers->sortBy('employee_order') as $worker)
// some content
#endforeach
</div>
#endforeach
This sorts the employees correctly by order-number but not the categories, how can I achieve that?
Why dont you query exactly what you want in your view
public function index()
{
$categories = Category::with('employees')->get();
return view('app.employee.index', compact('categories'));
}
and in the view : (notice what you want is as $category => $workers)
#foreach ($categories as $category)
<div class="col text-center mb-6">
<h2>{{ $category }}</h2>
</div>
<div class="row px-4 px-xl-10">
#foreach($category->employees->sortBy('employee_order') as $worker)
// some content
#endforeach
</div>
#endforeach
The good thing is you can now paginate on the catogories

Laravel - Undefined variable: articles

public function press (Request $request)
{
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
}
return view('pages.press.index', compact('articles'));
}
I'm receiving an error Undefined variable: articles. Above is the code I'm using in my controller. What is the error that I'm making?
VIew
#if(!empty($articles))
#foreach ($articles as $article)
<div class="col-md-4">
<div class="box border">
<div class="box-header">
<img src="{{ url('http://assets.hobohweb.com/uploads/press/'.$article->image) }}" class="img-fluid" alt="">
</div>
<div class="box-body">
<h6 class="h-2x">{{ $article->title }}</h6>
{{ Carbon\Carbon::parse($article->created_at)->diffForHumans() }} on {{ $article->source }}
</div>
</div>
</div>
#endforeach
#endif
Just check if there are any articles in your search, like:
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
} else {
$articles = [];
}
And use it in the view:
#forelse($articles as $article)
...
#empty
<p>There are no articles!</p>
#endforelse
You are declaring the variable inside the if, but if the condition is false, the variable is never set. Try to use an else condition:
public function press (Request $request)
{
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
} else {
$articles = null;
}
return view('pages.press.index', compact('articles'));
}
You will also need to check if $articles is null in your view.
that's because the variable is never assigned a value and does not exist in the external context of the -if
try to use this
public function press (Request $request)
{
$articles = array();
if($request->has('search')){
$articles = Press::search($request->input('search'))->get();
}
return view('pages.press.index', [
'articles' => $articles
]);
}
View
#foreach ($articles as $article)
<div class="col-md-4">
<div class="box border">
<div class="box-header">
<img src="{{ url('http://assets.hobohweb.com/uploads/press/'.$article->image) }}" class="img-fluid" alt="">
</div>
<div class="box-body">
<h6 class="h-2x">{{ $article->title }}</h6>
{{ Carbon\Carbon::parse($article->created_at)->diffForHumans() }} on {{ $article->source }}
</div>
</div>
</div>
#endforeach
I hope this help you

Categories