I run the laravel code for fetching the data from database but i got the error.
This is controller's code name is UserProfileController.blade.php
public function FetchUserQus()
{
$data = DB::table('userquestion')->where('userEmail', '=', '{{
Auth::user()->email }}');
return view('designpages/userqus', ['data' => $data]);
}
Route::get('designpages/userqus',
'UserProfileController#FetchUserQus')->name('designpages/userqus');
This is view page code saved with name designpages/userqus.blade.php
#foreach($data as $datas)
<p><b>Question: {!! $datas->quizUser !!}</b></p>
<p><b>Answer: </b>{!! $datas->ansAdmin !!}</p>
#endforeach
You cannot use blade syntax in the controller: So change this:
$data = DB::table('userquestion')->where('userEmail', '=', '{{ Auth::user()->email }}');
to this:
$data = DB::table('userquestion')->where('userEmail', '=', auth()->user()->email)->get();
And also I use get() to return a collection, without it it returns a Illuminate\Database\Query\Builder instance.
Related
I am getting this issue, and I don't understand what I am doing wrong as I did this same exact approach for another component on this site, and works perfectly....
ViewMessages.php
public $messages;
public function mount($messages)
{
$this->messages = $messages;
}
view-messages.blade.php
<div class="flex flex-col">
#foreach ($messages as $message)
{{$message->content}}
#endforeach
</div>
Everything works and it outputs all the messages correctly.
When I try and pass in a livewire component into the for each, it gives that error.
#foreach ($messages as $message)
#livewire('chat.show-message', ['message'=>$message], key('show-message-'.$message->id))
#endforeach
// ShowMessage.php
public $user;
public $message;
public $user_id;
public $content;
public function mount($message)
{
$this->message = $message;
}
Honestly am lost on what I am doing wrong, as I copied the exact same code and changed the variables that I used before. It works right now on the site when I do nested components.
<div class="flex flex-col space-y-4 py-4 overflow-y-auto">
#foreach ($chats as $chat)
#livewire('kanbans.show-sidebar-chat-message', ['chat'=>$chat], key('chat-'.$chat->id))
#endforeach
</div>
I redid this component already twice, and can't find any syntax issues or spelling errors. =/
The issue was with the DB Facade query
$messages = DB::select('SELECT * FROM chat_messages
WHERE (receiver_id = :auth_id and user_id = :user) OR (receiver_id = :user2 AND user_id = :auth_id2)',
['auth_id' => $auth_id, 'user' => $user->id, 'auth_id2' => $auth_id, 'user2' => $user->id]);
Although not sure yet how to pass in the $user->id but when I set the user id to #2, the livewire components work as intended.
public function view($user)
{
$user=User::where('id',$user)->firstOrFail();
$messages = ChatMessage::where(function ($query){
$query->where('user_id', '=', auth()->id() )
->where('receiver_id', '=', 2 );
})->orWhere(function ($query){
$query->where('user_id', '=', 2)
->where('receiver_id', '=', auth()->id() );
})->get();
return view('backend.chat.view-contact-chat',compact('user','messages'));
}
(2/2) ErrorException
Undefined variable: data(View: E:\xampp\htdocs\snipe-it\resources\views\hardware\bulk-checkout.blade.php)
My Controller
public function bulkAssetImport($assets_ids)
{
$data='testersdfsdf';
return Redirect('hardware/bulk-checkout',compact('data'));
}
My blade.php File
#foreach
{{$data}}
#endforeach
I tried different ways but no use, Please help me
If you just want to display data in html. Yo can do something like this.
<p>{{ $data }}</p>
If you want to loop data in your html
In your controller
public function bulkAssetImport($assets_id)
{
$data= array(
array('name'=> 'John doe', 'age' => 22),
array('name'=> 'Doe not', 'age' => 22),
);
return view('hardware/bulk-checkout',compact('data'));
}
In your html:
#foreach($data as $d)
My name is: {{$d->name}} and my age is: {{ $d->age }}
#endforeach
I am trying to get posts from all users, plus tasks from only the current user. All passed into a single page with a single function and route. It returns an error page instead.
Controller
public function getDashboard()
{
$user = Auth::user();
$userId = $user->id;
$posts = Post::orderBy('created_at', 'desc')->get();
$tasks = Task::where('employee_id', $userId )->get();
return view('dashboard', compact('posts', 'tasks'));
}
Route
Route::get('/dashboard', [
'uses' => 'PostController#getDashboard',
'as' => 'dashboard',
])->middleware('auth');
Blade/View
<div>
#foreach($tasks as $task)
<p data-taskId="{{ $task->id }}">{{ $task->task_body }}</p>
#endforeach
</div>
Looks like possibly a syntax issue, as compact should work fine. Try this in your controller:
return view('dashboard', compact('posts', 'tasks'));
Then in your view, make sure to use the variables and not the class name, and as Karl Hill said, it's used within (), not {{}}:
#foreach($posts as $post)
{{$post->nameOrWhatever}}
#endforeach
#foreach($tasks as $task)
{{$task->nameOrWhatever}}
#endforeach
What I'm trying to achieve is very simple, but I'm just making this a lot harder than it needs to be. So am seeking help for what is apparently, my lack of Laravel experience.
All I want to do is have a form that can update database entries via a text input. This has to be dynamic as it's being used for a few databases and I don't want to have multiple files for them.
Sorry in advance for the probable messy/crap code...
Here's the routes I have:
Route::get('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#greeting');
Route::post('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#updateGreeting')->name('greeting.update');
The PlayerProfileController
// Display greeting message
public function greeting($server, $playerID) {
$greetInfo = DB::connection($server)
->table('clients')
->where('id', $playerID)
->first();
return view('servers.greeting')
->with('greetInfo', $greetInfo);
}
// Update greeting message
public function updateGreeting(Request $request, $server, $playerID) {
$gUpdate = DB::connection($server)
->table('clients')
->where('id', $playerID)
->update(['greeting' => $request->input('greet')]);
return back()->with('success', 'Greeting updated successfully!');
}
And finally the form
{{ Form::open(['action' => ['PlayerProfileController#updateGreeting', $greetInfo->greeting], 'method' => 'POST']) }}
{{ Form::bsText('greet', '', ['placeholder' => 'Update greeting or leave blank to remove current message']) }}
{{ Form::hidden('_method', 'PUT') }}
<br>
{{ Form::bsSubmit('Update',['class' => 'btn btn-outline-secondary']) }}
{!! Form::close() !!}
Any and all help is appreciated. Thank you in advace.
The ErrorException you're seeing is caused by the missing argument for the route you're trying to call. Your route is expecting server and playerId, wheres you're only sending it $greetInfo->greeting.
As you're using named route, my suggestion would be to use route() helper like so:
Route::get('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#greeting');
Route::post('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#updateGreeting')
->name('greeting.update');
class PlayerProfileController extends Controller {
public function greeting($server, $playerID) {
$greetInfo = DB::connection($server)
->table('clients')
->where('id', $playerID)
->first();
return view('servers.greeting')
->with('greetInfo', $greetInfo);
}
// Update greeting message
public function updateGreeting(Request $request, $server, $playerID) {
$gUpdate = DB::connection($server)
->table('clients')
->where('id', $playerID)
->update(['greeting' => $request->input('greet')]);
return back()->with('success', 'Greeting updated successfully!');
}
}
{{ Form::open(['action' => route('greeting.update', [$server, $playerId]), 'method' => 'post']) }}
{{ Form::bsText('greet', '', ['placeholder' => 'Update greeting or leave blank to remove current message']) }}
{{ Form::bsSubmit('Update',['class' => 'btn btn-outline-secondary']) }}
{!! Form::close() !!}
Please replace $server and $playerId variables with the ones representing the arguments you want to send with the request.
You can see I've also removed {{ Form::hidden('_method', 'PUT') }}, as this was trying to overwrite the post method on the Form::open method.
I want to make a PHP method using laravel. I want to do the comparison of criteria and criteria. Here is the controller code :
public function create()
{
$kriteria1 = Model\Kriteria::pluck('nama_kriteria', 'id');
$kriteria2 = Model\Kriteria::pluck('nama_kriteria', 'id');
return view('kriteria_kriterias.create')->with('kriteria1', $kriteria1)->with('kriteria2', $kriteria2)->with('data', $data);
}
and this is the blade code :
It will make the form appear as total of criteria#
The problem is, I can't save it all to database. How do I get it to do this?
Updated method in the controller to the following:
public function create()
{
$kriteria1 = Model\Kriteria::pluck('nama_kriteria', 'id');
$kriteria2 = Model\Kriteria::pluck('nama_kriteria', 'id');
$data = [
'kriteria1' => $kriteria1,
'kriteria2' => $kriteria2
];
return view('kriteria_kriterias.create')->with($data);
}
How to output in the blade file:
{{ $kriteria1 }}
{{ $kriteria2 }}
Or you update the controller to pass the complete results:
public function create($id1, $id2)
{
$kriteria1 = Model\Kriteria::find($id1);
$kriteria2 = Model\Kriteria::find($id2);
$data = [
'kriteria1' => $kriteria1,
'kriteria2' => $kriteria2
];
return view('kriteria_kriterias.create')->with($data);
}
And the in the blade you can accss the data in various ways, one way is a foreach loop using blade in the blade template:
#foreach($kriteria1 as $k1)
{{ $k1 }}
#endforeach
#foreach($kriteria2 as $k2)
{{ $k2 }}
#endforeach'
To accept multiple values dynamicaly in the controller you can try something like this:
public function create($ids)
{
$results = collect([]);
foreach($ids as $id) {
$kriteria = Model\Kriteria::findOrFail($id);
if($kriteria) {
$results->put('kriteria' . $id, $kriteria);
}
}
return view('kriteria_kriterias.create')->with($results);
}
Then use the same looping method mentioned above to display them in the blade or a for loop that gets the count and displays accordingly.
maybe you forgot to add the opening tag ;)
{!! Form::open(array('url' => 'foo/bar')) !!}
//put your code in here (line 1-34)
{!! Form::close() !!}