Laravel 4 - Method[ ] does not exist - laravel

I'm still a novice to Laravel 4 and trying to figure out why I'm getting an error saying that Method [inventoryNotFound] does not exist. It has to do when I am attempting to use the InventoryController#show function.
On my Index page I have a href that I'm trying to send to the show views. The code looks like this:
#foreach($inventory as $item)
<div class="col-md-4 inventory">
<img class="img-responsive" src="{{{$item->image}}}">
<a class="description" href="">{{{$item->title}}}</a>
#foreach($item->size_details as $details)
<p>{{ $details->size }}: {{ $details->price }}</p>
#endforeach
</div>
#endforeach
My show function is:
class InventoryController extends BaseController {
public function index()
{
$inventory = Inventory::all();
return View::make('inventories.index')->with('inventory', $inventory);
}
public function show($id)
{
$inventory = Inventory::find($id);
if(is_null($inventory)){
return $this->inventoryNotFound();
}
return View::make('inventories.show',['inventory' => $inventory]);
}
}
And I have a resource controller as my routes:
Route::resource('inventories', 'InventoryController');
Even when I attempt to run my routes manually, I run into the same problem.
Any ideas as to why?

Related

how can I resolve this while working with for else loop and compact function in Laravel?

I'm using laravel version 7.30.1. it shows the error undefined variable $services.
code for home controller.
class HelloController extends Controller
{
public function services()
{
$services = [
'service 1',
'service 2'
];
return view('/services',compact('services'));
}
}
code for blade file.
#section('content')
<H1>welcome to laravel 6 from services</H1>
<ul>
#forelse($services as $service)
<li>{{ $service }}</li>
#empty
<li>this list is empty</li>
#endforelse
</ul>
#endsection
code for route file
route::view('/services','services');
route::view('/services','services');
here your only loading view not controller that's why your data is not pass in to view
use get req. which will be in controller then form controller load view with data
Route::get('services',[\App\Http\Controllers\HelloController::class, 'services']);

Laravel Accessing Attributes & Slots Within Component Classes

I have a question about Laravel - Documentation - Accessing Attributes & Slots Within Component Classes
I read this section, and did some experiments. What I've found is that the closure only can return string but component.view, and what it returns would overwrite what is defined on the component view, which leads me to a question What use case is this feature for?
Could anyone make some examples of using this feature for me?
Anyone could help me with it will be so much appreciated.
If the string matches an existing view then it will render that view and not overwrite it. You can return an existing view as follows:
// app/View/Components/Post.php
public function render()
{
return function (array $data) {
// $data['componentName'];
// $data['attributes'];
// $data['slot'];
return 'components.post'; // /resources/views/components/post.blade.php
};
}
// views/components/post.blade.php
<div class="item">
<div class="title">
New Post
</div>
<div class="content">
<p>Content post...</p>
</div>
</div>
Example:
// component class
class Link extends Component
{
public $path = "";
public function __construct()
{
$this->path = request()->path();
}
public function render()
{
return function (array $data) {
if(isset($data['attributes']['href'])) {
$data['attributes']["link"] = $data['attributes']['href'];
if ($data['attributes']['href'] == "/".$this->path) {
$data['attributes']['active'] = $data['attributes']['class']." link-active-class";
} else {
$data['attributes']['active'] = $data['attributes']['class'];
}
}
return 'components.commons.link';
};
}
}
// component view
<a href="{{ $attributes['link'] }}" class="{{ $attributes['active'] }}">
{{ $slot }}
</a>
Usage:
<x-commons.link href="/module/news" class="primary-action">
<i class="fas fa-newspaper"></i> News
</x-commons.link>

Undefined variable: foods

