Undefined variable: professors - laravel

In the show function in my controller I passed the variable $professors
In the controller method I am passing the variable like this -
public function show(Professor $professor)
{
$professors = Professor::all();
return view('professor.show',compact('professors'));
}
I am trying to display the professor name in the show.blade.php like this
<div class="form-group">
<div class="row">
<strong class="col-sm-2">Name:</strong>
{{$professors->name}}
</div>
</div>
I'm getting this error-
Property [name] does not exist on this collection instance.
Where am I doing wrong ?

I found the solution
public function show(Professor $professor)
{
$professors = Professor::find($professor->id);
return view('professor.show',compact('professors'));
}
I forgot the find($professor->id); works for me now guys

Related

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>

how do i pass data value to another page via link in laravel?

i am trying to make a list of locations that you can rent. but to rent the place you need to fill in some information. to fill in this information you excess another page. how do i make it so laravel knows the page belongs to a certain location
this is what ive done now but i keep getting the error:
Call to undefined method App\Reservation::location()
as soon as i have filled in the fields of information
this is the blade file that links to the the create reservation file
#foreach
($locations as $location => $data)
<tr>
<th>{{$data->id}}</th>
<th>{{$data->name}}</th>
<th>{{$data->type}}</th>
<th><a class="btn" href="{{route('Reservation.index', $data->id)}}">rent</a></th>
</tr>
#endforeach
this is the create reservations blade
<form action="{{ route('location.store') }}" method="post">
#csrf
<label>title</label>
<input type="text" class="form-control" name="name"/>
<label>type</label>
<select>
<option value="0">klein</option>
<option value="1">groot</option>
</select>
<button type="submit" class="btn">inschrijven</button>
</form>
this is what the location controller looks like
public function store(Request $request)
{
$location = new Reservation;
$location->name = $request->get('name');
$location->type = $request->get('type');
$location->location()->associate($request->location());
$location->save();
return redirect('/location');
}
and the relationships in my models should also work
class Reservation extends Model
{
public function locations()
{
return $this->belongsTo('Location::class');
}
}
class Location extends Model
{
public function reservations()
{
return $this->hasMany('Registration::class');
}
}
ive been stuck at this all day and i really dont know where to look anymore
The error you are getting is because of the wrong function name, you are calling location, while it is locations.
public function locations(){}
&
$location->location()->associate($request->location());
and you can pass the variable as a query parameter, you'll need to pass this data as an array in your blade file.
Web.php
Route::get('/somewhere/{id?}, function(){
//do something
})->name('test');
Blade
route('test', ['id' => $id]);
Controller Method
public function store(Request $request, $id) //Adding the query parameter for id passed in Route.
{
$location = new Reservation;
$location->name = $request->get('name');
$location->type = $request->get('type');
$location->location()->associate($id);
$location->save();
return redirect('/location');
}

why show undefined variable when pass frontend

When I pass a variable in the fronted site it shows that the variable is undefined: why does this happen? I've also tried using compact but it does not work for me. Is there any way to solve the problem?
HomeController file code:
public function index()
{
$category =DB::table('categories')->where('status', 1)->get();
$brand =DB::table('brands')->where('status', 1)->get();
$home=view('home.main_content');
return view('home')->with('home', $home)->with('category', $category);
}
Fronted code using a foreach loop:
#foreach($category as $result)
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">{{$result->name}}</h4>
</div>
</div>
#endforeach
Error:
Undefined variable: category (View: C:\xampp\htdocs\laravelblog\resources\views\home\sidebar_left.blade.php)
Your view is called home and the view in which you are trying to use the variable is called sidebar_left.
So if you use the blade #include directive to load that view you will need to pass the variable to that view.
You can do that like this:
#include('sidebar_left', ['category' => $category])
Then you will be able to use it.

Laravel Invalid argument supplied for foreach() fetching individual columns

can somebody help with this? I'm trying to pass some data from my Controller page to my index, but I'm getting this error "Invalid argument supplied for foreach()"
Here's my model
class Bookings extends Model
{
//Table name
protected $table = 'bookings';
//Primary key
public $primaryKey = 'book_id';
}
My Controller
use Illuminate\Http\Request;
use App\Bookings;
class PagesController extends Controller
{
public function index(){
$data = Bookings::orderBy('NameENG', 'DetailsENG')->get();
$data = Bookings::all();
$marks = 'NameENG';
$briefs = 'DetailsENG';
return view('pages/index', compact(['data', 'marks', 'briefs']));
}
}
and part of my index
<div class = "container">
#if(count($data) > 0)
#foreach($marks as $mark)
#foreach($briefs as $brief)
<div class="row">
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<img class="card-img-top" alt="Thumbnail [100%x225]" style="height: 225px; width: 100%; display: block;" src="images/calle_yungay.png">
<div class="card-body">
<h5>{{$mark}}</h5>
<p>{{$brief}}</p>
Can someone help please?
On controller function:
$routes = 'tourNameENG'; // is a string
and on view, you are trying to iterate it:
#foreach($routes as $route)
that's why the error Invalid argument supplied for foreach().
foreach(): works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.

Laravel 4 - Method[ ] does not exist

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?

Resources