Accessing Nested URL Using Blade in Laravel 5.1 - php

I am trying to access a page using a nested url in Laravel 5.1 but I have reached a dead end.
I would like to make a GET request with a parameter in the middle of the url. To be precise, cars/{cars}/edit.
This is my code:
In the routes file
Route::resource('cars', 'carController');
In the cars controller file
class carController extends Controller
{
public function index(){
$cars = Car::all();
return view('carshome', compact('cars'));
}
public function edit($id){
return 'Welcome: '.$id.'page';
}
}
In the carshome blade template file
#foreach ($cars as $car)
<tr>
<td>{{ $car->name }}</td>
<td>{{ $car->type }}</td>
<td class="text-center">
<a href = {{url('/cars',[$car->name])}}>
<i class="fi-clipboard-pencil"></i>
</a>
<a href = {{url('/cars',[$car->name])}}>
<i class="fi-x-circle"></i>
</a>
</td>
</tr>
#endforeach
In the car model file
class Car extends Model
{
protected $fillable = [
'name', 'type'
];
}
The helper function url can take parameters as part of the url. Am not sure how I can it to create the custom url. How can I access the url resource using blade?

This is just to mark the question as answered. As #Tezla shared:
In the carshome blade template file I can use:
route('cars.edit', [$car->name])
#lamzozo suggested another working method to use in the carshome blade template file:
url('cars', [$car->name, 'edit'])

Related

Passing an array of models to route to display in view from controller

So I have a view that would show all of the rows available and display them.
A route calls the controller to fetch the data:
Route::get('/list', array('as' => 'index', [ContactListController::class, 'getContacts']));
I assign my controller to grab all of the rows using:
class ContactListController extends Controller
{
public function getContacts()
{
$list = Contact::all();
return view('list')->with($list);
}
}
Then I display them in my view:
<tbody>
#foreach ($list as $contact)
<tr>
<form action="" method="post">
<input type="hidden" name="id" value="{{ $contact->id }}">
<td scope="row">{{ $contact->fname }}</td>
<td>{{ $contact->lname }}</td>
<td>{{ $contact->number }}</td>
</form>
</tr>
#endforeach
</tbody>
But the page says:
I would just like to display these data as a clickable table.
Please be easy on me since this is my first week of using Laravel ;)
At the first change your route to this:
Route::get('/list', [YourController::class, 'yourMethod'])->name('index');
and then in the controller:
class ContactListController extends Controller
{
public function getContacts()
{
$list = Contact::all();
return view('list')->with('list', $list);
}
}
I think you are using older route declaration with Laravel 8. The syntax of fromAction method in the screenshot of the error is Laravel 8 implementation.
Use either of this solutions:
// Using PHP callable syntax...
Route::get('/list', [ContactListController::class, 'getContacts'])->name('index');
// Using string syntax...
Route::get('/list', 'ContactListController#getContacts')->name('index');
More info on this is on Laravel 8.x Upgrade Guide

new link & controller produce Action Controller#index not defined error

I am creating a backend page that i want to use to manage employee data (with laravel 5.8). I Added a link on a sidemenu blade that points to the employee overview page.
Link:
<li class="nav-item">
<a href="{{ action('Profiles\Controllers\EmployeeController#index') }}"
class="nav-link {{ Request::is('admin') ? 'active' : null }}">
<i class="fas fa-user"></i> Employees
</a>
</li>
I also made a controller to fetch the data that i want to display, currently with dd() in the function.
class EmployeeController extends Controller
{
public $model = CustomerLogin::class;
protected $views = 'WebshopCustomers::customerslogins ';
static $routes = ['index', 'create', 'store', 'edit', 'update', 'destroy'];
protected $datatableSelect = 'customer_logins';
protected $datatableRelations = ['roles', 'permissions'];
protected $datatableTrashed = true;
protected $datatableRawColumns = ['email', 'deleted_at'];
public function baseBreadcrumbs()
{
return [
['name' => 'Gebruikersbeheer']
];
}
public function index()
{
dd('test_index');
}
}
After a reloaded the page shows the following error:
ErrorException (E_ERROR):
Action App\Modules\Profiles\Controllers\EmployeeController#index not defined.
(View: C:\xampp\htdocs\shopname\app\Modules\Backend\Views\partials\sidebar-default.blade.php)
Route:
I googled this error and read advice to check if the route to the function existed (it didnt) so i added that.
Route::get('/', 'EmployeeController#index')->name('employeeprofiles.index');
Changing the value of $namespace to null in the RouteServiceProvider was also mentioned, setting it to null did not change the current behavior of my code.
How can i correct this, what other things can i check?
in Laravel 5.8 in the RouteServiceProvider the namespace for routes was pointed to:
App/Http/Controllers
in the new Laravel I think they removed it.
Now for your problem, you should check where is the namespace from RouteServiceProvider pointing too, and then adding extra 'directories' on it; e.g
Route::get('/',
'App\Modules\Profiles\Controllers#index')->name('employeeprofiles.index');

