Undefined variable in displaying values of index controller - laravel

I am new to Laravel, I know I'm just missing out on something yet I cant seem to find it.
I am sending compact('tabletinfo') from my controller index to home.blade.php under tablet folder yet my home.blade.php can't seem to find tabletinfo variable.
Here's the code for index controller.
public function index()
{
$tabletinfo = Tablets::all();
return view ('tablet.home', compact('tabletinfo'));
}
CODE FOR home.blade.php
<tbody>
#foreach ($tabletinfo as $tablet)
<tr>
<td>{{$tablet->owner_name}}</td>
<td>{{$tablet->owner_address}}</td>
<td>
<button class="btn btn-success" type="button" data-toggle="modal" data-target="#viewModal">View Details</button>
<button class="btn btn-info edit" type="button" data-toggle="modal" data-target="#editModal">Edit Details</button>
<button class="btn btn-danger" type="button">Delete</button>
</td>
</tr>
#endforeach
</tbody>
I guess my main problem is my view can't get the tabletinfo variable from my index controller.

there is a mistake. you write tablets instead of tablet
return view('tablet.home',compact('tabletinfo));

Can you please try this one ?
return view('tablet.home')->with('tabletinfo',$tabletinfo);
Or you can use this like
return view('tablet.home',compact['tabletinfo']);

Related

Display data using pivot table

I have 2 tables which users and groups and I also has created a pivot table for them.
What I want to display is list of the group that user have not joined yet.
my controller
public function redirectFunct()
{
$user = User::find(Auth::user()->id);
$groups=Group::all();
$exists = DB::table('group_user')
->whereuser_id(Auth::user()->id)
->count() > 0;
return view('member.dashboard',['user'=>$user,'groups'=>$groups,'exists'=>$exists]);
}
blade file
#foreach($groups as $groups)
#if($!exists)
<tbody>
<tr>
<td>{{$groups->id}}</td>
<td>{{$groups->groupName}}</td>
<td>{{$groups->groupDesc}}</td>
<td><button type="button" class="btn btn-primary .btn-{color}">Details</button></td>
<td><a href="{{url('/join',$groups->id)}}"><button type="button" class="btn btn-secondary .btn-{color}">Join</button></td>
</tr>
</tbody>
#endif
#endforeach
I do not sure how to check if the data is exist or not in the pivot table. Please help me.
you could try this in your $exist I'm not sure what ->count() > 0; is use for
$exists = DB::table('Group')
->where('userID', Auth::user()->id)
->get();
If you want to display only the group that the user doesn't exist in group you could try this
<tbody>
#foreach($groups as $group)
#if('your group parent id' != 'your user id that related to the group')
<td>{{$group->id}}</td>
<td>{{$group->groupName}}</td>
<td>{{$group->groupDesc}}</td>
<td><button type="button" class="btn btn-primary .btn-{color}">Details</button></td>
<td><a href="{{url('/join',$groups->id)}}"><button type="button" class="btn btn-secondary .btn-{color}">Join</button></td>
#endif
#endforeach
</tbody>
this is the screenshot of that page
The pivot table

How to add number_format decimal in laravel in html view?

does anyone know how to add the decimal number_format here?, I don't use the blade from Laravel but I route the view directly to html... so if I add the number_format attribute, I'm confused ;(
<tr dir-paginate="income in incomes | filter:searchText | itemsPerPage:20" total-items="totalItems">
<td>{{income.incomeTitle}}</td>
<td>{{income_cat[income.incomeCategory]}}</td>
<td>Rp.{{income.incomeAmount}}</td>
<td>{{income.incomeDate}}</td>
<td>{{income.incomeNotes}}</td>
<td>
<a ng-show="income.incomeImage != ''" target="_blank" href="{{'index.php/incomes/download/'+income.id}}"><button type="button" class="btn btn-success btn-circle" title="{{phrase.Download}}" tooltip><i class="fa fa-cloud-download"></i></button></a>
<button ng-show="$root.can('Incomes.editIncome')" ng-click="edit(income.id)" type="button" class="btn btn-info btn-circle" title="{{phrase.Edit}}" tooltip><i class="fa fa-pencil"></i></button>
<button ng-show="$root.can('Incomes.delIncome')" ng-click="remove(income,$index)" type="button" class="btn btn-danger btn-circle" title="{{phrase.Remove}}" tooltip><i class="fa fa-trash-o"></i></button>
</td>
</tr>

Submit Button not getting correct route to add new data in Laravel

I am facing problem for Submit Button not getting correct route to add new data.
2 Blade file with 2 Submit Button which need to save 2 data to 2 different tables.
Problem facing - Data submitted in 1 table for both submit button....
addnewBuffalo.blade.php
<div class="box-header with-border" align="right">
<button type="submit" class="btn btn-success btn"><i class="fa fa-floppy-o"></i> <b>Add
Buffalo</b></button><a href="" class="btn btn-warning btn"><i class="fa fa-refresh">
</i> <b> Refresh</b></a>
</div>
addnewcow.blade.php
<div class="box-header with-border" align="right">
<button type="submit" class="btn btn-success btn"><i class="fa fa-floppy-o"></i> <b>Add
Cow</b></button><a href="" class="btn btn-warning btn"><i class="fa fa-refresh"></i> <b>
Refresh</b></a>
</div>
web.php
Route:: post('submit', 'App\Http\Controllers\addnewbuffaloController#addbuffaloData')
->name('submit');
Route:: post('submit', 'App\Http\Controllers\addnewcowController#addcowData')
->name('submit');
You need to create 2 separate Route Names. Having ->name('submit') for both won't work. And having the same path won't work
Route::post('submit=buffalo', 'App\Http\Controllers\addnewbuffaloController#addbuffaloData')
->name('submit-buffalo');
Route::post('submit-cow', 'App\Http\Controllers\addnewcowController#addcowData')
->name('submit-cow');
Or if the form is wrapped by one form tag then you can handle both submissions in the one Controller action
<button type="submit" name="buffalo-submission">Save Buffalo</button>
<button type="submit" name="cow-submission">Save Cow</button>
Then in the controller action code you could check
if ($request->has('buffalo-submission') {
Do buffalo code
} else if ($request->has('cow-submission')) {
do cow code
}

Individual ID's pdf print by Laravel8

I'm studying laravel using this gentleman's awesome code.
https://github.com/savanihd/Laravel-8-CRUD/blob/master/app/Http/Controllers/ProductController.php
I use Laravel Framework 8.48.2
I would like to add PDF pritabile view to each ID's "show" page
I could success PDF export using this simple test page.
public function generate_pdf() {
$pdf = \PDF::loadView('products.generate_pdf');
return $pdf->stream('yokoya_test.pdf');
}
so I had been trying to add PDF export to "show" function .
Here is my current code
Controller
public function each_print(Product $product)
{
$pdf = \PDF::loadView('products.each_print');
return $pdf->stream('each_print.pdf');
}
original index.blade.php code is this
<a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a>
I changed this as below
<a class="btn btn-info" href="{{ route('products.each_print',$product->id) }}">Print</a>
Here is my current WEB.php
Route::get('products/each_print', [ProductController::class, 'each_print']);
Route::resource('products', ProductController::class);
but I got this error
Symfony\Component\Routing\Exception\RouteNotFoundException
Route [products.each_print] not defined.
C:\xampp0827\htdocs\lara\ic0630-simple\vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php:427
Could you teach me right code please?
UPDATE
index.blade.php
https://jsfiddle.net/diessses/m72bs54r/1/
each_print.blade.php
https://jsfiddle.net/diessses/8Lg5wot3/
you need define the name to the route like:
Route::get('products/each_print/{product}', [ProductController::class, 'each_print'])->name('products.each_print');
UPDATED
first refactor your code in the index blade table like:
<td>
<a class="btn btn-info" href="{{ route('products.show',$product->id) }}">detail</a>
<a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">edit</a>
<a class="btn btn-info" href="{{ route('products.each_print',$product->id) }}">Print</a>
<form action="{{ route('products.destroy',$product->id) }}" method="POST">
#csrf
#method('DELETE')
<i class="fas fa-trash-alt"></i>
<button type="submit" class="btn btn-danger">×</button>
</form>
</td>
and in the each_print function pass the product instance to the view like:
public function each_print(Product $product)
{
$pdf = \PDF::loadView('products.each_print', ['product' => $product]);
return $pdf->stream('each_print.pdf');
}

why my database does not update ? (LARAVEL)

my database does not update while when i do
dd(request()->has('validated'));
this is what my web.php looks like:
Route::patch('prof/theme/{id}/validated', 'Theme\ThemeController#valide')->name('prof.theme.validated');
this is what my ThemeController.php looks like:
public function valide(Theme $theme) {
$theme->update([
'validated' => request()->has('validated')
]);
return back();
}
this is what my show.blade.php looks like:
<form action="{{route('prof.theme.validated', $theme)}}" method="POST">
#csrf
#method('PATCH')
#if ($theme->validated)
<button type="submit" class="btn btn-danger text-center" style="width: 350px">
INVALIDER LE THEME
</button>
#else
<button type="submit" class="btn btn-success text-center" name="validated" id="validated" style="width: 350px">
VALIDER LE THEME
</button>
#endif
</form>
this code does not show me any error but it does not produce the expected action. my variable "validated" is a boolean
thank you Harshith VA!!!!! i was able to solve the problem thanks to your idea. I kept your view and used your controller idea this way
public function valide(Request $request, Theme $theme) {
$theme->validated = $request->has('validated');
$theme->save();
return back();
}
thanks you for your help
From first look, I would believe this is due to the fact that you have added name="validated" to the <button> and not as an <input>.
<form action="{{route('prof.theme.validated', $theme)}}" method="POST">
#csrf
#method('PATCH')
#if ($theme->validated)
<button type="submit" class="btn btn-danger text-center" style="width: 350px">
INVALIDER LE THEME
</button>
#else
<input type="hidden" name="validated" value="true">
<button type="submit" class="btn btn-success text-center" style="width: 350px">
VALIDER LE THEME
</button>
#endif
</form>
Here, we add a hidden input field with the validated input. Doing this should pass that the validated key/value to the controller.
Another point. dd() is used to die and dump data, and would stop execution of whatever script is running. Therefore it won't save to the db once you do this.
The above assumes you didn't mean it doesn't save to the database when you dd().
Update
Also note that ->has() checks if the input exists. You should use request('validated') to get its value.

Resources