passing session variable in laravel 5.2 - php

I have the modal where I am processing the data in the modal and then I need to store 1 value and pass it to the next page but then for some reason it is not working.
My modal code :- I have the variable $doctorName already set
session(['doctorName' => $doctorName]);
My view file
<div class="col-sm-12 row2">
<table width="100%">
<tr>
<td>
<p>Second Opinion from
<br /><span id="dr_Name"></span></p>
</td>
{{ session('doctorName') }}
<td align="right">
<p>INR 3000/-</p>
</td>
</tr>
</table>
</div>

Try {!! session('doctorName') !!} instead of {{ session('doctorName') }}

Do you open new view after modal? Maybe you need:
return view('someview')->with('doctorName');

Related

Can't delete from SQLite table in Laravel

I'm Doing a "take home assignment" for job interview. While I have some experience in web development, its not my forte. I am trying to delete a row in a SQLite table using a HTML DELETE button. I am using Laravel-php framework.
I've tried different solutions on google and stack Overflow, but none seem to solve the problem. I modeled my approach after this Laracasts video
Link to my code
The blade seems to be passing the correct info ($id from $contact->id) & the controller seems to be receiving. But the given contact associated with the id isn't being deleted.
FROM BLADE:
<div class="col-md-6">
<table class="table table-striped table-hover">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
#foreach($contacts as $contact)
<tr>
<td> {{$contact->f_name}} </td>
<td> {{$contact->l_name}} </td>
<td> {{$contact->address}} </td>
<td>
<form method="POST" action="/delete/{{ $contact->id }}">
#method('DELETE')
#csrf
<div class="field">
<div class="control">
<button type="submit" class="button">Delete Contact</button>
</div>
</div>
</form>
</td>
</tr>
#endforeach
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
FROM CONTROLLER:
public function delete($id) {
Contact::find($id)->delete();
return view('index');
}
FROM ROUTE:
Route::delete('/delete', [
'uses'=>'ContactController#delete',
'as'=>'contacts.delete'
]);
you're not getting the id on your delete method, you need to include it on the url like
Route::delete('/delete/{id}', [
'uses'=>'ContactController#delete',
'as'=>'contacts.delete'
]);
in this case, you don't need to change the delete method.
you can also retrieve the id from the request object without changing the delete route, int this case you need to include the id as a hidden input on your view and your delete method will look like this
public function delete(Request $request) {
Contact::find($request->id)->delete();
return view('index');
}

Showing only first and single record from database in laravel 5.5

I am trying to get all record which are available in my database table currently there are 3 records available with id 1,2 and 3 and i am only getting record related to id 1.
My Controller method:
public function EmailList()
{
$listing = Buyer::all();
return view('email-list',compact('listing'));
}
and my route:
$router->get('/List-of-Emails', 'AjaxController#EmailList');
and in my view i am passing foreach loop
#foreach($listing as $list)
<tr>
<input type="hidden" id="rwid" value="{{$list->id}}"/>
<td>
<input id="{{$list->id}}" class="checkbox-custom" name="{{$list->id}}" type="checkbox"
value="{{$list->id}}">
<label for="{{$list->id}}" class="checkbox-custom-label"></label>
</td>
<td>
<p data-placement="top" data-toggle="tooltip" title="" data-original-title="Tolltip">
{{$list->name}}
</p>
</td>
<td>
<a href="" class="pglst-lnk">
{{$list->email}}
</a>
</td>
<td>
{{$list->created_at}}
</td>
<td>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
#endforeach
i am getting only first and only single record however i want to get all records from table where i am doing wrong.
Any help would be highly appreciated!
I think this is a DOM parsing error.
Can you put the #endforeach just below the closing </tr> tag?
#foreach($listing as $list)
<tr>
<!-- data -->
</tr>
#endforeach
EDIT: Explanation
For those interested: the browser is incapable of parsing the HTML as given by OP. This is because after one record, he closes the table and a couple of divs. Then the foreach-loop starts again, in which he suddenly introduces new <tr>s and is closing <table>s and <div>s that don't exist. It will probably put an error message in the console, but not everybody looks there as it's not directly obvious that the outputted HTML is wrong.
Use Some query like
$user = DB::table('users')->where('name', 'John')->orderby('id','desc')->limit('the record you want')->first();

