Retrieving data from database using laravel - laravel-5

I would like someone to help me step by step laravel process in retrieving data from a database. This is what I have done. The problem is no data is being displayed. I am not so good in this and need some help. Thanks
ViewController.php
<?php
namespace App\Http\Controllers\AddressBook;
use DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
class ViewController extends Controller
{
/**
* Show a list of all of the application's users.
*
*
*/
Public function getContacts(){
$contacts= AddressBookModel::all();
$data = ['contacts' => $contacts];
return view('view')->with($data);
}
}
Route
Route::get('contacts',[
'uses'=>'AddressBook\ViewController#getContacts'
]);
The Route is working well and its connecting and display the content in view.blade.php
view.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h2 align="center">These are the Registered Contacts in the Database</h2>
</body>
</html>

Try this :
Controller :
public function getContacts()
{
// Try to name your model Contact instead of AddressBookModel
$contacts= AddressBookModel::all();// = Contact::all();
return view('view')->withContacts($contacts);
}
View:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<h2 align="center">These are the Registered Contacts in the Database</h2>
<table>
<!-- I assume that name and phone are contact model attributes-->
<th>Name</th>
<th>Phone</th>
#foreach ($contacts $as $contact)
<tr>
<td> {{$contact->name}} </td>
<td> {{$contact->phone}} </td>
</tr>
#endforeach
</table>
</body>

I added this function in the view.blade.php
#foreach($contacts as $display) {{$display}}
#endforeach

Related

Retrieve data from database using key in the view file

I am trying to retrieve data from my database and show it in my view but there I get an error.
Here is my controller
public function index()
{
$Page=Superior::all();
return view('Myview.Firstpage')->with('Task',$Page);
}
And this is where I assign in the view
<body>
<p>this is our first page </p>
{{ $Task }}
</body>
</html>
but this task is creating error and it says that the Task is an undefined variable my whole page looks like this
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>This is our first page </title>
</head>
<body>
<p>this is our first page </p>
{{ $Task }}
</body>
</html>
Superior is the name of my model from which I want to retrieve my data.
My routes files in web.php is
<?php
Route::get('First',function(){
return view('Myview.Firstpage');
});
i am learning laravel
In the index method of your controller
public function index()
{
return view('Myview.Firstpage')->with('tasks',Superior::all());
}
Keep in mind that the all() method returns a collection which you want to loop through in your view.
In your view, you should have:
#foreach($tasks as $task)
{{ $task->title }}
#endforeach
You need to also update your route to make use of the controller:
Route::get('/', 'TaskController#index');
You could visit https://laravel.com/docs/5.8/collections#method-all to learn more about collections.
Hi please try to pass you variable to view like this:
$Tasks = Superior::all();
return view('Myview.Firstpage', compact('Tasks'));
And then use a loop in your view like suggested in above comments.
#foreach($Tasks as $task)
{{ $task->title }}
#endforeach

Why my form open not working in laravel 5.1?

Hi all I have problame with my form I am writing html form but its error
and this is my route code
Route::get('course','CourseController#index');
and this is my CourseController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CourseController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('course.create');
}
and this is my view course/create.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="">
</head>
<body>
<div class="container">
{!! Form::open(array('route' => 'course.store')) !!}
<input type="text"><br>
<input type="password"><br>
{!! Form::close() !!}
</div>
</body>
</html>
and error:
Whoops, looks like something went wrong.
1/1 FatalErrorException in 818f720d4e075893d8198d2b0ff02e25 line 9: Class 'Form' not found
Help me please
With Laravel 5.* the Form & Html have been deprecated. Check out uprade guide (search Form & HTML Helpers).
1. Install laravelcollective/html
composer require laravelcollective/html
2. Add the Form and HTML facades and service provider.
Edit config/app.php and add this line to the 'providers' array:
'Collective\Html\HtmlServiceProvider',
Next, add these lines to the 'aliases' array:
'Form' => 'Collective\Html\FormFacade',
'Html' => 'Collective\Html\HtmlFacade',

Trouble with blade templating in laravel 5

I'm trying to understand laravel's basic blade template engine, but I can't seem to get past a basic example. My blade template is not loading .It only show the white screen but when I remove the hello.blade.php to hello.php it works .Any suggestion?
Routes.php
Route::get('/', 'PagesController#home');
PagesController.php
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class PagesController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function home()
{
return Views('hello');
}
}
hello.blade.php
<html>
<head>
<title>Hello World</title>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Starting to learn Laravel 5</div>
</div>
</div>
</body>
</html>
There is no Views helper. It's called view:
return view('hello');
Well, I had the same "white screen problem". And the blade docs in laravel is so confused. Then, try this:
Create a layout, lets say template.blade.php:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<div class="container">
<div class="content">
<div class="title">Starting to learn Laravel 5</div>
#yield('view_content')
</div>
</div>
</body>
Create a simple view that you want to wrap into the template, lets say hello.blade.php:
#extends('template')
#section('view_content')
This is my view content
#stop
Now, in your controller just call the view instead the layout:
public function home(){
return view('hello');
}
I'm not sure how it is working regardless of the extension as in your controller the syntax is incorrect. You return the view not Views... return view('hello'). You should technically see something like the following:
FatalErrorException in PagesController.php line 18:
Call to undefined function App\Http\Controllers\Views()
Even if app_debug is false you should see Whoops, looks like something went wrong.
I got this when I made a typo like so:
return views('abc');
instead of
return view('abc');
That extra s was killing everything.

How to pass value from view to controller

In controller i have function public ActionResult Index(int id,int test= 0,int to=10){} I try to change value test from view on click to get test+=10 but i dont know how. I tryed with action link and ajax but its not working. Any ideas?
My view code :
<html ng-app>
<head>
<meta name="viewport" content="width=device-width" />
<title>Countries</title>
<link href="~/Content/style.css" rel="stylesheet" />
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
<table>
<tr>
<td><h3>Matches</h3></td>
</tr>
<tr>
</tr>
#foreach (var i in Model.ListMatch)
{
<tr>
<td>#Html.ActionLink(i.Description, "", new { id = i.Id })</td>
</tr>
}
</table>
</div>
</body>
</html>
I return results to ng-view. Can i do something like
This question isn't explained very well, but from what I gather you want to be able to make a change to a value without reloading the page or leaving that view.
In order to do this you have to use AJAX. I know you said you tried AJAX but perhaps you got something wrong. Try this:
$('#yourbutton').click(function() {
$.post( "/YourController/YourFunction", function() {
alert( "success" );
});
});

Images not watched

I'm trying to make an image gallery. I have one controller with two functions and every function load one view. When I call index I can watch images but when I call the other one (tg) I can't watch them.
Images are in a folder named imagenes and its path is example.com/codeigniter.
The controller:
class Galeria extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function index()
{
$this->load->view('vgaleria');
}
public function tg()
{
$this->load->view('tgaleria');
}
}
The default controller is this one.
The view that I can't watch images:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Galería de imágenes</title>
<style type="text/css">
</style>
<script src="<?php echo base_url();?>javascript/funciones.js"
language="javascript" input="type/text">
</script>
</head>
<body>
<div id="imgs">
<img src="imagenes/imagen1.jpg" id="eimg">
</div>
<input type="button" id="bant" onclick="imginter('arrancar')"
value="Arrancar">
</button>
<input type="button" id="bsig" onclick="imgparar()" value="Parar"></button>
</body>
</html>
How can I watch images?
Thanks.
Try this:
<img src="<?php echo base_url(); ?>imagenes/imagen1.jpg" id="eimg">
Would also depend if you have removed your index.php from the url using .htaccess. Otherwise use site_url().

Resources