laravel controller and view issue - laravel

I need a get field value total sum. But I can't and don't know where the problem is.
controller code:
public function index()
{
$user1 = Auth::user();
$user2 = $user1->client_id;
$withdraws = Withdraw::where('client_id', $user2)->get();
$deposits = Deposit::where('client_id', $user2)->get();
return view('home')->with(compact('deposits', 'withdraws', ));
}
view code:
#foreach($deposits as $deposit)
<td id="count-data-td">{{ $deposit->amount }}</td>
#endforeach

return view('home')->with('transfers', array('deposits'=>$deposits, 'withdraws'=>$withdraws));
and view code
#foreach($transfers['deposits']) as $deposit)
{{ $deposit->amount }}
#endforeach
but if you need the sum you can always do this:
{{ $transfers['deposits']->sum('amount') }}
in your case :
{{ $deposits->sum('amount') }}

CONTROLLER
public function index()
{
$user1 = Auth::user();
$user2 = $user1->client_id;
$withdraws = Withdraw::where('client_id',$user2)->get();
$deposits = Deposit::where('client_id', $user2)->get();
return view('home')->with(['deposits' => $deposits, 'withdraws',$withdraws]);
}
VIEW
{{ $deposits->sum('amount') }}
{{ $withdraws->sum('amount') }}
NB: You don't need to run the foreach loop to get the sum

Related

Check SHA-256 Laravel

I have a system that requires a homeowner to submit a form about their guest's details. When they submit a form, each will be assigned a unique 6-digit code. This code will be hashed with SHA-256. In the admin's view, there is a list of all submitted forms. I want to do a search function where when I enter the code, the system will look through the hashed code and check if it exists or not.
This is my GuestController:
public function index()
{
//returns admin's view
$guest = Guest::all();
return view('pages.guest.index', compact('guest'));
}
public function store(Request $request)
{
$guest = new Guest;
$guest->code = random_int(100000, 999999);
$guest->hash = hash('sha256', $guest['code']);
$guest->owner_id = Auth::user()->id;
$guest->owner = Auth::user()->name;
$guest->unit = Auth::user()->unit;
$guest->guestname = $request->input('guestname');
$guest->guestphone = $request->input('guestphone');
$guest->guestic = $request->input('guestic');
$guest->guestcar = $request->input('guestcar');
$guest->numberofguests = $request->input('numberofguests');
$guest->datevisit = $request->input('datevisit');
$guest->timevisit = $request->input('timevisit');
$guest->save();
return redirect('show-pass')->with('status', 'Guest Added Successfully');
}
public function search(Request $request)
{
//Get the search value from the request
$search = $request->input('search');
//Search in the code from the list
$guest = Guest::query()
->where('code', 'LIKE', "%{$search}%")
->get();
//Return the search view with the results compacted
return view('pages.guest.search', compact('guest'));
}
This is my search result blade:
<div class="card-body">
#if($guest->isNotEmpty())
#foreach ($guest as $item)
<div class="post-list">
<p>Owner : {{ $item->owner }}</p>
<p>Unit : {{ $item->unit }}</p>
<p>Guest Name : {{ $item->guestname }}</p>
<p>Guest Phone Number : {{ $item->guestphone }}</p>
<p>Guest IC Number : {{ $item->guestic }}</p>
<p>Guest Car Number : {{ $item->guestcar }}</p>
<p>Date : {{ $item->datevisit }}</p>
<p>Time : {{ $item->timevisit }}</p>
</div>
#endforeach
#else
<div>
<h4>No guests with the code was found</h4>
</div>
#endif
</div>
Is what I'm trying to do possible? If yes, how can I edit my search method to be able to do so? May I get some help?

Trying to get property 'name' of non-object

In code Controller recieving data from model but failed to show in blade view file.
This is blade view code.
#for($id = 0;$id < 59;$id++)
#foreach($data[$id] as $data)
{{$data->name}}
#endforeach
#endfor
Here is the controller
public function cart(){
$data = array();
$data['flashSale'] = Product::flashSale();
$cartProdId = Session::get('prodId');
for ($id = 0;$id<sizeof($cartProdId);$id++){
$data[$id] = Product::getCartProduct($cartProdId[$id]);
}
return view('user.cart')->with('data',$data);
}
you are using two loops unnecessarily..you can achieve this by using only one loop if it is for or foreach loop...
You can do it by single foreach loop. It will save execution/loading time.
#foreach($data as $key => $value)
{{$value->name}}
#endforeach
Do smart work!! .

Passing variables,arrays and objects to view with compact