Dynamic buttons href tests with phpunit

I'm initiating my tests using phpunit so, I have doubts about how to test things dynamically. I created a table dynamically as the image bellow
Here are my view:
<div class="panel-body">
#if(Session::has('success'))
<div class="alert alert-success">{{Session::get('success')}}</div>
#endif
<table class="table">
<th>Unity Name</th>
<th>Phone Number</th>
<th>Actions</th>
<tbody>
#foreach($companies as $company)
<tr>
<td>{{$company->name}}</td>
<td>{{$company->owner_number}}</td>
<td>
{{Form::open(['method' => 'DELETE', 'url'=>'/admin/company/'.$company->id, 'style' => 'display:inline'])}}
<button type="submit" class="btn btn-default fa fa-trash-o"></button>
{{Form::close()}}
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
So, how can I test the href tag if I haven't previously the tag id?
I did some tests like these one in Symfony projects.
In dev environment, I insert some data in a dev database. After this step, I launch functionnal tests. In these tests, I analyze tag's content. Here is an example from your data :
$client = static::createClient();
$crawler = $client->request('GET', '/yourUrl');
$node = $crawler->filter('#show-company-1');
self::assertNotEmpty($node, "Node #show-company-1 does not exists");
self::assertEmpty( $node->html(), "#show-company-1 content is not empty");
self::assertContains('/admin/company/1', $node->attr('href'), "a#show-company-1.href has not a good value");
Before request, you can add some logic to determine the id of your company.

Laravel with TwigBridge method_field not working

I'm following the basic quickstart with laravel 5. I'm deviating only in that I'm using TwigBridge to allow me to use twig templates instead of blade. I'm getting in error when I tried to load my view.
Unknown "method_field" function in "/home/vagrant/laravel-test/resources/views/tasks.twig" at line 63.
method_field should be an available default helper function in laravel. I'm not really sure why twigbridge isn't finding it. Is there something I'm missing I need to make this function available to TwigBridge?
Edit
Here's my code:
{% if tasks|length > 0 %}
<div class="panel panel-default">
<div class="panel-heading">
Current Tasks
</div>
<div class="panel-body">
<table class="table table-striped task-table">
<!-- Table Headings -->
<thead>
<th>Task</th>
<th> </th>
</thead>
<!-- Table Body -->
<tbody>
{% for task in tasks %}
<tr>
<!-- Task Name -->
<td class="table-text">
<div>{{ task.name }}</div>
</td>
<td>
<form action="{{ url('task/'~task.id) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button>Delete Task</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
{% endif %}
Edit 2
I partially figured it out. I had to add method_field to the config/twigbridge.php file in the functions array:
'functions' => [
'elixir',
'head',
'last',
'method_field',
].
This got it to run the function, however the output is converted to html entities because it actually renders the input tag text instead of the tag itself. So I need to figure out away to make it not escape the output.
The answer is that you have to add the function to the config/twigbridge.php with an is_safe option. That can be done this way:
in /app/config/twigbridge.php
'functions' => [
// Other functions
'method_field' => [
'is_safe' => [true],
],
],

Server side pagination of laravel 5

I am having some trouble implementing server side pagination in Laravel 5.
Do you have any idea please tell
try with this one
//in controller
$users = \App\User::paginate(15)
return view('your desired view file name', compact('users'));
// in view
<div class="text-center text-muted" role="status" aria-live="polite">Showing {{$users->firstItem()}} to {{$users->lastItem()}} of {{$users->total()}} entries</div>
<div style="display:flex;justify-content:center;align-items:center;" >
<p></p>
{!! with(new Illuminate\Pagination\BootstrapThreePresenter($users))->render()!!}
</div>
In controller you do somrthing like following
$users = \App\User::paginate(15)
return view('template.file', compact('users'));
and in View file you put following
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td> {{ $user->name }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $users->appends(\Input::except('page'))->render() !!}
Last line renders pagination links.

Categories