ErrorException Undefined variable:

Why I am getting this error?
ErrorException
Undefined variable: features (View: C:\xampp\htdocs....views\layouts\index.blade.php)
FeaturedController.php
public function index()
{
$features = Feature::get();
return view ('layouts.index')->with(compact('features'));
}
ProductsController.php
public function index()
{
$products = Product::get();
return view ('products')->with(compact('products'));
}
layouts page- index.blade.php
#yield('content')
#foreach($features as $f)
<li>
<div class="prodcut-price mt-auto">
<div class="font-size-15">LKR {{ $f ['features_id'] }}.00</div>
</div>
</li>
#endforeach
view page - index.blade.php
#extends('layouts.index')
#section('content')
#foreach($products as $p)
<div class="mb-2">{{ $p ['prod_sub_category'] }}</div>
<h5 class="mb-1 product-item__title">{{ $p ['prod_name'] }}</h5>
<div class="mb-2">
<img class="img-fluid" src="{{asset('/storage/admin/'.$p ['prod_image_path'] ) }}" alt="Image Description">
</div>
<div class="flex-center-between mb-1">
<div class="prodcut-price">
<div class="atext">LKR {{ $p ['prod_price'] }}.00</div>
</div>
<div class="d-none d-xl-block prodcut-add-cart">
<i class="ec ec-shopping-bag"></i>
</div>
web.php
Route::resource('/products', 'ProductsController');
Route::resource('/layouts/index', 'FeaturedController#index');
Aside from not passing your variables to your blade views appropriately which other answers have pointed out, your trying to access features from a controller that does not have features set.
The controller below sets features and then makes use of it in the layouts.index blade file.
FeaturedController.php
public function index()
{
$features = Feature::get();
return view ('layouts.index')->with(['features' => $features]);
// or
// return view ('layouts.index', compact('features'));
}
While this controller sets products but then makes use of a blade file that extends another blade file that has a features variable in it. This is why your getting the error
ProductsController.php
public function index()
{
$products = Product::get();
return view ('products', compact('products'));
}
And to fix it you must pass the features variable along side products like so:
ProductsController.php
public function index()
{
$products = Product::get();
$features = Feature::get();
return view ('products')->with(['features' => $features, 'products' => $products]);
}
But if more than one blade file is going to extend this layouts.index file then this approach is not advisable, and situations like this is why Taylor Otwell introduced Blade Components. You can now move the features blade view and logic to a component that can wrap around any other file you want or be included.
The documentation is straight forward but if you want me to show you how to implement it to solve your dilemma then hit me up on the comment below.
as u r using data in layout u should use laravel view composer to share data to layout file ref link https://laravel.com/docs/7.x/views#view-composers
in your AppServiceProvider.php
inside boot() add this line
public function boot()
{
\View::composer('layouts.index', function ($view) { // here layout path u need to add
$features = Feature::get();
$view->with([
'features'=>$features,
]);
});
}
It share data based on specif view file like here layouts.index data is send to this view so if u not send data from controller it will get data from view composer
You can change your controller to this:
public function index()
{
$features = Feature::all();
return view ('layouts.index', compact('features'));
}
A your blade you should actually do #section instead:
#section('content')
#foreach($features as $f)
<li>
<div class="prodcut-price mt-auto">
<div class="font-size-15">LKR {{ $f->features_id }}.00</div>
</div>
</li>
#endforeach
#endsection

Want to add {{$value['category']}} in {{$value['category']}} in Laravel

