This is Controller. I named it CartController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Gloudemans\Shoppingcart\Facades\Cart;
use App\Product;
class CartController extends Controller
{
public function index()
{
$cartItems=Cart::content();
return view('cart.index', compact('cartItems'));
}
public function edit($id)
{
$product=Product::find($id);
Cart::add($id,$product->name,'1',$product->price);
}
This is my welcome.blade.php. This is the page where I display all my Items.
<div class="row">
#forelse($books->chunk(4) as $chunk)
#foreach($chunk as $book)
<div class="column-prod">
<a href="{{url('view')}}">
<div class="card-prod">
<center><img src="{{url('images',$book->image)}}" style="width: 300px; height: 300px;"></center> </a>
<div class="container-prod">
<h2></h2>
<p class="title">{{$book->name}}</p>
<p class="price">₱ {{$book->price}}</p>
<p><i class="fas fa-cart-arrow-down"></i>Add to Cart</button></p>
<p><button class="button"><i class="fas fa-heart"></i> Add to Wishlist</button></p>
</div>
</div>
</div>
#endforeach
#empty
<h3> No Books </h3>
#endforelse
</div> <!--Div all end-->
This is the codes of my Cart Page. When the user click add to cart in the welcome page, his/her chosen items will be place in this page.
<h3> Cart Items </h3>
<!--TABLE NAMESSS-->
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Image</th>
<th scope="col">Name</th>
<th scope="col">Price</th>
<th scope="col">Quantity</th>
</tr>
</thead>
<tbody>
#foreach($cartItems as $cartItem)
<li>{{$cartItem->name}}</li>
<tr>
<td>{{url('images',$cartItem->image)}}</td>
<td>{{$cartItem->name}}</td>
<td>{{$cartItem->price}}</td>
<td>{{$cartItem->qty}}</td>
</tr>
#endforeach
</tbody>
</table>
This is my route.
Route::get('/', function () {
return view('welcome')->with(['books' => \App\Product::all()]);
});
Route::get('/cart','ShopController#cart');
Route::get('/signin','ShopController#signin');
Route::resource('/cart','CartController');
Route::get('/wishlist','ShopController#wish');
Auth::routes();
Route::get('/logout', 'Auth\LoginController#logout');
Route::get('/home', 'HomeController#index');
Route::group(['prefix' => 'admin', 'middleware' => 'auth'], function(){
Route::get('/',function(){
return view('admin.index');
})->name('admin.index');
Route::resource('product','ProductsController');
Route::resource('category','CategoriesController');
});
Route::get('/categ', function(){
return view ('admin.category.categ_index');
});
I'm confused I don't know how to fix my error. This is the complete error that the system return.
"htmlspecialchars() expects parameter 1 to be string, object given (View: C:\xampp\htdocs\Shop\resources\views\welcome.blade.php)"
I hope you can help me in solving this. Your answers are very much appreciated.
Related
Hello i'm trying to display the authenticated user but i'm getting this error :
Undefined variable: users (View:
C:\wamp64\www\Blan\resources\views\users\index.blade.php)
this is my code :
usercontroller :
public function index()
{
return view('users.index')->with('user', Auth::user());
}
the view :
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-header">
Update Your Profile
</div>
<div class="card-body">
<table class="table table-hover">
<thead>
<th>Image</th>
<th>Name</th>
<th>Update</th>
</thead>
<tbody>
#if( $users -> count() > 0 )
#foreach ($users as $user)
<tr>
<td>
#if(isset($user->profile->avatar))
<img src="{{ asset($user->profile->avatar)}}" width="60px" height="60px" style="border-radius: 50%" alt="">
#else
No image
#endif
</td>
<td>
{{$user->name}}
</td>
<td>
Update
</td>
</tr>
#endforeach
#else
<tr>
<th colspan="5" class="text-center">No Users </th>
</tr>
#endif
</tbody>
</table>
</div>
</div>
#stop
But if i return all the users using this code in the controller :
return view('users.index')->with('users', User::all());
its work fine.
so how i can return only the current authenticated user ?
You don't need to send the auth user via your controller.Auth is the global facade in laravel.Also you don't need foreach because there is only 1 authenticated user. Just type Auth::user()->name in the blade
where you want to display the user
Yea and btw the error ur getting is because you are doing foreach for $users but sending $user to blade but im pretty sure it wont work even if you correct that typo
In your example
public function index()
{
return view('users.index')->with('user', Auth::user());
}
You have used "user"
But tried to access variable as "users"
Unless you just made a typo in your example?
In my routing if i remove the {page} prefix it works completely fine but when i put it i get an error. For other it is working fine but it is not working for this route: Route::get('/{categories}', 'AdminVisible\CostIncludeController#index');
My AdminPageController:
public function index($page)
{
$page = Page::where('Pages_Slug_Name',$page)->firstorFail();
$pages = Page::all();
return view('admin.pages.page',[
'page' => $page,
],compact('pages'));
}
My CostIncludeController:
public function index($categories){
$pages = Page::all();
$packages = Package::where('slug',$categories)->first();
return view('admin.pages.costinclude',[
'packages' => $packages,
],compact('pages'));
}
My Route:
Auth::routes(['register' => false,'login' => false]);
Route::prefix('admin')->group(function() {
Route::get('/')->name('login')->uses('Auth\LoginController#showLoginForm');
Route::post('/')->name('login')->uses('Auth\LoginController#login');
Route::get('/dashboard', 'AdminVisible\HomeController#index')->name('admin.dashboard');
Route::prefix('pages')->group(function() {
Route::get('/','AdminVisible\AdminPageController#pages')->name('pages');
Route::prefix('{page}')->group(function() {
Route::get('/','AdminVisible\AdminPageController#index')->name('page');
Route::get('/banner', 'AdminVisible\BannerController#index');
Route::get('/why-with-us', 'AdminVisible\WhyWithUsController#index');
Route::get('/testimonials', 'AdminVisible\TestimonialsController#index');
Route::get('/about', 'AdminVisible\AboutController#index');
Route::get('/about-why-with-us', 'AdminVisible\AboutWhyWithUsController#index');
Route::get('/general-information', 'AdminVisible\PackageController#index');
Route::get('/package-program', 'AdminVisible\PackageController#index');
Route::get('/cost-exclude', 'AdminVisible\PackageController#index');
Route::prefix('cost-include')->group(function() {
Route::get('/', 'AdminVisible\PackageController#index');
Route::get('/{categories}', 'AdminVisible\CostIncludeController#index');
});
});
});
});
My blade.php file:
#extends('layouts.app')
#section('style')
<link href="{{ asset('css/Admin/sql-data-viewer.css') }}" rel="stylesheet">
<style></style>
#endsection
#section('content')
<section class="data-viewer">
<div class="d-flex justify-content-between">
<h3>Select Package to change</h3>
<button type="button" class="btn add-data text-white rounded-pill">Add <i class="fas fa-plus"></i></button>
</div>
<table>
<thead>
<tr class="data-head">
<td scope="col" style="width: 5%"><input type="checkbox"></td>
<th scope="col" style="width: 8.7%">Id</th>
<td scope="col">Includes</td>
</tr>
</thead>
<tbody>
#foreach ($packages->costIncludes as $include)
<tr class="data">
<td scope="col" style="width: 6.5%"><input type="checkbox"></td>
<th scope="col" style="width: 10%;">{{$include->id}}</th>
<td scope="col" class="text-justify" style="width:696px">{{Str::limit($include->Cost_Include,100)}}</td>
</tr>
#endforeach
</tbody>
</table>
</section>
#endsection
With {page} prefix:
Without {page} prefix:
With {page} prefix when i do dd():
Without {page} prefix when i do dd():
In your CostIncludeController#index, add the new variable. The router is expecting you to handle two variables.
public function index($page, $categories){
$pages = Page::all();
$packages = Package::where('slug',$categories)->first();
return view('admin.pages.costinclude',[
'packages' => $packages,
],compact('pages'));
}
You can confirm the cause of the error by doing a dd($categories) inside your controller function in both cases.
It seems we need to pass $page parameter in the CostIncludesController. This same question is answered in this post:
I am having trouble with my route in laravel
I wont to fetch data from database and show them in a view page as a table. I tried so many ways and didn't work. Also I have used member adding form as a model in my home.blade.php and it works fine.
here is my home.blade.php
<!-- show tasks -->
<div class="container-fluid">
<div class="container mt-4">
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">ID</th>
<th scope="col">Task</th>
<th scope="col">Assigned Date</th>
<th socpe="col">Sign-off Date</th>
<th socpe="col">Edit/Delete</th>
</tr>
</thead>
#foreach($tasks as $task)
<tr>
<td>{{$task->id}}</td>
<td>{{$task->task}}</td>
<td>{{$task->assigned_date}}</td>
<td>{{$task->end_date}}</td>
<td>
Delete
Edite
</td>
</tr>
#endforeach
</table>
</div>
</div>
<!-- end Show tasks -->
here is my taskController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\task;
class taskController extends Controller
{
public function store(Request $request){
$this->validate($request,[
'task'=>['required', 'max:100', 'min:5'],
'assignedDate' => ['required', 'date'],
'endDate' => ['required', 'date'],
]);
$task = new task;
$task->task = $request->task;
$task->assigned_date = $request->assignedDate;
$task->end_date = $request->endDate;
$task->save();
return redirect()->back()->with('message', 'Task added successfuly');
}
public function getdata()
{
$data=task::all();
return view('home')->with('tasks', $data);
}
}
and here is my web.php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::post('/saveTask', 'taskcontroller#store');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
So, What am I doing wrong here? Can somebody please explain me?
Change your Last Route to
Route::get('/home', 'HomeController#getdata')->name('home');
I have two table in my database which are user and the attendance table. What I want to do now is I want to show the attendance data from database according to the user in their attendance view which is linked with their profile. This is my attendance function in the userController.
public function attendance($id)
{
$user = UserProfile::findOrFail($id);
$this->authorize('modifyUser', $user);
return view ('user.attendance', ['user'=>$user]);
}
This is my route to attendance view.
Route::get('/attendance/', ['as' => 'user.attendance', 'uses' => 'UserController#attendance']);
This is my attendance view.
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
<h1><i class="fa fa-university"></i> Attendance</h1>
</div>
<div class="row">
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Date</th>
<th>Time</th>
<th>Present</th>
</tr>
<?php $no=1; ?>
<tr>
<td>{{ $no++ }}</td>
<td>{{$user->attendance->date}}</td>
<td>{{$user->attendance->time}}</td>
<td>{{$user->attendance->present}}</td>
</tr>
</table>
</div>
</div>
</div>
#stop
This is the error that i got.
Type error: Too few arguments to function App\Http\Controllers\UserController::attendance(), 0 passed and exactly 1 expected".
I am new to laravel.
You're getting this error because attendance() method expects an ID and you don't pass it. Change the route to:
Route::get('attendance/{id}', ['as' => 'user.attendance', 'uses' => 'UserController#attendance']);
And pass an ID when creating a link to the attendance() method:
{{ route('user.attendance', ['id' => 1]) }}
Or:
{{ url('attendance/1') }}
If you want to get ID of currently logged in user, do not pass ID. Use auth()-user() instead:
public function attendance()
{
$this->authorize('modifyUser', auth()->user());
return view ('user.attendance');
}
I'm trying to update my 'pay' record in the DB after pressing a button in a table row, but it shows this error when pressing the button:
ErrorException in 0db55b8fee884912074e1a4700061051aeb18bcc.php line
15: Undefined variable: users (View:
/Users/mauricio/code/cnr/resources/views/pages/users.blade.php)
I don't know why it gives me that error when I have use the users variable before in the blade file.
Here is my Registration Controller:
<?php
namespace App\Http\Controllers;
use DB;
use App\User;
class RegistrationController extends Controller
{
public function updatePay(User $id)
{
DB::table('users')->where('id', $id)->update(['pay' => 1]);
return view('pages.users');
}
}
Welcome controller:
public function usersAdmin()
{
$users = DB::table('users')->get();
return view('pages.users', compact('users'));
}
Routes file:
// for admin only
Route::get('/users', 'WelcomeController#usersAdmin');
Route::post('/users/{id}', 'RegistrationController#updatePay');
Here is my view
#extends('layouts.master')
#section('content')
<table class="highlight">
<thead>
<tr>
<th data-field="id" hidden="">ID</th>
<th data-field="name">Nombre</th>
<th data-field="last_name">Apellidos</th>
<th style="text-align: right;">Acciones</th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->last_name}}</td>
<td style="text-align: right; width: 30em">
#if( $user->isAdmin )
{{"ADMINISTRADOR"}}
#elseif( $user->pay )
<a class="waves-effect waves-light btn yellow darken-2"><i class="material-icons left" style="margin:0">create</i></a>
<a class="waves-effect waves-light btn red darken-1"><i class="material-icons left" style="margin:0">delete</i></a>
#else
<form method="POST" action="/users/{{$user->id}}">
{{ csrf_field() }}
<button type="submit" class="waves-effect waves-light btn green lighten-1"><i class="material-icons left" style="margin:0">done</i></button>
</form>
#endif
</td>
</tr>
#endforeach
</tbody>
</table>
#endsection
You need to retrieve users and send it to the view similar like this:
Controllers:
public function methodName()
{
$users = User::all();
return view('path.to.view')->with(compact('users'));
}
Also, You have two choice to use:
First
public function updatePay($id)
Second
public function updatePay(User $user)
{
DB::table('users')->where('id', $user->id)->update(['pay' => 1]);
and instead of return view('pages.users'); use return redirect()->url('users'); in the updatePay method.