hi guys am in need of assistance , i know this seems to be an easy one but am a bit confused , i have a foreach loop in my main.blade.php file, which shows foods from my database it works fine but when its clicked it meant to lead to the individual post and thats where i get the error
Undefined variable: foods
heres my foreach loop in main.blade.php file
#foreach($foods as $food)
<li class="item">
<a href="{{ route('Foods.show', $food->id) }}">
<img src="{{ Storage::disk('local')->url('food_images/'.$food->image ) }}" class="img-responsive w-25 p-3" alt="Food" >
<div class="menu-desc text-center">
<span>
<h3> {{ $food->title }}</h3>
{{ $food->body }}
</span>
</div>
</a>
<h2 class="white"> #{{ $food->price }}</h2>
</li>
#endforeach
heres my main.blade.php controller
public function LoadHome()
{
$foods = Food::all();
$foods = Food::orderBy('created_at','desc')->inRandomOrder()
->limit(12)
->get();
return view('pages.home')->withFood($foods);
}
and heres Foods.show controller function
public function show($id)
{
$foods = Food::Find($id);
return view('foods.show')->withFood($foods);
}
please what am i doing wrong
Have you tried passing data from your controller to the view using this something like this:
public function show($id)
{
$foods = Food::Find($id);
return view('foods.show')->with('foods', $foods);
}
or, if you're passing multiple variables to the view:
public function show($id)
{
$foods = Food::Find($id);
$something = "else";
return view('foods.show', compact('foods', 'something'));
}
Your view doesn't know what $foods is at that point, so it's always good practice to check that foods is set before the loop:
#if (isset($foods) && $foods->count() > 0)
#foreach($foods as $food)
...
#endforeach
#endif
See the official Laravel docs for more information.
If you want the variable to be named foods in the view then you would need to use withFoods not withFood:
return view(...)->withFoods($foods);
As mentioned in the other answers, there are other ways to pass data to the views as well.
There is no data being passed to a view.
This is how you pass data to a view:
public function LoadHome()
{
$foods = Food::orderBy('created_at','desc')->inRandomOrder()
->limit(12)
->get();
return view('pages.home', ['foods' => $foods])
}
If you always want to have $foods in main.blade.php you should place this in a service provider
View::share('foods', Food::orderBy('created_at','desc')->inRandomOrder()
->limit(12)
->get());
https://laravel.com/docs/7.x/views#sharing-data-with-all-views

The result of the code is not displayed in the browser and there is no error in laravel

This is postController.php code:
public function index()
{
$posts = post::orderBy('created_at', 'DESC')->get();
return view('posts.index', compact('posts'));
}
Also I make Route ( web.php ):
Route::resource('post','PostController');
This is view ( index.blade.php ) code:
#section('content')
#foreach($posts as $post)
<div class="panel">
<div class="panel-heading">
<h3>{{ $post -> title }}</h3>
</div>
<div class="panel-body">
{{ $post -> body }}
</div>
</div>
#endforeach
#endsection
After all of that I cannot find the error why my result doesn't display from data base
This tables should be displayed from database
When I try another way the result was displayed u can see that when I write at postController.php code:
public function index()
{
$posts = post::orderBy('created_at', 'DESC')->get();
return $posts;
}
It showed from data base but there is also problem here ! Why the code is not displayed in a neat way in browser
try print post variable on post.index.blade using
{{dd($posts)}}. if your getting $posts variable on blade than everything fines.if not than dd("some meassage") into your index() to check if your index method actually get call.
if your index() method does not print dd("some message") than in terminal type php artisan route:list to check if your routes is prefix with something.

How to push the values to blade template in Laravel 4

I am trying to push an array to the blade template in laravel 4
Controller name: AdminController
public function listCompanies()
{
$companyObj = new company();
$companies = $companyObj->getCompanies();
$this->layout->content = View::make('admin/main',$companies);
}
Template name: main.blade.php
<div class="container">
#yield('content')
#foreach ($companies as $company)
<p>This is company {{ $company['name'] }}</p>
#endforeach
</div>
The error I am getting is:
Undefined variable: companies (View: C:\wamp\www\larvel-project\laravel\app\views\admin\main.blade.php)
The view is expecting to receive an associative array which includes the index 'companies' and will turn that into $companies.
Any of the following should work for you:
$this->layout->content = View::make('admin/main', ['companies' => $companies]);
$this->layout->content = View::make('admin/main', compact('companies'));
$this->layout->content = View::make('admin/main')->with('companies', $companies);

Resources