I do not manage to display the data from my database (I'm working with Laragon).
I already tried to change some names (variables etc.) here and there but I can't find where come from the problem.
I was able to display nothing first so I needed to change:
resources\views\artist\index.php -> resources\views\artists\index.blade.php
I probalby need to change more things in the code but I don't know where.
VIEW (views\artists\index.blade.php)
#extends('layouts.app')
#section('title', 'Liste des artistes')
#section('content')
<h1>Liste des {{ $resource }}</h1>
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
#foreach($artists as $artist)
<tr>
<td>{{ $artist->firstname }}</td>
<td>{{ $artist->lastname }}</td>
</tr>
#endforeach
</tbody>
</table>
#endsection
ROUTES (routes\web.php)
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('artists', 'ArtistController#index');
CONTROLLER (Controllers\ArtistController.php)
<?php
namespace App\Http\Controllers;
use App\Artist;
use Illuminate\Http\Request;
class ArtistController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
/*
Take the artist from the db
and send it to a specific template
*/
$artists = Artist::all();
return view('artists.index',
[
'artists' => $artists,
'resource' => 'artistes',
]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param \App\Artist $artist
* #return \Illuminate\Http\Response
*/
public function show(Artist $artist)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Artist $artist
* #return \Illuminate\Http\Response
*/
public function edit(Artist $artist)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Artist $artist
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Artist $artist)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Artist $artist
* #return \Illuminate\Http\Response
*/
public function destroy(Artist $artist)
{
//
}
}
DATABASE (database\ArtistsTableSeeder.php)
<?php
use Illuminate\Database\Seeder;
class ArtistsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$artists = [
['firstname'=>'Bob','lastname'=>'Sull'],
['firstname'=>'Marc','lastname'=>'Flynn'],
['firstname'=>'Fred','lastname'=>'Durand'],
];
foreach ($artists as $a) {
DB::table('artists')->insert([
'firstname' => $a['firstname'],
'lastname' => $a['lastname'],
]);
}
}
}
The only thing who get displayed on my browser when I go to 127.0.0.1:8000/artists is:
**Liste des Artistes**
Firstname Lastname
But nothing from the content of my Artistes table is displayed.
Thank you very much guys, the problems was that I needed to change the names of my columns...
FirstName LastName to Firstname Lastname
The data get displayed properly now.
Related
Hello When I run page it tells me that:
ErrorException
Undefined variable: tickets (View: D:\xampp\htdocs\new-helpdesk\resources\views\tickets\index.blade.php)
http://localhost:8000/tickets
Hide solutions
$tickets is undefined
when I dd it works.
index.blade.php
#extends('layouts.ticket')
#section('content')
#if ($tickets)
**
**#foreach ($tickets as $ticket)
<div class="form-group">
{!! Form::label('company', 'Company:', ['class' => 'form-label fs-6 d-inline-block']) !!}
{!! Form::select('company', [' '=> 'Choose Company'], null,['class'=>'form-control']);!!}
</div>
#endforeach
#endif
#endsection
TicketsController
<?php
namespace App\Http\Controllers;
use App\Models\Companies;
use App\Models\Tickets;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class TicketsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
// $tickets = Tickets::with('companies')->get();
return view('tickets.index');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
// return view('tickets.create');
$tickets = Companies::all();
dd($tickets->toArray());
// return view('tickets.index', compact('tickets'))->with('tickets', $tickets);
return view('tickets.index')->with(['tickets' => $tickets]);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$request->validate([
'ticket_title'=> 'required',
'description'=>'required'
]);
$query = DB::table('tickets')->insert([
'ticket_title'=>$request->input('ticket_title'),
'description'=>$request->input('description'),
]);
if($query){
return back()->with('success','Data have beeen inserted');
} else {
return back()->with('fail', 'Data not inserted');
}
}
/**
* Display the specified resource.
*
* #param \App\Models\Tickets $tickets
* #return \Illuminate\Http\Response
*/
public function show(Tickets $tickets)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Tickets $tickets
* #return \Illuminate\Http\Response
*/
public function edit(Tickets $tickets)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Tickets $tickets
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Tickets $tickets)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Tickets $tickets
* #return \Illuminate\Http\Response
*/
public function destroy(Tickets $tickets)
{
//
}
public function showcategory(){
// $companies = Companies::pluck('name', 'id');
// dd($companies);
}
}
web.php
Route::resource('/tickets', TicketsController::class);
Route::resource('/companies', CompaniesController::class);
Tickets Model
public function companys(){
return $this->belongsTo(Companies::class);
}
Companies Model
public function tickets(){
return $this->HasMany(Tickets::class);
}
How can I solve this problem?
I try to change it as layout
You have a little chaos in your return value, you either pass the collection in compact() or in with(). with() needs an array, so you will have to do one of the following:
return view('tickets.index', compact('tickets'));
//or
return view('tickets.index')->with(['tickets' => $tickets]);
=====EDIT
The /tickets route is calling your index() action. https://laravel.com/docs/9.x/controllers#actions-handled-by-resource-controller This action also returns the index.blade.php view, but you are not passing the tickets collection.
public function index()
{
$tickets = Tickets::with('companies')->get();
return view('tickets.index')->with(['tickets' => $tickets]);
}
As TimLewis pointed out, the #if($tickets) in your index.blade.php has no purpose and can be removed.
I've been trying to find the correct answer for my problem I know that the question has been asked before here and actually many other places, but no answers I found could suit my problem.
The vue component
FooterNewsletter.vue
<template>
<div class="lg:w-2/4 md:w-1/2 w-full px-8 border-l-2">
<p class="font-bold text-3xl">
Don't want to miss the latest cryptocurrency news?
</p>
<p class="py-3 text-lg">
Get the latest news and updates by subscribing to our free
newsletter 📰
</p>
<form #submit.prevent="subscribeToNewsletter">
<div class="flex flex-col">
<div class="flex">
<input
v-model="form.email"
type="text"
name="post_title"
class="
border-primary
focus:border-primary
focus:ring-offset-transparent
focus:ring-transparent
"
id="exampleFormControlInput1"
placeholder="Your E-mail"
/>
<input
type="submit"
value="Subscribe"
:disabled="form.processing"
class="px-5 py-2 bg-primary text-white cursor-pointer"
/>
</div>
</div>
</form>
</div>
</template>
<script>
export default {
props: [],
components: {
},
data() {
return {
form: this.$inertia.form({
email: "",
}),
};
},
methods: {
subscribeToNewsletter() {
this.form
.transform((data) => ({
...data,
// remember: this.form.remember ? "on" : "",
}))
.post(this.route("newsletter.store"), {
onSuccess: (data) => {
console.log("data", data);
},
onError: (data) => {
console.log("data", data);
},
});
},
},
};
</script>
The controller
NewsletterController.php
namespace App\Http\Controllers;
use App\Http\Requests\NewsletterStoreRequest;
use App\Models\Newsletter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
class NewsletterController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(NewsletterStoreRequest $request)
{
return response()->json([
'message' => 'suss',
]);
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
The request file
NewsletterStoreRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class NewsletterStoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'email' => ['required', 'max:150', 'email'],
];
}
}
The Route
web.php
Route::post('/newsletter', [
NewsletterController::class,
'store',
])->name('newsletter.store');
In the sample above i return a json object, which is not accepted and gives me this error
I have tried following the inertia documentation about responses here I think the correct answer is to be found there, I have not been able to solve it on my own
I have also looked into inertia Jetstream docs without any luck here
I don't want to return a new view, I actually want it to work as an ajax request I guess where a call-back is either giving me the error or success without reloading the page or anything like that.
I see that many people have this problem, does anyone know whats going wrong and what i have to return in the controller?
For me i had to change the controller to this:
namespace App\Http\Controllers;
use Inertia\Inertia;
use App\Models\Newsletter;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests\NewsletterStoreRequest;
class NewsletterController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(NewsletterStoreRequest $request)
{
Newsletter::create([
'email' => $request->email
]);
return back()->with('flash', [
'message' => 'success',
]);
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
What you should note here is this part:
return back()->with('flash', [
'message' => 'success',
]);
I found a sample where my problem was achieved with the help from a friend of mine in: vendor/laravel/jetstream/src/Http/Controllers/Inertia/ApiTokenController.php
Here they return with a flash message like my example above, the message key will be in props->components->flash->message
It won't work as I thought like ajax, because it uses Vue routes so the page will reload on submit.
I hope someone will find this useful.
Also if you get a console error because you use data in a vue component you have to make a check on the wrapper to check if the data is null like so:
<ul
v-if="assets != null"
class="
marketcap-rows
border-l border-r border-gray-200
flex flex-col
"
>
<li
v-for="asset in $props.assets.data"
v-bind:key="asset"
class="
bg-white
grid
gap-4
border-t border-gray-200
"
>
</li>
</ul>
This is the check I'm talking about: v-if="assets != null"
This is how to access jetstream initial default flash message:
session()->flash('flash', [
'bannerStyle'=> 'danger',
'banner' => 'this is the first message',
]);
return Inertia::render('Admin/Overview', [
'users'
]);
You need to add that to your Render...
doc: https://inertiajs.com/shared-data#flash-messages
class HandleInertiaRequests extends Middleware
{
public function share(Request $request)
{
return array_merge(parent::share($request), [
'flash' => [
'message' => fn () => $request->session()->get('message')
],
]);
}
}
I have used the laravel akaunting open source software and trying to modify it according to my needs but when I call the function from my route like
Route::resource('low-stocks','Reports\LowStock');
Or
Route::get('low-stocks','Reports\LowStock#index');
It does not work and when I call this route it redirects the page into dashboard
But when I write this
Route::get('low-stocks','Reports\LowStock#testing');
It works
I tried creating permissions in the akaunting prebuild users permissions but it is still doing the same
My whole route.php looks like this
Route::group(['middleware' => 'language'], function () {
Route::group(['middleware' => 'auth'], function () {
Route::group(['prefix' => 'reports'], function () {
Route::resource('income-summary', 'Reports\IncomeSummary');
Route::resource('expense-summary', 'Reports\ExpenseSummary');
Route::resource('income-expense-summary', 'Reports\IncomeExpenseSummary');
Route::resource('tax-summary', 'Reports\TaxSummary');
Route::resource('profit-loss', 'Reports\ProfitLoss');
Route::resource('best-seller', 'Reports\BestSeller');
Route::get('best-seller-monthly', 'Reports\BestSeller#index');
//It works
Route::get('testing', 'Reports\LowStock#testing');
// It doesnot works
Route::resource('low-stocks','Reports\LowStock');
});
});
});
It looks like it is banning to call index , create , edit , delete , store , update function without permission I could not understand it
This is my controller
<?php
namespace App\Http\Controllers\Reports;
use App\Http\Controllers\Controller;
use App\Models\Common\Item;
use App\Models\Setting\Group;
use App\Models\Setting\Category;
class LowStock extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
public function testing()
{
$items = Item::with('category')->where('quantity' , '=' ,0)->collect();
$categories = Category::enabled()->orderBy('name')->type('item')->pluck('name', 'id');
$genres = Group::enabled()->orderBy('name')->type('item')->pluck('name', 'id');
return view('reports.low_stocks.index', compact('items','categories','genres'));
}
}
It's not so much hard to maintain a custom module in akaunting. You should read the doc of akunting well.
First you need to create a module for this
php artisan module:make Blog
php artisan module:install blog 1 //(1 its your company id)
For Better understanding read this two https://akaunting.com/docs/developer-manual/modules
& https://nwidart.com/laravel-modules/v1/advanced-tools/artisan-commands
I define a variable in view. But problem is that it shows undefined. My task is, first i take all the input from user using form helper then i want to post it to the store function & do some calculation and then pass it to the view for showing purpose. But i tried it, unfortunately shows undefined.Undefined variable is "totalamount" Here i attached all the code:
controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\TestSetup;
use Illuminate\Support\Facades\Input;
class TestRequestController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$allTestNames=TestSetup::all()->pluck('test_name','id');
// $testNames=TestSetup::orderBy('test_name')
// ->get();
return view('testrequestentries.createTestRequestEntry')->withAlltestnames($allTestNames);
// return view('testrequestentries.createTestRequestEntry');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// if(Input::get('Add')) {
$total=$request->fee+$total;
return view('testrequestentries.createTestRequestEntry')->withTotalamount($total);
// }else if(Input::get('Save')){
//
// }
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
view
#extends ('main')
#section('title','|Test Request Entry')
#section('stylesheets')
{!! Html:: style('css/parsley.css')!!}
#endsection
#section('content')
{!! Form::open(['route' => 'testrequests.store','data-parsley-validate'=>'']) !!}
{{Form::label('patient_name','Name of the Patient:')}}
{{Form::text('patient_name',null,array('class' => 'form-control','required'=>'', 'data-parsley-required-message' => 'Patient Name is required'))}}
{{Form::label('date_of_birth','Date Of Birth:')}}
{{Form::date('date_of_birth',null,array('class' => 'form-control','data-parsley-required' =>'true')) }}
{{Form::label('mobile_no','Mobile No:')}}
{{Form::text('mobile_no',null,array('class' => 'form-control','type'=> 'number','maxlength'=>'11','minlength' => '11','required'=>'', 'data-parsley-required-message' => 'Mobile No is required'))}}
{{Form::label('test_id','Select Test:')}}
{{Form::select('test_id',$alltestnames,null,['class'=>'form-control','required'=>'','placeholder' => 'Pick a Test...'])}}
{{Form::label('fee','Fee:')}}
{{Form::text('fee',null,array('class' => 'form-control','required'=>'', 'data-parsley-required-message' => 'Fee is required'))}}
{{Form::submit('Add',array('class' => 'btn btn-lg btn-block btn-success','style' => 'margin-top:10px;'))}}
<table class="table table-bordered" style="margin-top: 50px;">
<thead>
<tr>
<th>SL</th>
<th>Test Name</th>
<th>Fee</th>
<th>Type Name</th>
</tr>
</thead>
#php ($i=1)
<tbody>
<tr>
<th scope="row">{{$i}}</th>
<td>{{$totalamount}}</td>
</tr>
</tbody>
#php ($i++)
</table>
{!! Form::close() !!}
#section('scripts')
{!! Html:: script('js/parsley.min.js') !!}
#endsection
#endsection
I bet the view is returned from create() here, so change this:
return view('testrequestentries.createTestRequestEntry')->withAlltestnames($allTestNames);
To:
return view('testrequestentries.createTestRequestEntry', [
'alltestnames' => $allTestNames,
'totalamount' => $total
]);
In the store() method, do not return the view. Redirect back() or to index() or show() methods instead.
first thing first: check the value of $total=$request->fee+$total;
ie: put "dd('$total', $total);" right after the line you set it, before the return.
second: make it simplier return view(...)->with('totalAmount', $total);
test the value of $totalAmount in the view (BLADE file)
ie: {{dump('totalAmount', $totalAmount)}}
Anyway the store() method does NOT store anything ... WHY THAT?
I am stuck with this error in Laravel:
Trying to get property of non-object (View: C:...\resources\views\admin\users\index.blade.php)
Is anything wrong with my code?
This My index
#extends('layouts.admin')
#section('content')
<h1>Users</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
<th>Photo</th>
</tr>
</thead>
<tbody>
#if($users)
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->role->name}}</td>
<td>{{$user->active == 1 ? 'Active' : 'Not Active'}}</td>
<td>{{$user->created_at->diffForHumans()}}</td>
<td>{{$user->updated_at->diffForHumans()}}</td>
<td>{{$user->photo_id}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
#stop
This my Controller
public function index()
{
//
$users = User::all();
return view('admin.users.index', compact('users'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
$roles = Role::pluck('name','id')->all();
return view('admin.users.create', compact('roles'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(UsersRequest $request)
{
//Buat bikin create dengan method ini
User::create($request->all());
return redirect('/admin/users');
// return $request->all();
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
return view('admin.users.show');
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
return view('admin.users.edit');
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
This my Request
class UsersRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
'name'=>'required',
'password'=>'required',
'email'=>'required',
'active'=>'required',
'role_id'=>'required',
'photo_id'=>'required|image|mimes:jpeg,png,jpg,gif,svg|max:7098',
];
}
}
This means (inside the controller)
$users = User::all();
returns empty object. There are no users in the db. The error probably comes from (admin/users/index.blade.php)
{{$user->id}}
and the other lines follow as well.
Before looping over the users, check if the data is empty. Correct this line
#if($users)
to look like this
#if($users->toArray())
It will return an empty array if there are no records for users and never enter in loop.
This will correct your error.
PS: Put an else statement for the #if statement with proper message so you can know it worked.
Please change this line
<td>{{$user->role->name}}</td>
to
<td>{{ $user->role ? $user->role->name : '-' }}</td>
Should work now... :)