I have a method in laravel that aims to pass data to views. I have data in variables, arrays and objects.
This is the method
public function view($id){
$id = request()->segment(2);
$items = Role::all(['name', 'display_name']);
return view('text.ServicesEditUser',compact('items',$items));
}
I also have this code
$uid = Auth::id();
$settings = Settings::where('user_id','=',Auth::id())->first();
return view('text.settings',compact('settings'));
that i want to incorporate to the code above. Once i pass the settings to the view, i can access data like $settings->language for instance. I also want to pass a variable which is
$title = 'degrees of tilt';
to the first code snippet's compact method.
This is the method now with all the data i want to pass to the view
public function view($id){
$id = request()->segment(2);
$title = 'Tilt in degrees';
$settings = Settings::where('user_id','=',Auth::id())->first();
$users = User::where('id','=',$id)->get();
$items = Role::all(['name', 'display_name']);
return view('text.ServicesEditUser',compact('items',$items));
}
How do i pass $settings ,$title,$users , $items to the view and how do i access each in view?
When returning a view you can pass multiple variables like this.
return view('text.ServicesEditUser')
->with(compact('items',$items))
->with('variable2', $variable2);
See here the documentation about this - Laravel Views.
In controller:
$variable = "Var";
$array = ["first" => "a", "second" => "b"];
$object = (object) $array;
return view('view',compact(['variable', 'array', 'object']));
In view:
{{$variable}}
{{$array['first']}}
{{$object->first}}
compact() doesn't need dollar at start.
You can just add your code to view by:
$title = 'My title';
$settings = ['my', 'settings',];
return view('text.settings', compact('title', 'settings'));
Anyway you can pass your variables by array:
$settings = ['my', 'settings',];
return view('text.settings', [
'title' => 'Title',
'settings' => $settings,
]);
The last option is with() method:
return view('text.settings')
->with('settings', $settings)
->with('title', 'My title');
Get access to variables in the view by {{ $title }} and
#foreach ($settings as $sett)
{{ $sett }}
#endforeach
This did also work
public function view($id){
$id = request()->segment(2);
$title = 'Degrees of tilt';
$settings = Settings::where('user_id','=',Auth::id())->first();
$users = User::where('id','=',$id)->get();
$items = Role::all(['name', 'display_name']);
return view('text.ServicesEditUser', compact('title', 'settings','users','items'));
}
in the blade view
<p>{{$title}}</p>
{{$settings->language}}
#foreach($items as $item)
<option value="{{$item->name}}">{{$item->display_name}}</option>
#endforeach
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->email}}</option>
#endforeach
In settings
#if($settings)
$settings->language
#endif

Intervention \ Image \ Exception \ ImageNotWritableException using Laravel 4

I am using Laravel 4. I am not sure why I am getting this error when everything seems to be correct. Also, the product is not updating to the database.
Error: Intervention \ Image \ Exception \ ImageNotWritableException
Can't write image data to path [/img/products/1396668877.jpg]
Snippet of ProductsController where product object is created:
public function postCreate() {
$validator = Validator::make(Input::all(), Product::$rules);
if ($validator->passes()) {
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
Image::make($image->getRealPath())->resize(468, 249)->save('/img/products/'.$filename);
$product->image = 'img/products/'.$filename;
$product->save();
return Redirect::to('admin/products/index')
->with('message', 'Product Created');
}
return Redirect::to('admin/products/index')
->with('message', 'Something went wrong')
->withErrors($validator)
->withInput();
}
product object passed to view
#foreach($products as $product)
<li>
{{ HTML::image($product->image, $product->title, array('width'=>'50')) }}
{{ $product->title }} -
{{ Form::open(array('url'=>'admin/products/destroy', 'class'=>'form-inline')) }}
{{ Form::hidden('id', $product->id) }}
{{ Form::submit('delete') }}
{{ Form::close() }} -
{{ Form::open(array('url'=>'admin/products/toggle-availability', 'class'=>'form-inline'))}}
{{ Form::hidden('id', $product->id) }}
{{ Form::select('availability', array('1'=>'In Stock', '0'=>'Out of Stock'), $product->availability) }}
{{ Form::submit('Update') }}
{{ Form::close() }}
</li>
#endforeach
Products Model
<?php
class Product extends Eloquent {
protected $fillable = array('category_id', 'title', 'description', 'price', 'availability', 'image');
public static $rules = array(
'category_id'=>'required|integer',
'title'=>'required|min:2',
'description'=>'required|min:20',
'price'=>'required|numeric',
'availability'=>'integer',
'image'=>'required|image|mimes:jpeg,jpg,bmp,png,gif'
);
public function category() {
return $this->belongsTo('Category');
}
}
products table in the database
public function up()
{
Schema::create('products', function($table){
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->string('title');
$table->text('description');
$table->decimal('price', 6, 2);
$table->boolean('availability')->default(1);
$table->string('image');
$table->timestamps();
});
}
Make sure the public/img/products folder exists and it's writable and also try to use absolute path if necessary, like this:
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/products/' . $filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
replace :
Image::make($image->getRealPath())->resize(468, 249)->save('/img/products/'.$filename);
with:
Image::make($image->getRealPath())->resize(468, 249)->save('public/img/products/'.$filename);
you must specify the public folder for the save method.

Laravel 4 pulling data from database

I have table called 'players'. I want to pull every player from the database, or simply select players that have in column 'online' = 1. I want to display their name (column 'name') in 'players' table.
Here's what I've tried:
public function online()
{
$results = Player::with('online')->find(1)
return View::make('aac.test')->with('results', $results);
}
also tried:
public function online()
{
$results = DB::select('select * from players where level = 1');
return View::make('aac.test')->with('results', $results);
}
None of them works.
Try this:
public function online()
{
$results = Player::where('online', 1)->get('name');
return View::make('aac.test')->with('results', $results);
}
To display it using blade:
<ul>
#foreach($results as $result)
<li>
{{$result->name}}
</li>
#endforeach
</ul>
public function online()
{
$results = Player::where('level','=','1')->get();
return View::make('aac.test')->with('results', $results);
}
To display it using blade:
<ul>
#foreach($results as $result)
<li>
{{$result->name}}
</li>
#endforeach
</ul>

Resources