Taking the following code as an example, how can I use the param paramenter inside the index view?
Route::get('foo/{param}', [FooController::class, 'index']);
Have you index method of the controller take a parameter, $param, and pass it to your view:
public function index($param)
{
return view('index', ['param' => $param]);
}
Then in the Blade view:
{{ $param }}
Or, you could get the route parameter from the request directly in the view if needed:
{{ request()->route('param') }}
Related
Hey as i am passing a blade view which is having it own controller also i am including it into the view which does not have its own controller. it gives me an undefined variable error can any one help me how to it.
I have a view which does not have any controller only have Route like this Route::get('index', function () { return view('index'); }); in this view i am passing another view which having its own controller and also having some data from an array. but after using this view inside the view i get undefined variable error.
Two steps :
Declare & transfer $variable to View from Controller function.
public function index()
{
return view("index", [ "variable" => $variable ]);
}
Indicate where transferred $variable from Controller appear in view.blade.php.
{{ $variable }}
If you do not make sure, $variable is transferred or not
{{ isset($variable) ? $variable : '' }}
If this helps anyone, I was completely ignorant to the fact that my route was not hooked with the corresponding controller function and was returning the view directly instead, thereby causing this issue. Spent a good half hour banging my head till I realized the blunder.
Edit
Here again to highlight another blunder. Make sure you're passing your array correctly. I was doing ['key', 'value] instead of ['key' => 'value'] and getting this problem.
You can try this:
public function indexYourViews()
{
$test = "Test Views";
$secondViews = view('second',compact('test'));
return view('firstview',compact('secondViews'));
}
and after declare {{$secondViews}} in your main view file(firstview).
Hope this helps you.
public function returnTwoViews() {
$variable = 'foo bar';
$innerView = view('inner.view', ['variable' => $variable]);
return view('wrapper.view, ['innerView' => $innerView]);
}
This may be what you are looking for?
... inside your wrapper.view template:
{!! $innerView !!}
EDIT: to answer the question in the comment: In order to fetch each line you for do this inside your $innerView view:
#foreach($variable as $item)
{{ $item }}
#endforeach
... and in the wrapper view it will still be {!! $innerView !!}
I have passed parameter for my get route by
Show more
And my route is
Route::get('list/{category}', ['as' => 'tour.featured', 'uses' => 'PublicController#productList']);
I want to display the category name in my product-list.blade.php view
This is what i have tried:
{{$_GET['category']}}
This is giving me error of
Undefined index: category
use {{request()->route('category')}}
Use your same route and make your controller like:
public function yourMethod($category)
{
// other stuff here, will return value for $category
return view('someview', COMPACT('category'));
}
And now you can use $category value into your blade file like:
{{ $category }}
Answer reference: laravel 5.2 How to get route parameter in blade?
You are missing one step:
Route::get('list/{category}', ['as' => 'tour.featured', 'uses' => 'PublicController#productList']);
after that, you have to create the productList method in PublicController like
function PublicController(Request $request)
{
echo $request; // will print the data in $product->category
// Now you can pass this value to your view like:
return view('view_name', array('category', $product->category));
}
and get this on view like:
{{$_GET['category']}}
Using Laravel 5 I m trying to delete a single record within a controller, here is my code:
public function destroy($id)
{
$employee = Employee::find($id);
$employee->delete();
return Redirect::route('noorsi.employee.index');
}
My view page code is:
<td>Delete</td>
My route is:
Route::delete(employee.'/{id}', array('as' => 'noorsi.employee.destroy','uses' => Employeecontroller.'#destroy'));
That did not work.
How do I fix the implementation ?
From the official Laravel 5 documentation:
Delete an existing Model
$user = User::find(1);
$user->delete();
Deleting An Existing Model By Key
User::destroy(1);
User::destroy([1, 2, 3]);
User::destroy(1, 2, 3);
In every cases, the number between brackets represents the object ID, but you may also run a delete query on a set of models:
$affectedRows = User::where('votes', '>', 100)->delete();
http://laravel.com/docs/5.0/eloquent#insert-update-delete
So the Laravel's way of deleting using the destroy function is
<form action="{{ url('employee' , $employee->id ) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button>Delete Employee</button>
</form>
You can find an example here http://laravel.com/docs/5.1/quickstart-intermediate#adding-the-delete-button
And your route should look something like this
Route::delete('employee/{id}', array('as' => 'employee.destroy','uses' => 'Employeecontroller#destroy'));
It works with eg:Route::resource('employee', 'EmployeeController'); and it should also work with how you set up your destroy route.
Obviously you have a bad routing problem. You're trying to use a 'get' verb to reach a route defined with a 'delete' verb.
If you want to use an anchor to delete a record, you should add this route:
Route::get('/employee/{id}/destroy', ['uses' => 'EmployeeController#destroy']);
or keep using a delete verb, but then you need to use a form (not an anchor) with a parameter called _method and value 'delete' stating that you're using a 'delete' verb.
Route::get('/showcon/{del_id}/delete','MainController#deletemsg');
public function deletemsg($del_id){
$mail=Mail::find($del_id);
$mail->delete($mail->id);
return redirect()->back();
}
del
In my routes I have this route defined as:
// app/routes.php
Route::resource('CharacterController');
The corresponding method in the controller is:
// app/controllers/CharacterController.php
public function index(){
$characters = Character::all();
$houses = House::all();
return View::make('characters.index')->with(array('characters'=>$characters, 'houses' => $houses));
}
Finally, in the view:
// app/views/characters/index.blade.php
#this fires an error:
{{ $houses[$characters->house_id]->name }}
# at the same time this gives correct result:
{{ $houses[1]->name }}
# and this IS equal to 1:
{{ $characters->house_id }}
You can't use the id as index of the array to access the object with given id.
Since you have an Eloquent Collection you can use its various functions. One of them being find() for retrieving one item by id
{{ $houses->find($characters->house_id)->name }}
I am trying to return a specific value in my Laravel view. I define a variable in my controller and pass it on to my view. This is what I get when my blade code looks like this:
<h1>{{ $sport }}</h1>
However, since this is returned in my view and I only want to have the "sport" itself I did this:
<h1>{{ $sport->sport }}</h1>
Undefined property: Illuminate\Database\Eloquent\Collection::$sport
Any help would be much appreciated.
Thanks
You are referencing an Array Collection not an object itself
Try
<h1>{{ $sport->first()->sport }}</h1>
Or modify your controller like this
// Controller
return View::make('sports', array('user' => $sport->first()));
// View
{{ $sport->sport }}