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.
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?
My View:
<table border="1" style="text-align: left;">
<td>
<b>Firstname</b>
</td>
<td>
<b>Actions</b>
</td>
#foreach($Newslist as $News)
<tr>
<td>
{{ $News->lastname }}
</td>
<td>
<button type="button" class="btn btn-primary">Edit</button>
</td>
</tr>
#endforeach
</table>
My routes/web.php:
Route::Post('NewsEdit/{{$News->id}}/EditForm','NewsControllere#EditForm');
My Controller.php:
<?php
namespace App\Http\Controllers;
use App\Newsmodel;
use Illuminate\Http\Request;
class NewsControllere extends Controller
{
public function EditForm($NewsId)
{ dd(request()->all());
echo "deepakkeynes";exit();
//$Newsmodel = Newsmodel::find($NewsId);
//return view('/News')->with('News',$Newsmodel);
}
}
While clicking the edit button in the view, the following result is obtained:
Result: 404 page
Expected Result: The Id of the edit button along with the echo value!
Can anyone help me out? I am a newbie in laravel..!
Have a look at this LaraCast tutorial on Route Model Binding. This explains the underlying documentation well.
Essentially:
routes/web.php:
Route::get('NewsEdit/{news}/EditForm','NewsControllere#EditForm');
newsController.php:
public function EditForm(Newsmodel $news)
{
return view('/News')->with('News',$news);
}
So right now I'm working on project that needs price summing on each payment method, for example if somebody is paying with cash it needs to sum total price for every cash payment. Here is my code
This is my home.blade.php :
#extends('layouts.theme')
#section('content')
<style>
body{
margin: 0;
background-color: #EBEBEB;
}
</style>
<div class="mainl">
<div class="row">
<div class="columna">
<h1>{{ Auth::user()->name }}</h1>
<hr>
</div>
<div class="columns">
<a href="{{ route('logout') }}" id="logout"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('LOGOUT') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
</div>
</div>
</br></br></br>
#if(count($posts)> 0)
<table>
<thead>
<tr>
<th>BR.KESICE</th>
<th>IME I PREZIME</th>
<th>BR.TELEFONA</th>
<th>POSAO</th>
<th>CIJENA</th>
<th>PLACANJE</th>
<th>POPUST</th>
<th>DATUM PREUZ.</th>
<th>DATUM IZDAV.</th>
<th>SMJENA</th>
<th>RADNIK</th>
<th>STATUS</th>
<th>IZMIJENI</th>
</tr>
</thead>
<tbody>
#foreach($posts as $post)
<tr>
<td>{{$post->br_kesice}}</td>
<td>{{$post->ime}}</td>
<td>{{$post->br_telefona}}</td>
<td>{{$post->posao}}</td>
<td>{{$post->cijena}}</td>
<td>{{$post->placanje}}</td>
<td>{{$post->popust}}</td>
<td>{{$post->datum_preuz}}</td>
#if($post->status == 1)
<td>/</td>
#else
<td>{{$post->datum_izdav}}</td>
#endif
<td>{{$post->smjena}}</td>
<td>{{$post->radnik}}</td>
<td>
#if($post->status == 0)
<span class="label label-primary" id="statusdeaktivan">Deaktivan</span>
#elseif($post->status == 1)
<span class="label label-success" id="statusaktivan">Aktivan</span>
#elseif($post->status == 2)
<span class="label label-danger" id="statusdeaktivan">Rejected</span>
#else
<span class="label label-info" id="statusdeaktivan">Deaktivan</span>
#endif
</td>
#if($post->status == 3)
#else
<td><i class="far fa-edit"></i></td>
#endif
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th>UKUPAN IZNOS: {{ Auth::user()->posts->sum('cijena') }}€</th>
<th>KARTICA: {{ Auth::user()->posts->where('placanje', 'Kartica')->where('created_at','=',$date)->sum('cijena')}}€</th>
</tr>
</tfoot>
</table>
<br><br>
{{ $posts->links() }}
#else
<p>Trenutno nema unosa.</p>
#endif
</div>
#endsection
In this home.blade.php I put where('created_at','=',$date)
from my HomeController but it's not working. In my homepage it only shows posts made today. And this is how I did it in my
HomeController.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Post;
use DB;
use Auth;
use Carbon\Carbon;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Contracts\Support\Renderable
*/
public function index()
{
$date = new Carbon(request('date'));
$posts = Post::where('user_id', Auth::id())
->whereDate('created_at','=',$date)
->orderBy('created_at', 'DESC')
->paginate(30); //add {{ $posts->links() }} if paginate is enabled
return view('home', compact('date', $date))->with('posts', $posts);
}
}
So all I need is when manager create post and type in method of payment for example 'Kartica', in my homepage it table footer it needs to sum total payment of all posts MADE TODAY and all price for 'Kartica' payment method also from TODAY. If anyone have idea please let me know, thanks !
created_at field use YYYY-MM-DD HH:II:SS format so you have to filter by date not by datetime.
You can do something like this:
public function index()
{
$date = new Carbon(request($date))->format("Y-m-d");
...
->whereRaw('DATE(created_at) = ?', $date)
...
}
whereRaw method can be used to inject a raw where clause into your query.
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.
I have Tried to fetch data from MySQL db using eloquent in my laravel application it shows Parse Error here my code :
Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class design extends Model {
//Table Name
protected $table='designs';
//Primary key
public $primarykey='id';
// TimeStamp
public $timestamps=true;
}
Controller Page:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\design;
class designController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
$design=design::all();
//$design = design::select('Design No','Design Name','Chain Weight/Length','status')->where('status','=','Active')->get();
return view('pages.design')->with('design',$design);
}
}
page for listing:
the error occurring while using for-each
#extends('layouts.layout')
#section('content')
<div class="main">
<div class="container col-md-12 col-sm-12 col-xs-12">
<h2 class="text-center">Designs</h2>
#if(count($design)>=1)
<h2>Done</h2>
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>Design No</th>
<th>Design Name</th>
<th>Weight/Lenght</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($design as $design)
<tr>
<td>{{$design->Design No}}</td>
<td>{{$design->Design Name}}</td>
<td>{{$design->Chain Weight/Length}}</td>
<td>
<button type="button" class="btn btn-warning">Edit</button>
<button type="button" class="btn btn-danger">{{$design->status}}</button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endif
</div>
</div>
#endsection
it doesn't allow space in column name it should be in lower cases as design_no , design_name in your DB
and then fetch as
#foreach($design as $new_designs)
<td>{{$new_designs->design_no }}</td>
// and so on
#endforeach
Hope it will work.
try this
{{$design->{'Design No'} }}
Yous should try this:
Controller Page
public function index()
{
//
$designs=design::all();
return view('pages.design',compact('designs'));
}
View Page
#foreach($designs as $design)
<tr>
<td>{{$design->Design_No}}</td>
<td>{{$design->Design_Name}}</td>
<td>{{$design->Chain_Weight/Length}}</td>
<td>
<button type="button" class="btn btn-warning">Edit</button>
<button type="button" class="btn btn-danger">{{$design->status}}</button>
</td>
</tr>
#endforeach