Passing input (form) from a page to itself - php

I am working on my first laravel script, trying to submit a form and see the input on the same page. I am working on this problem for days, hopefully someone can solve this. The problem is that i get this error:
Undefined variable: data (View: D:\Programmer\wamp\www\laravel\app\views\bassengweb.blade.php)
view: Bassengweb.blade.index
#extends('master')
#section('container')
<h1>BassengWeb testrun</h1>
<table>
{{ Form::open(array('route' => 'bassengweb', 'url' => '/', 'method' => 'post')) }}
<tr>
<td>{{ Form::label('bassengId', 'Basseng ID')}}</td>
<td>{{ Form::text('bassengId') }}</td>
</tr>
<tr>
<td>{{ Form::label('frittKlor', 'Fritt Klor')}}</td>
<td>{{ Form::text('frittKlor') }}</td>
</tr>
<tr>
<td>{{ Form::label('bundetKlor', 'Bundet Klor')}}</td>
<td>{{ Form::text('bundetKlor') }}</td>
</tr>
<tr>
<td>{{ Form::label('totalKlor', 'Total Klor')}}</td>
<td>{{ Form::text('totalKlor') }}</td>
</tr>
<tr>
<td>{{ Form::label('ph', 'PH')}}</td>
<td>{{ Form::text('ph') }}</td>
</tr>
<tr>
<td>{{ Form::label('autoPh', 'Auto PH')}}</td>
<td>{{ Form::text('autoPh') }}</td>
</tr>
<tr>
<td>{{ Form::submit('Lagre målinger') }}</td>
{{ Form::close() }}
</table>
#if($data)
{{ $data->bassengId }}
#endif
#stop
Homecontroller.php
<?php
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
public function showTest()
{
return View::make('test');
}
public function getInput()
{
$input = Input::all();
print_r($input); die();
return View::make('bassengweb')->with('data', '$input');
}
}
view: master.blade.php
<div class="container">
#yield('container')
</div>
routes.php
Route::get('/', function()
{
return View::make('bassengweb');
});
//Route::post('/', array('uses'=>'HomeController#getInput'));
Route::post('/',function(){
$input = Input::all();
//for inspecting input
print_r($input);
die();
//to send input to view but before sending to view comment last two lines
return View::make('bassengweb')->with('data',$input);

You enclosed $input in single quotes in your HomeController.php, instead try:
<?php
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
public function showTest()
{
return View::make('test');
}
public function getInput()
{
$input = Input::all();
print_r($input); die();
return View::make('bassengweb')->with('data', $input);
}
}

In your routes.php, change route registration a little.
Route::get('/', function()
{
$data = Input::all();
return View::make('bassengweb', compact('data'));
});

Related

Call to a member function count() on null

When I'm going to add data an error appears "Call to a member function count() on null"
View
#foreach ($data as $row)
<tr>
<td>{{ $row->customer}}</td>
<td>{{ $row->vessel}}</td>
<td>{{ $row->scopejob}}</td>
<td>{{ $row->pergantian_sparepart}}</td>
<td>{{ $row->teknisi}}</td>
<td>{{ $row->tahun}}</td>
<td>{{ $row->keterangan}}</td>
<td>{{ $row->images->count()}}</td>
<td>{{ $row->files->count()}}</td>
<td>
FOTO
FILE
EDIT
<a href="#" class="btn btn-danger delete m-1" data-id="{{ $row->id}}" data-customer="{{ $row->customer}}" >DELETE</a>
</td>
</tr>
#endforeach
Controller rekap:
public function insert_rekap(Request $request) {
// dd($request->all());
$this -> validate($request,[
'customer'=>'required',
'vessel'=>'required',
'scopejob'=>'required',
'pergantian_sparepart'=>'required',
'teknisi'=>'required',
'tahun'=>'required',
'keterangan'=>'required'
]);
$data = rekap::create($request->all());
if($request->hasFile("images")){
$images=$request->file("images");
foreach($images as $image){
$imageName=time().'_'.$image->getClientOriginalName();
$request['rekap_id']=$data->id;
$request['image']=$imageName;
$image->move(public_path("/images_rekap"),$imageName);
Image_rekap::create($request->all());
}if($request->hasFile("files")){
$files=$request->file("files");
foreach($files as $file){
$fileName=time().'_'.$file->getClientOriginalName();
$request['rekap_file_id']=$data->id;
$request['file']=$fileName;
$file->move(\public_path("/rekap_file"),$fileName);
file_rekap::create($request->all());
}
}
}
return redirect()->route('rekap')->with('success','Data Berhasil Ditambahkan');
}
but the data is successfully entered into the database, and what steps should I take to correct the error
error tells you that images or files or both variables are null, looks like you don't use eager loading
quick and dirty way is to use optional helper
<td>{{ optional($row->images)->count()}}</td>
<td>{{ optional($row->files)->count()}}</td>
if you need only count then better approach is to use eloquent counting related models
// controller
$data = rekap::withCount(['images', 'files'])
// other selecting rules
//->where ...
->get();
return view('view_name', ['data'=>$data]);
now in every $row of $data you have fields images_count and files_count
<td>{{ $row->images_count }}</td>
<td>{{ $row->files_count }}</td>
also you can rename count variables like rekap::withCount(['images as imagesCount', 'files as filesCount'])
<td>{{ $row->imagesCount }}</td>
<td>{{ $row->filesCount }}</td>
Try
<td>{{ $row->images ? $row->images->count() : '' }}</td>

Undefined variable: blogcat (View: /home/techpriest/joseph/resources/views/admin/view_category.blade.php)

Undefined variable: blogcat (View: /home/techpriest/joseph/resources/views/admin/view_category.blade.php) this is my error message in laravel 8 but when I look at my code all seems well.
here is my code.
AdminController
public function addBlogCat (Request $request){
if ($request->isMethod('post')) {
$data = $request->all();
$blogcat = new Foliocategories;
$blogcat->name = $data['category_name'];
$blogcat->save();
return redirect ('/blog/categories');
}
return view ('admin.view_category');
}
public function viewBlogCat (){
$blogcat = DB::select('select * from foliocategories');
return view ('admin.view_category', ['blogcat'=>$blogcat]);
}
And here is my view;
#foreach($blogcat as $blogcat)
<tr>
<td>{{ $blogcat->id }}</td>
<td>{{ $blogcat->name }}</td>
<td>{{ $blogcat->created_at }}</td>
<td><span class="badge badge-danger">Due</span></td>
<td class="action h4">
<div class="table-action-buttons">
<a class="edit button button-box button-xs button-info" href="#"><i class="zmdi zmdi-edit"></i></a>
<a class="delete button button-box button-xs button-danger" href="#"><i class="zmdi zmdi-delete"></i></a>
</div>
</td>
</tr>
#endforeach
I found two issues here. In the addBlogCat action, you don't inject any blog category. The viewBlogCat is not bad, but in the view, you are using a loop with a model instance.
What about rewrite the code to this, lets use full meaningful variable names:
public function addBlogCategory(Request $request)
{
if ($request->isMethod('post')) {
$data = $request->all();
Foliocategories::create(['name' => $request->input('category_name')]);
return redirect ('/blog/categories');
}
return view ('admin.view_category', [
'blogCatogories' => Foliocategories::all();
]);
}
public function viewBlogCategories()
{
$blogCategories = Foliocategories::all();
return view ('admin.view_category', compact('blogCategories'));
}
Then in the view:
#foreach($blogCategories as $blogCategory)
<tr>
<td>{{ $blogCategory->id }}</td>
<td>{{ $blogCategory->name }}</td>
<td>{{ $blogCategory->created_at }}</td>
</tr>
#endforeach

How to use json decoded data in blade laravel

part of this data in store method decoded and i want to use this encoded data in blade template:
How is this possible?
public function store(VehiclesRequest $request)
{
$data = Vehicle::create([
'container_type' => $request->container_type,
'type' => $request->type,
'delivery_capacity' => $request->delivery_capacity,
'plate_number' => json_encode($request->plate_number),
'chasis_number' => $request->chasis_number,
'capacity_dimensions' => json_encode($request->capacity_dimensions),
'fuel_consumption_rate' => $request->fuel_consumption_rate,
'capacity_weight' => $request->capacity_weight,
'insurance_expire_date' => json_encode($request->insurance_expire_date),
'insurance_type' => $request->insurance_type,
'is_available' => $request->is_available,
]);
return redirect()->action('VehicleController#index');
}
VehicleController:
public function index()
{
$vehicles = Vehicle::all();
return view('vehicle.index', compact('vehicles'));
}
view:
<tbody>
#foreach(vehicles as $vehicle)
<tr>
<td>{{ $vehicles->plate_number }}</td>
<td>{{ $vehicles->delivery_capacity }}</td>
</tr>
#endforeach
</tbody>
i used from {{ json_decode($vehicles->plate_number) }} in blade template but an error occurred.
You can Decode json data in view.blade.php like this :
<tbody>
#foreach($vehicles as $vehicle)
<tr>
<td>
#foreach(json_decode($vehicles->plate_number) as $plate)
{{ $plate['variable_name'] }}
#endforeach
</td>
<td>{{ $vehicles->delivery_capacity }}</td>
</tr>
#endforeach
</tbody>

Laravel one to many relationship doesn't work anymore

This relationship of mine was working last week, but it stopped working.
When i use it in my blade template it says: Trying to get property 'name' of non-object.
The weird thing is, when i use it in dd() it perfectly shows, but when i try to print it in blade, it doesn't.
Code PaymentsController
<?php
namespace App\Http\Controllers;
use App\Betaling;
use App\Settings;
use Illuminate\Http\Request;
class PaymentsController extends Controller
{
public function index(){
$user_id = auth()->user('id');
$role = auth()->user()->role_id;
$settings = Settings::where('user_id', $user_id->id)->first();
if ($role === 1){
$totaalaantalbetalingen = Betaling::all();
} else {
$totaalaantalbetalingen = Betaling::where('user_id', $user_id->id)->get();
}
return view('payments.index', compact(['totaalaantalbetalingen','role', 'user_id', 'settings']));
}
}
Code index.blade.php
#foreach($totaalaantalbetalingen as $betaling)
<tr>
<th scope="row">{{ $betaling->id }}</th>
#if($role === 1)
<td>{{ $betaling->user->name }} </td>
#endif
<td>{{ $betaling->customer->name }}</td>
<td>{{ $betaling->reference}}</td>
<td>€ {{ $betaling->amount}}</td>
<td>{{ $betaling->time_of_invoice }}</td>
<td>{{ $betaling->time_of_payment }}</td>
<td></td>
<td>{{ $betaling->user->location }}</td>
</tr>
#endforeach
code User model:
public function betalingen(){
return $this->hasMany(Betaling::class);
}
code Betaling model:
public function customer(){
return $this->belongsTo(User::class);
}
public function user(){
return $this->belongsTo(User::class);
}
code Customer model:
public function betaling(){
return $this->hasMany(Betaling::class);
}
pass auth()->user()->id and in blade file check !empty($totaalaantalbetalingen)
code User model:
public function betalingen(){
return $this->hasMany('App\Betaling','betalingenid','id'); // pass foreign key for ex.
}
code Betaling model:
public function customer(){
return $this->belongsTo('App\User','customer_id','id');// pass foreign key for ex.
}
public function user(){
return $this->belongsTo('App\User','user_id','id');// pass foreign key for ex.
}
code Customer model:
public function betaling(){
return $this->hasMany('App\Betaling','customer_id','id');// pass foreign key for ex.
}
Controller
public function index(){
$user_id = auth()->user()->id;
$role = auth()->user()->role_id;
$settings = Settings::where('user_id', $user_id)->first();
if ($role == 1){
$totaalaantalbetalingen = Betaling::all();
} else {
$totaalaantalbetalingen = Betaling::where('user_id', $user_id)->get();
}
return view('payments.index', compact(['totaalaantalbetalingen','role', 'user_id', 'settings']));
}
blade.php
#if(!empty($totaalaantalbetalingen) && isset($totaalaantalbetalingen))
#foreach($totaalaantalbetalingen as $betaling)
<tr>
<th scope="row">{{ $betaling->id ?? '' }}</th>
#if($role === 1)
<td> {{ $betaling->user->name ?? '' }} </td>
#endif
<td> {{ $betaling->customer->name ?? '' }} </td>
<td> {{ $betaling->reference ?? '' }} </td>
<td> € {{ $betaling->amount ?? '' }} </td>
<td> {{ $betaling->time_of_invoice ?? '' }} </td>
<td> {{ $betaling->time_of_payment ?? '' }} </td>
<td> </td>
<td> {{ $betaling->user->location ?? '' }} </td>
</tr>
#endforeach
#endif

Can't pass data in view (laravel 4)

I am trying to pass an array from controller in view but it doesn't show error because it rezognise the variable but the data are not there, meanwhile when i try dd() in controller, i have data there.
Below is my code:
//Controller
public function showMessage(){
$id = Input::get('recordid');
foreach ($id as $chat_id) {
$history = Chat::where('chat_id','=', $chat_id)->get();
return View::make('chat.chatview', compact($history));
}
}
//View
#extends ('master')
#section ('content')
<div class="container">
<h1 style="color: white;">Chat History</h1>
<?php if (isset($history)) { ?>
#foreach($history as $row)
<table>
<tr>
<td><?php echo $history ;?></td>
</tr>
</table>
#endforeach
<?php } ?>
</div>
#stop
//route
Route::get('chathistory', function(){
return View::make('chat/chatview');});
The array that I get is from another view with the code below:
//view
#foreach($chat as $row)
<tr>
<td>{{ $row->chat_id }}</td>
<td>{{ $row->fullname }}</td>
<td>{{ $row->email }}</td>
<td>{{ $row->transcript_text }}</td>
<td>{{ $row->duration }}</td>
<td>
<input type="submit" value="History" class="btn pull-left" name="status[]" id="status[]" onclick="javascript:changeStatus('{{$row->chat_id}}','{{$arr}}')"/>
</td>
</tr>
<input name="recordid[]" id="recordid[]" type="hidden">
<?php $arr++; ?>
#endforeach
<script type="text/javascript">
function changeStatus(id, arr){
$('[id^=recordid]').eq(arr).val(id);
}
</script>
//Route
Route::get('individual', function(){
$chat = Chat::all();
return View::make('chat/individual')->with('chat', $chat);});
Route::post('individual','HomeController#showMessage');
Change the following line:
return View::make('chat.chatview', compact($history));
to
return View::make('chat.chatview', ['history' => $history]);
or
return View::make('chat.chatview')->with(compact('history'));
and try again.
Change here
return View::make('chat.chatview')->with('history',$history);

Categories