I want to set {{$value['category']}} in {{route('')}}
This is my code:
#foreach($details as $value)
{{$value['category']}}
#endforeach
Controller function:
public function viewhome(Request $req){
$product = product::all()->unique('category');
return view('homepage',['details'=> $product]);}
Route:
Route::get('/homepage/db_val', 'HomeController#db_val')->name('db_val');
How to declare href properly. And what will be the route. Thank you.
1) warn : you call db_val function in HomeController but you show viewhome method in your question.
Route::get('/homepage/db_val', 'HomeController#<b>db_val</b>')->name('db_val');
if you want use method viewhome :
Route::get('/homepage/db_val', 'HomeController#<b>viewhome</b>')->name('db_val');
2) route is used with named route
you have a route named 'db_val' --> Route::.... ->name('db_val');
so it must be used like that ,
<a href='{{route('db_val')}}
3) in your case , assuming $value in foreach is an array with a 'category' index inside
you can use url instead route
#foreach($details as $value)
<a href="{{url('/your_url')}}/{{$value['category']}}">
link to {{$value['category']}}
</a>
#endforeach
4) but blade spirit is
route.php
Route::get('/showcategory/{id}','HomeController#showcategorie')->name('showcat');
view
#foreach($details as $value)
<a href="{{route('showcat', $value['category'])}}">
#endforeach
it means : you have one named route /showcategory with a parameter /{id}
Route:
Route::get('/homepage/db_val_1', 'HomeController#db_val_1')->name('db_val_1');
Route::get('/homepage/db_val_2', 'HomeController#db_val_2')->name('db_val_2');
Route::get('/homepage/db_val_3', 'HomeController#db_val_3')->name('db_val_3');
...
Controller Function :
public function db_val_1(Request $req){
$all = product::all()->where('category', 'db_val_1');
return view('homepage', ['details'=> $all]);
}
public function db_val_2(Request $req){
$all = product::all()->where('category', 'db_val_2');
return view('homepage', ['details'=> $all]);
}
public function db_val_3(Request $req){
$all = product::all()->where('category', 'db_val_3');
return view('homepage', ['details'=> $all]);
}
...
View home: In route the value will be ($value->category) like this.
#foreach($details as $value)
{{$value['category']}}
#endforeach
I got the solution. Thank you for helping.

How to pass value inside href to laravel controller?

This is code snippet from my view file.
#foreach($infolist as $info)
{{$info->prisw}} / {{$info->secsw}}
#endforeach
Here is my route which I defined inside route file
Route::get('switchinfo','SwitchinfoController');
I want to pass two values inside href tag to above route and retrieve them in controller. Can someone provide code to do this thing?
Since you are trying to pass two parameters to your controller,
You controller could look like this:
<?php namespace App\Http\Controllers;
class SwitchinfoController extends Controller{
public function switchInfo($prisw, $secsw){
//do stuffs here with $prisw and $secsw
}
}
Your router could look like this
$router->get('/switchinfo/{prisw}/{secsw}',[
'uses' => 'SwitchinfoController#switchInfo',
'as' => 'switch'
]);
Then in your Blade
#foreach($infolist as $info)
Link
#endforeach
Name your route:
Route::get('switchinfo/{parameter}', [
'as'=> 'test',
'uses'=>'SwitchinfoController#function'
]);
Pass an array with the parameters you want
<a href="{{route('test', ['parameter' => 1])}}">
{{$info->prisw}} / {{$info->secsw}}
</a>
and in controller function use
function ($parameter) {
// do stuff
}
Or if don't want to bind parameter to url and want just $_GET parameter like url/?parameter=1
You may use it like this
Route::get('switchinfo', [
'as'=> 'test',
'uses'=>'SwitchinfoController#function'
]);
function (){
Input::get('parameter');
}
Docs
You can simply pass parameter in your url like
#foreach($infolist as $info)
<a href="{{ url('switchinfo/'.$info->prisw.'/'.$info->secsw.'/') }}">
{{$info->prisw}} / {{$info->secsw}}
</a>
#endforeach
and route
Route::get('switchinfo/{prisw}/{secsw}', 'SwitchinfoController#functionname');
and function in controller
public functionname($prisw, $secsw){
// your code here
}

Categories