I'm learning Laravel, and I've been trying to pass data between a controller and a view, but I'm still getting the same error on the page. Does anyone know how to fix this? Even with this small code, it doesn't seem to work. Do I need to make a configuration or something?
I've tried
->with('message', 'Hi Victoria')
view
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>App title</title>
<html>
<body>
<h1>Hello, {{ $message }}</h1>
</body>
</html>
</html>
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Hello;
class HelloWorld extends Controller
{
public function sayHello()
{
return view('hello', ['message' => 'Hi Victoria']);
}
}
web.php
Route::get('/', function(){
return view('hello');
});
The problem is that you have written a controller, but you are not using it. In your route ( web.php ), you have returned the view.
You should take a look at Laravel's documentation regarding how to make a route point to a controller method
So, you would need to change your route in web.php to
use App\Http\Controllers\HelloWorld;
Route::get('/', [HelloWorld::class, 'sayHello']);
In web.php, you should write the following code to use the HelloWorld controller:
use App\Http\Controllers\HelloWorld;
Route::get('/', [HelloWorld::class, 'sayHello']);
you can try this in web.php
Route::get('/', function(){
return view('hello',['message' => 'Hi Victoria']);
});
Related
I have a mailable class that sends an email to someone that makes a contract. Now I'm trying to style the mail but... I can't quite seem to call the variable that I pass in the mailable class return.
I've tried passing it to the view in the mailable class and I've tried calling it but in the mail it doesn't show up.
this is my mailable class:
public function build()
{
$data = array(
'comapny' => $this->data['company'],
'file' => $this->data['file'],
'subject' => $this->data['subject'],
'email' => $this->data['email']
);
foreach($data['email'] as $mail)
return $this->view('mails.contract')->with('data' , $data['company'])->to($mail)->subject($data['subject'])->attach($data['file'])->withSwiftMeassage(function ($message){
$swiftMessage = $message->getSwiftMessage();
$headers = $swiftMessage->getHeaders();
$headers->addTextHeader('From', 'example - contract <example-email#gmail.com>');
$headers->addTextHeader('Reply-To', 'example-email#gmail.com');
$headers->addTextHeader('X-Mailer:', 'PHP/' . phpversion());
});
}
}
the view i need to call the data to:
<!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>Document</title>
</head>
<body>
<p><?php echo $data ?></p>
</body>
</html>
any help is appreciated
You need to use with() with arrays.
Replace ->with('data' , $data['company']) with ->with(['data' => $data['company']])
If you are returning a view to render data for the mail template (in this case i suppose its mails.contract template), do you have a blade template that is located in resources/mails/ and named contract.blade.php?
https://laravel.com/docs/5.8/mail#configuring-the-view
https://laravel.com/docs/5.8/mail#view-data
You can pass a variable into the mailable class like.
Need to create a constructor and define a public function.
public $data;
public function __construct( $parameter )
{
$data = /*Your logic define here and assign to `$this->data`*/
$this->data = $data;
}
public function build()
{
return $this->view('mails.contract')->with(['data' => $this->data])
->to($mail)->subject($this->data['subject'])
->attach($this->data['file'])
->withSwiftMeassage(function ($message){
$swiftMessage = $message->getSwiftMessage();
$headers = $swiftMessage->getHeaders();
$headers->addTextHeader('From', 'example - contract <example-email#gmail.com>');
$headers->addTextHeader('Reply-To', 'example-email#gmail.com');
$headers->addTextHeader('X-Mailer:', 'PHP/' . phpversion());
});
}
any public property defined on your mailable class will automatically be made available to the view.
When I try to create a view for a product, the URL gets built correctly.
http://localhost:8000/product/my-slug
However, I get a 404 page not found and I have no idea why. It's like the controller is not getting called.
Initiation
<a href="{{ route('product.view', $product->slug) }}">
Route
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
Controller
public function view($slug)
{
$product = Product::find($slug);
return view('products.view', compact('product'));
}
View
<h1>{{ $product->name }}</h1>
EDIT
web.php
Route::get('/', 'ProductsController#index')->name('product.index');
Route::get('/products/create', 'ProductsController#create')->name('product.create');
Route::post('/products', 'ProductsController#store')->name('product.store');
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
/*Route::get('/users', 'UsersController');*/
Route::get('/contact', 'PagesController#contact');
Route::get('/about', 'PagesController#about');
Try changing
Route::get('/product/{$slug}', 'ProductsController#view')->name('product.view');
to
Route::get('/product/{slug}', 'ProductsController#view')->name('product.view');
Ref: Laravel Routing
i'm try to get news_id from database but when go to view say : error -> Trying to get property of non-object / Message: Undefined variable: data
this model - >
class Model1 extends CI_Model {
public function get_art()
{
$query = $this->db->get('entries');
return $query->result();
}
}
here controller Code - >
class Home extends CI_Controller
{
public function members()
{
$this->load->model('model1');
$data=$this->model1->get_art();
$this->load->view('members', $data);
}
}
and this Full View - >
<html>
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
</head>
<body>
<h1>
<? echo $data->body; ?>
</h1>
</body>
</html>
This is because of you invalid pass data to view. At controller replace line
$data=$this->model1->get_art();
with
$data["query"] = $this->model1->get_art();
Then at view you will have var $query with results of your database query.
You can use it like this:
<h1>
<? foreach($query as $row) {
echo $row->body;
}
?>
</h1>
I am trying to get data out of my table, and send that array of output over to my view and there echo out specific parts of it.
I get an exception error: Undefined index bassengId.
index.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Bassengweb</title>
</head>
<body>
<?php
if(isset($htt))
{
echo $htt['malingsId'];
}
?>
</body>
</html>
routes.php
Route::get('/', 'HomeController#showIndex');
Route::post('/data', 'HomeController#showInput');
homecontroller.php
public function showIndex()
{
return View::make('index');
}
public function showInput()
{
$htt = hvertredjetime::all();
return View::make('index')->with('htt', $htt);
}
If I try to just echo the $htt variable from index, I get:
[{"malingsId":1,"dato":"25.02.2014","tid":"12:44:00","frittKlor":"4.00","bundetKlor":"5.00","totalKlor":"9.00","ph":"7.00","autoPh":"8.00","autoKlor":"9.00","redox":"5.00","bassengId":1}]
I am a little stuck here, being new to this and not really seeing what I do wrong.
By using the ::all() method you are returning an array
public function showInput()
{
$htt = hvertredjetime::all();
return View::make('index')->with('htt', $htt);
}
In the View
#if(! is_null($htt))
#foreach($htt as $item)
<p>{{ $item->bassengId }}</p>
#endforeach
#endif
If you want to access data like this, you should do:
return View::make('index')->with(array('htt' => $htt));
Instead, return it like this:
return View::make('index')->with($htt);
And then, in view, access it directly:
{{ bassengId }}
You should access to the variable like this :
if(isset($htt))
{
echo $htt->bassengId;
}
or if you're using blade template :
#if(isset($htt))
{{ $htt->bassengId }}
#endif
What must be the problem with this code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><?php echo $title; ?></title>
<?php
echo link_tag('assets/css/jquery.mobile-1.3.2.min.css', 'stylesheet');
echo script_tag('assets/js/jquery-1.10.2.min.js');
echo script_tag('assets/js/jquery.mobile-1.3.2.min.js');
?>
</head>
<body>
<div data-role="page">
<header data-role="header">
Show
<h3><?php echo $title; ?></h3>
<div data-role="controlgroup" data-type="horizontal" class="ui-btn-right">
My Account
Logout
</div>
</header>
I'am using jquery mobile for my client-side script and PHP(codeigniter) for server-side script.
When I refresh the page after including the in anchor the page now doesn't display the page anymore.
Can anyone tell what's wrong with the code or I'am just missing something.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Event_management_c extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('html', 'url', 'form');
}
public function index() {
$data['title'] = 'Events';
$data['reply_title'] = 'Reply Message';
$this->load->view('fragments/header', $data);
$this->load->view('fragments/nav', $data);
$this->load->view('events/index', $data);
$this->load->view('fragments/footer', $data);
}
}
?>
Problem here
Logout
to
Logout
you can use template library for codeigniter like TEMPLATE LIBRARY
and reload the script and style files.
Based on your extra answers from the comments; the problem is most likely that the function site_url() isn't defined. The result of this is a fatal error, which isn't shown to you due to settings in php concerning error_report.
To solve this, run $this->load->helper('url') in relevant controller, or simply add url to the helper array in application/config/autoload.php. Since this function is so common, I recommend to autoload it.