ErrorException Missing required parameters laravel - php

I am having an issue by modifying the route for a view. I want instead of /company/id to show /company/id/name
the route:
Route::get('/company/{id}/{name}', 'PagesController#showCompany')->name('company.detail');
show method in controller:
public function showCompany($id){
$company = Company::find($id);
return view('company.show')->with('company', $company);
}
and in the view $companies is from a search controller - and it should get the results with a link to open the view
#foreach($companies as $company)
Show detail
#endforeach
if using only with id like /company/id works. What i am wrong?

A simple an elegant way (i think) is:
{{route('company.detail', ['id' => $company->id, 'name' => strtolower(preg_replace('/[^A-Za-z0-9-]+/', '-', $company->company_name))}}
You can have a friendly url name. I am sure that there are better solutions out there.
If you have more params in the route you can use an associative array and initialize each param name with a value.
the controller now is the same with the id.

Related

how to fix Undefined variable: Products in view blade laravel 9.50.1 [duplicate]

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 !!}

How to add query string to Laravel view

I'm using Laravel 5.7 and would like to return a view, with query strings. E.g. below is what I'm doing right now.
return view('cart', compact('somevar'))
This takes the user to mydomain.com/cart
I'd like to add query strings, e.g. so the user goes to mydomain.com/cart?id=123
How would I do this?
You can achieve this :
return view('cart', ['id' => $id]);
Your data should be an array with a key-value pair.
You can also use with method:
return view('cart')->with('id', $id);
Also, you can use compact :
return view('cart', compact('id'));
For more, please refer Passing Data To Views
You can pass data to view like this:
return view('admin-panel.leave.index')->with($data);
And $data is an array that contain multiple values like this:
$data = array('count' => '7', 'id' => '4');
What your asking is not doable with this approach.Because, the main problem is /var/www/vendor/laravel/framework/src/Illuminate/View does not have what you are looking for. Only way to do that is return redirect()->route('cart', ['id' => 123]);
If this is really necessary, I think you need to redirect before call the /cart Something like this to your rootes/web:
Route::get('/addcart', 'CartsController#workaround');
Route::get('/cart', 'CartsController#showtheview')->name('cart');
Then in your CartsController:
public function workaround(){
// your code.....
return redirect()->route('cart', ['id' => 1]);
}
public function showtheview(){
//your code....
return view('cart', compact('somevar'))
}
Hope you get the logic.
I don't think it's possible. All the answers here forget that "somevar" has to be returned as well. Not only somevar compact data must be returned, but the URL must have variables too.
Put otherwise, you want internal blade data by compacting, but the URL must look different because it must have "&somevar2=xyz&somevar3=abc" appended.
Please correct me if I'm wrong, but not possible.
You can try this :
Route::get('cart/{id}', 'TicketsController#edit');
And in return:
return view('cart/'.$id, compact('somevar'))
Also you can try like this:
// app/Http/routes.php
Route::get('/cart/{id}', function ($id) {
return view('cart')->with('id', $id);
});
// resources/views/example.blade.php
The last part of the route URI is <b>{{ $id }}</b>

How to print values passed by get method in next view in laravel?

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']}}

How to delete a single record in Laravel 5?

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

Laravel 4 drop down list

I've made a drop down list that takes 'destination-from' values on 'oneways' table from the database. Following this tutorial here: http://www.laravel-tricks.com/tricks/easy-dropdowns-with-eloquents-lists-method
But whenever I try to run it, It gives me this kind of error: Undefined variable: categories What seems to be the problem here? I am really new to this. A newbie on laravel.
Here are my codes:
onewayflight.blade.php
$categories = Category::lists('destination-from', 'title');
{{ Form::select('category', $categories) }}
onewayflightcontroller.php
public function onewayflightresults()
{
return View::make('content.onewayflight');
$list = DB::table('oneways');
$listname = Oneways::lists('destination-from');
$content = View::make('content.onewayflight', array('list'=>$list, 'listname'=>$listname));
}
I am not really sure of what I have left out one this. And I am also wondering if the model has something to do with this?
Additionally to #jd182 's advice (as I lack the reputation to comment, I use answer); are you sure that lists() function/facade returns some value other than null. PHP, sometimes, considers null values as undefined.
And your blade syntax is wrong. if you want to define a variable inside a blade file (which is not advised) you should wrap it around tag and work it as a regular php file.
The code in your template where you are fetching the categories needs to be surrounded by PHP tags otherwise it won't be executed. But better still - move it to your controller which is where it belongs.
Also in your controller you return the view at the top of your onewayflight() - nothing after this will be executed.
So change it work work like this and you should be ok:
onewayflight.blade.php
{{ Form::select('category', $categories) }}
onewayflightcontroller.php
public function onewayflight()
{
$categories = Category::lists('destination-from', 'title');
return View::make('content.onewayflight', array('categories' => $categories));
}
Also, in routes.php the route should look like this:
Route::get('/your-route-path', [
'as' => 'your_route_name',
'uses' => 'onewayflightcontroller#onewayflight'
]);

Categories