I keep getting Undefined variable: cars when loading the page in Laravel.
I have the following code:
web.php
Route::get('/cars', function () {
$cars= Car::select('brand')->get();
return view('test.cars')->with(compact($cars));
});
If I put dd(cars); before return view the collection is outputted.
In cars.blade.php I have the following code:
<select name="car">
#foreach($cars as $car)
<option>{{ $car->brand}}</option>
#endforeach
</select>
and I get Undefined variable: cars, I also removed the dropdown from blade template and I tried {{ dd($cars) }} but get the same error.
Try the below code :
Route::get('/cars', function () {
$cars= App\Car::pluck('brand');
return view('test.cars',['cars'=>$cars]);
});
OR
Route::get('/cars', function () {
$cars= App\student::pluck('first_name');
return view('test.cars', compact('cars'));
});
Note: Car model is present in App folder here.
The correct syntax for passing variables to views is either:
view('test.cars', ['cars' => $cars])
or
view('test.cars')->with('cars', $cars)
the "with" function requires a name to be passed first, followed by the variable.
Related
I kept getting this error when trying to use the update function in edit view, but not sure how to fix it.
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The PATCH method is not supported for this route. Supported methods: GET, HEAD.
here is the edit blade:
<form method="POST" action="{{ route("course.update", [auth()->user()->username, $course->title. $course->id]) }}" enctype="multipart/form-data">
#method('PATCH')
#csrf
here is the web.php:
Route::get('/{user}/edit_{course}', 'CoursesController#edit')->name('course.edit');
Route::patch('/{user}/update_{course}', 'CoursesController#update')->name('course.update');
the update url only shows like this, without the variables {course}:
/{user}/update_
edit url works fine, using the same format. but the $course data doesn't seem passing into the edit view:
{{ route("course.edit", [auth()->user()->username, $course->title. $course->id]) }}
here is the controller:
public function edit(Course $course) {
return view('courses.edit', compact('course'));
}
public function update(UpdateCourseRequest $request, Course $course) {
$course->update($request->all());
if (count($course->course_images) > 0) {
foreach ($course->course_images as $media) {
if (!in_array($media->file_name, $request->input('course_images', []))) {
$media->delete();
}
}
}
$media = $course->course_images->pluck('file_name')->toArray();
foreach ($request->input('course_images', []) as $file) {
if (count($media) === 0 || !in_array($file, $media)) {
$course->addMedia(storage_path('tmp/uploads/' . $file))->toMediaCollection('course_images');
}
}
return view('courses.index', compact('course'));
}
When generating routes, the second parameter is an associative array. Usually you don't pass id's or strings into routes, but simply models and let Model Binding do the rest.
{{ route("course.update", ['user' => auth()->user(), 'course' => $course]) }}
So i have a problem that has been bugging me for a few days now. I want to display my posts on my homepage that is already displaying on my other pages. I think i have found why the information is not displaying and this is due to the Routes of the page. The view is looking fine and works correctly, however my controllers are the issue:
Web.php
Route::get('/', 'PageController#index');
Route::get('/welcome', 'HomeController#index1');
Route::get('/services', 'PageController#services');
Route::get('/register', 'PageController#register');
Route::get('/Create', 'PageController#Create');
Route::get('/search', 'PageController#search');
Route::get('/payment', 'PageController#Payment');
Route::resource('posts', 'PostsController');
Route::resource('search', 'SearchController');
Route::resource('reviews', 'ReviewsController');
Route::resource('postings', 'HomeController');
HomeController.php
public function index()
{
return view('home');
}
public function index1()
{
$postings = Post::all();
return view('Pages.welcome', compact('postings'));
}
Welcome.blade.php
#if(count($postings) > 1)
#foreach($postings as $post)
<h2>{{$post->title}}</h2>
#endforeach
#else
</p>no posts found</p>
#endif
The issue is in my WEB.PHP. PageController#index directs the page to the homepage, with HomeController being the controller that holds index. I then decided to create a function within that HomeController that allows me to display posts, however i keep getting error 'undefined error'. To conclude how would i insert a function in an existing controller that already has index.
You don't have $postings variable in your view, don't access it. This does not mean you can not do what you want.
You can abuse how if statements are resolved. If you have && operator in your if statement it will not check secondary conditions if first condition fails.
Will fail
#if ($postings)
Will work
#if (false && $postings)
To check if a variable is present without accessing it, you can use isset(), that is a PHP function. This will create the preferred statement that will not access $postings if it is not there. So try with the following code.
#if(isset($postings) && count($postings) > 1)
#foreach($postings as $post)
<h2>{{$post->title}}</h2>
#endforeach
#else
</p>no posts found</p>
#endif
I'm trying to pass the parameter that I have in my route file, into my form.
And when I do it, the error of Undefined Variable appears
<form action="{{route('capitulos_destroy_multiple',$curso)}}" method="POST" id="eliminar-multiples">
{{ csrf_field() }}
</form>
Here is my route:
Route::post('destroy_multiple/{curso}','CapitulosController#destroy_multiple')->name('capitulos_destroy_multiple');
And Here is my controller, that returns that exact variable:
public function destroy_multiple(Request $request,$curso)
{
return redirect('administrador/capitulos/index/'.$curso);
}
Your route should be called the following way:
{{route('capitulos_destroy_multiple',['curso'=>$curso])}}
Feel free to refer to Laravel manual: https://laravel.com/docs/5.8/routing#named-routes
Route::get('user/{id}/profile', function ($id) {
//
})->name('profile');
$url = route('profile', ['id' => 1]);
Hope it helps.
hi m working to show products into a seprate shop.blade.php page but page is not opening and it gives error Undefined variable: productsALL,
the code is here:
#foreach($productsALL as $product)
<!--some html-->
<img src="{{ asset('images/backend_images/products/small/'.$product->image) }}" alt="IMG-PRODUCT">
{{ $product->product_name }}
#endforeach
here is its route:
Route::get('shop', function () {
return view('shop');
});
and this is ShopController:
public function shop()
{
$productsALL = Product::get();
return view('shop')->with(compact('productsALL'));
}
anyone would prefer to provide its solution
Your Route is wrong.
Actually, your Route isn't link with your Controller, you need to link with put "Namespace#functionName".
Try this :
Route::get('shop', 'ShopController#shop');
Documentation
I'm using davejamesmiller's Breadcrumbs package and I have some trouble with getting post category in post view. I want my breadcrumbs to be like this :
Home > Category > Post
I defined breadcrumbs like this:
// Home
Breadcrumbs::for('home', function ($trail) {
$trail->push('Home', route('home'));
});
// Home > [Category]
Breadcrumbs::for('category', function ($trail, $category) {
$trail->parent('home');
$trail->push($category->title, route('category', $category->slug));
});
// Home > [Category] > [Post]
Breadcrumbs::for('post', function ($trail, $post) {
$trail->parent('category', $post->category);
$trail->push($post->title, route('post', $post->slug));
});
And called this code in the post view template to output breadcrumbs:
{{ Breadcrumbs::render( 'course', $course, $category ) }}
But getting this error:
Undefined property: stdClass::$category
And this refers to this line of defined breadcrumb:
$trail->parent('category', $post->category);
My posts table does'nt have category column and I don't know what should I pass as the second parameter of parent method! Should I pass post's category row which is in other table to my post view? What should I pass as second parameter?
I solved my problem by passing $category variable to the breadcrumb that I was defined. like this:
Breadcrumbs::for('post', function ($trail, $post, $category) {
$trail->parent('category', $category);
$trail->push($post->title, route('post', $post->slug));
});
then in my view I changed the output code to:
{{ Breadcrumbs::render( 'post', $post, $category ) }}
I also passed the post's category table row to view from controller.