I want to send my data from controller to xedit.blade.php, but I get the same error
in controller:
public function index5()
{
$users=User::all();
return view('xedit')->with('users',$users);//xedit is from xedit.blade.php
}
my route:
Route::get('admin/edit', function () {
return view('xedit');
})->name('edit');
Route::get('edit', 'Admin\UserController#index5');
I get the error:
Undefined variable: users
Remove this route
Route::get('admin/edit', function () {
return view('xedit');
})->name('edit');
Because when you go to this route there is no users variable. You can pass here also if you want.
If you want named route then you can also named 2nd one like -
Route::get('edit', 'Admin\UserController#index5')->name('edit');
Also you can send user variable in first one like this-
Route::get('admin/edit', function () {
$users = App\User::all();
return view('xedit', compact('users'));
})->name('edit');
Related
I have a project on Laravel 5.7 where I create a custom admin (admin.blade.php) page after login, just adding the column userlevel on my Users table, and I have the next foreach on my admin.blade
admin.blade.php
#foreach($customers as $customer)
<h3><b>{{$customer->count('id')}}</b></h3>
#break
#endforeach
Everything is ok when I do it the login on my page, but IF I close the page and I try to open again with the URL (example: http://192.168.1.125:8010/) the project show me the next error:
Undefined variable: customers (View:
D:\ExamplePage\system\resources\views\admin.blade.php) Undefined
variable: customers (0)
routes/web.php
Route::get('/', function () {
if($user = Auth::user())
{
if(Auth::user()->userlevel == "admin"){
return view('/admin');
}
}
return view('/auth/login');
});
Route::get('/admin', 'HomeController#index')->name('admin');
HomeController.php
use App\customers;
use App\pawns;
public function index()
{
$customers = customers::all();
$pawns = pawns::all();
return view('admin', compact('customers'), ['customers' => $customers,'pawns'=>$pawns]);
}
RedirectIfAuthenticated.php
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
if ($guard == 'admin') {
return redirect()->route('/admin');
}
}
return $next($request);
}
How can I solve to everything that I open again the URL, this show me the admin.blade.php if I logged and dont show me the error with the variable $customers
Thanks
That is because it unable to customer variable, compact is php function to merge array, and third parameter is used to merged Data , you don't need to pass a third parament, Instead Update the code in your home controller like this.
use App\customers;
use App\pawns;
public function index()
{
$customers = customers::all();
$pawns = pawns::all();
return view('admin',['customers'=>$customers,'pawns'=>$pawns]);
Or
return view('admin',compact('customers','pawns'));
}
I don't know what is going wrong with laravel controller? I want to increment the value of my counter but it is not working.
Code:
public function index(Request $request)
{
$data=0;
if($request->ajax())
{
$data=$data+1;
//My Code
}
return view('home', compact('data'));
}
result:
Data value : 0
I need the delete query within this controller
public function del($id)
{
$x=App\ImageMod::find();
// $x->where("id='$id'");
$x->delete();
return view('show');
}
How can I fetch the id dynamically and delete it?
you can delete for eg blog in this way:
public function destroy($id)
{
$blog = Blog::findOrFail($id);
$blog->delete();
return redirect()->back()->with('success','Blog deleted');
}
Use the Request dependency injection. Then, get the id property from your route. Then you can access the id from the request. Your code would looks like:
Controller
public function del(Request $request) {
$id = $request->id;
$x=App\ImageMod::destroy($id);
return view('show');
}
And then in routes/web.php you should have:
Route::delete('imagemod/delete/{id}', 'App\YourController#delete')->name('imagemod.delete');
Pass the $id from your page (ajax or even a direct request) by placing it within the route. If using ajax, something like:
$.ajax({ url: "{{url('ImageMod')}}/" + id, // <-- id from an input, pulling the val()
type: "DELETE",
data: {
_method: 'DELETE'
},
success: function (success) { .. }
In your web.php:
Route::delete('ImageMod/{id}', 'ImageModController#destroy');
Then, the routing binds that variable to the destroy method:
public function destroy($id)
{
ImageMod::destroy($id);
return 1; // if going back to ajax
}
You can also use:
public function destroy($id)
{
DB::table('blogs')->where('id', '=', $id)->delete();
}
I'm starting a project with laravel, and I'm having a problem when I try to edit a user.
It always return the "QueryException" as if I didn't pass the user ID, but I used the default layout for my Model, so my URL is something like /users/1/edit.
This is how my route is defined:
Route::get('/', function(){
return view('welcome');
});
Route::resource('users', 'CtrUsers');
This is the way I get the edit URL:
href="{{route('users.edit', $user)}}"
(the $user is set inside a foreach loop)
And this is my edit function:
public function edit(User $user)
{
return view('users.edit', compact('user'));
}
And something strange is that when I enter the URL /users/edit/1 (with de ID in the end), IT stops returning the QueryException, but returns "NotFoundHttpException".
Anyone had this problem?
change your controller
public function edit($id)
{
$user = User::findOrFail($id);
return view('users.edit', compact('user'));
}
Edited again
public function edit($id)
{
$user = User::where('usr_id',$id)->findOrFail();
return view('users.edit', compact('user'));
}
The issue is with the variable name which you have used for route model binding. Both variable names: the one which is on the route and another from the action parameter must match.
You can check the route info using php artisan route:list command.
So you have two options, either change the users variable name to user in the route like:
Route::resource('user', 'CtrUsers');
OR
You can change the variable name from user to users in the action parameter like:
public function edit(User $users)
{
return view('users.edit', compact('user'));
}
For more info:
https://laravel.com/docs/master/routing#route-model-binding
The best solution I found was rebuild my migrations changing the names of my tables ID's to just "ID", becouse I noticed that the errors were allways because the system doesn't find any column named usr_id.
I have following functions in Controller.
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
$this->SaveChanges($request);
}
private function SaveChanges($request) {
if($request['CountryID'] == 0) {
$Country = new \App\Models\CountryModel();
}
else {
$Country = \App\Models\CountryModel
::where('CountryID', $request['CountryID'])->first();
}
$Country->Country = $request['Country'];
$Country->CountryCode = $request['CountryCode'];
$Country->save();
return redirect()->route('AllCountries');
}
public function AllCountries() {
$Countries = \App\Models\CountryModel::all();
return view('Country.List', array('Countries' => $Countries));
}
Issue is in below line: When I call function SaveChanges, I am not able to see List of countries page and when I write the code directly in UpdateCountry function, it redirect route successfully.
return redirect()->route('AllCountries');
Anybody faced this issue before ?
Your route is being handled by the UpdateCountry function. Laravel will take action based on the returned value from this function. However, you're not returning anything from this function.
You call SaveChanges, which returns a Redirect object, but then you don't return anything from your UpdateCountry function. You need that Redirect object from the UpdateCountry function in order for Laravel to actually return the redirect to the client.
Update your UpdateCountry function to this:
// added the return statement, so this function will return the redirect
// to the route handler.
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
return $this->SaveChanges($request);
}
Maybe you missed a return in $this->SaveChanges($request). It has to be:
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
return $this->SaveChanges($request);
}
I hope it works fine for you.