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
Related
I am trying to get data from the database and simply display it in the view
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests', compact($messages));
}
The view
#foreach ($messages as $message)
<h1>{{ $message->first_name }}</h1>
#endforeach
But I am getting this error
compact(): Argument #1 must be string or array of strings, Illuminate\Database\Eloquent\Collection given
The PHP method compact() is a little tricky with its syntax. I make this same mistake all the time.
Change:
return view('dashboard/projects-interests', compact($messages));
to:
return view('dashboard/projects-interests', compact('messages'));
compact() looks for a string representation of the variable.
There are multiple ways you could write this.
using compact where you pass strings representing the variable
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests', compact('messages'));
}
using ->with
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests')->with(['messages' => $messages]);
}
With using laravel magic
public function index()
{
$messages = ProjectInterestedMessages::get();
return view('dashboard/projects-interests')->withMessages($messages);
}
Personally, I prefer this form as it avoids pointless variables
public function index()
{
return view('dashboard/projects-interests')
->withMessages(ProjectInterestedMessages::get());
}
You don't need $
return view('dashboard/projects-interests', compact('messages'));
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');
Web route is
Route::post('/loginext/{username}/{password}', 'LoginControllerExt#checkLogin');
while calling the url
http://localhost/old/loginext/bank/test
It will call the controller and get the response. But that time the url is
http://localhost/old/loginext/bank/test
I want to show the url as
http://localhost/old/
LoginControllerExt Controller
public function checkLogin(Request $request)
{
$LoginID=$request->username;
$Password=$request->password;
$Status='Success';
if($Status == 'Success' ) {
return view('dashboard.index');
}
}
Change route as
Route::post('/loginext', 'LoginControllerExt#checkLogin');
Pass the credentials in request body.Try with postman.
Check the screenshot
In controller
public function checkLogin(Request $request)
{
dd($request->all());
}
I have a table sites with list of sites with following columns ('id', 'path', 'site_link'). I've written in a Site model public $timestamps = false; so that it won't try to work with time what I don't need.
Also I have the following routes
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
$api->get('sites', 'App\Http\Controllers\SiteController#index');
$api->get('sites/{site}', 'App\Http\Controllers\SiteController#show');
});
The first one is working fine and returning all the data, however the second one is returning just [].
I have a controller which is below
namespace App\Http\Controllers;
use Illuminate\Http\Request;
Use App\Site;
class SiteController extends Controller
{
public function index()
{
return Site::all();
}
public function show(Site $site)
{
return Site::findOrFail($site);
}
public function store(Request $request)
{
$site = Site::create($request->all());
return response()->json($site, 201);
}
public function update(Request $request, Site $site)
{
$site->update($request->all());
return response()->json($site, 200);
}
public function delete(Site $site)
{
$site->delete();
return response()->json(null, 204);
}
}
The show method in your SiteController is taking a Site object. However the route is set up to only take the siteId. The code below should work for you based on how you've set up your routes.
public function show($site)
{
return Site::findOrFail($site);
}
Apply same to all your other controller methods since you want pass the site id via the url to the controller methods.
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.