Retrieve and edit data on page using Laravel - mysql

I cant display my data, laravel's 404 page popping.
Here is my foreach codes.
#foreach($este as $row)
{{$row['価格']}}
{{$row['間取り']}}
{{$row['販売戸数']}}
{{$row['総戸数']}}
{{$row['専有面積']}}
{{$row['専有面積']}}
{{$row['その他面積']}}
{{$row['所在階/構造 階建']}}
{{$row['完成時期']}}
{{$row['住所']}}
#endforeach
What am I doing wrong here.
I want to edit table with css but data doesn't display.
My controller, and route is here:
public function sumos($id)
{
$este = estates::all();
return view('pages.sumo', ['este' => $este]);
}
Route::get("sumo/{id}", "PagesController#sumos");

Change your controller and route to the following
public function sumos()
{
$data['este'] = estates::all();
return view('pages.sumo', $data);
}
Route::get("sumo", "PagesController#sumos");
then your view to
#foreach($este as $row)
{{$row->価格}}
{{$row->間取り}}
{{$row->販売戸数}}
{{$row->総戸数}}
{{$row->専有面積}}
{{$row->専有面積}}
{{$row->その他面積}}
{{$row->所在階/構造 階建}}
{{$row->完成時期}}
{{$row->住所}}
#endforeach

Don't return the value from the controller . Instead send the values to the view as parameter and then use it in your view
Like
public function sumos()
{
$este = estates::get();
return view('viewName', ['este' => $este]);
}
And in your view
#foreach($este as $row)
write markup for your items here.
#endforeach

1.Actually get is for retrieve all the data from table ,
2.After getting related data pass it on view
public function sumos()
{
$este = estates::get();
return view("editPage" , compact('este'));
}
After View data in table using
#foreach($este as $est) //data here like html css #endforeach
add route to update data and pass it into controller
Then find related row using `
public function find($id){
$estates = estates::find($id);
//after changes
$estates->save();
return redirect()->back();
}
`

firs you have to create a view file
How to use views in Laravel?
then just use this construction:
public function sumos()
{
$este = estates::all();
return view('name_of_your_view_file', compact('este');
}

change your route to
Route::get("sumo/{id}", "PagesController#sumos");

Related

i have inserted data in databse but not knowing how to show in laravel

I am getting this error
Missing required parameters for [Route: service.show] [URI: service/{service}].
public function show($id)
{
$service = Service::find($id);
return view('service.show')->withPost($service);
//
}
controller
i have one blade file to show.
Route file .
Route::resource('service', 'ServiceController');
Route::get('service/{id}', 'ServiceController#show');
Is this route correct
It is easier when you read Laravel Documentation.
This are some teachers of Laravel Coders Tape and Brad Traversy. You can learn from them from scratch
You can do it this way.
CONTROLLER
public function show($id)
{
$service = Service::find($id);
return view('service.show', compact('service'));
//
}
ROUTE
Route::resource('/service', 'ServicesController');
BLADE / VIEW
<div>
{{$service->id}}
</div>
public function show($id)
{
$service = Service::find($id);
return view('service.show')->with('service', $service);
}
In your blade,
<div>
{{ $service}}
</div>
Just use 2 curly braces {{ }} and put it inside the variable you use. In this example i used service
You need to call the show route with service id in parameter.
Route::get('service/{id}', 'ServiceController#show')->name('service.show');
<a href="{{ route('service.show', $id)">Show<a/>
If you wanna redirect to that route:
return \Redirect::route('service.show', $id);
Route
I would give the route a unique name, so you can easily generate links.
Route::name('service_info')->get('/service/{id}', 'ServiceController#show');
Controller
I think you need to use $request.
I added a check for 404 issues, but you could also check that in a middleware.
public function show(\Illuminate\Http\Request $request)
{
$service = Service::find($request->id);
if ($service === null) {
abort(404, "service not found");
}
return view('show', [
'service' => $service
]);
}
Blade
And in the blade you can use the service with {{}}
<div>
{{$service}}
</div>
Or if 'Service' has attributes like 'name' or 'id' and you want to use them:
<div>
{{$service->id}}: {{$service->name}}
{{$service->id.': '.$service->name}}
</div>
Both are possible.

How to do Query & show the result in Laravel

I want to dislpay all the data from table: room that has id = 1
but no result came out
here is my code:
public function show($id)
{
$room_list = Room::where('house_id','=','1');
return view('book.show', compact('room_list'));
}
in view
<?php foreach($room_list as $room): ?>
Room Number: {{room->house_id}}
<?php endforeach ?>
but no result appear, even the Room Number label is not appear meaning the query result is empty.
I already check the table room, it has data wih house_id = 1
Thank you for your help!
You should try this:
public function show($id)
{
$room_list = Room::where('house_id','=',$id)->get();
return view('book.show', compact('room_list'));
}
View
#foreach($room_list as $room)
Room Number: {{$room->house_id}}
#endforeach
Try this:
public function show($id)
{
$room_list = Room::where('house_id','=','1')->get();
// get() will return all the matching records that fulfill the condition criteria
return view('book.show', compact('room_list'));
// here compact('room_list') will pass the variable named $room_list to the view
}
View: You can use $room_list variable on view like:
#foreach($room_list as $room)
Room Number: {{$room->house_id}}
#endforeach

cakePHP3 pass variable from controller to view

I have a problem when passing a variable from Controller to view in CakePHP3.
File FincasController.php: I have created a public Method to make a report and be able to pass to a Template I have also created.
public function worklist()
{
$worklist = TableRegistry::get('Fincas');
$query = $worklist
->find()
->select(['id', 'prov', 'municipio', 'paraje', 'poligono', 'parcela', 'f_ult_poda' , 'f_ult_recog' ]);
foreach ($query as $worklist) {
if ( $worklist->f_ult_poda > $worklist->f_ult_recog )
debug($worklist); //it works fine
}
$this->set('finca', $this->$query); //I have tried also $this->$worklist
}
File Template\Fincas\worklist.ctp :
<?php debug($finca);?>
Thanks a lot
$query it justa a variable, it's not an attribute of your FincasControllerõ. Why are you using$this`?
simply
$this->set('finca', $query);

How to view data using junction table Yii2

I've got three tables
checkoutcounter {id, name}
user {id, username}
checkoutcounter_users { id, checkoutcounter_id, user_id}
I use gii and then add
in checkoutcounter model (I add and joinWith and find()->with but it still doesn't work):
public function getUser_display()
{
return $this->hasMany(User_display::className(), ['id' => 'user_id'])
->viaTable(CheckoutcounterUsers::tableName(), ['checkoutcounter_id' => 'id']
);
}
In checkoutcounter model search:
public function search($params)
{
$query = FinanceSettingsCheckoutcounter::find()->with('user_display');
$query->joinWith('user_display');
}
what should I add in checkoutcounter view to get usernames or user id's? Why when I add in gridview 'attribute'=>'user_display.id' it doesn't display any data?
echo yii\helpers\Json::encode($dataProvidercheckoutcounter);
shows
{"query":{"sql":null,"on":null,"joinWith":[[["user_display"],true,"LEFT JOIN"]],"select":null,"selectOption":null,"distinct":null,"from":null,"groupBy":null,"join":null,"having":null,"union":null,"params":[],"where":null,"limit":null,"offset":null,"orderBy":null,"indexBy":null,"modelClass":"app\\models\\FinanceSettingsCheckoutcounter","with":["user_display"],"asArray":null,"multiple":null,"primaryModel":null,"link":null,"via":null,"inverseOf":null},"key":null,"db":null,"id":null}
Im not sure how you're using your search() function, but you're not using $params.. And its not returning the results..
I belive the query should be like this:
public function search($params)
{
$result = FinanceSettingsCheckoutcounter::find()->with('user_display')->all();
echo yii\helpers\Json::encode($result);
}
if you are using this as part of a Search Model, returning a dataProvider, check out this link
http://www.ramirezcobos.com/2014/04/16/displaying-sorting-and-filtering-model-relations-on-a-gridview-yii2/

Query is returning empty arrays in CakePHP (model and controller are set up)

I'm using Cake's find('all') method in the controller to query a table I have, and the requestAction method to use the controller function (and so to output the data) within the view. The problem is that the find method is returning empty arrays(ouput is 'ArrayArrayArray'). The table is named 'users' (as per Cake conventions) and the code for all three files are below:
Code for the view (test.ctp)
<?php
$users = $this->requestAction('/users/read');
foreach($users as $user) {
echo $user;
}
?>
Code for the controller (users_controller.php)
<?php
class UsersController extends AppController {
var $name = 'Users';
function read(){
$users = $this->User->find('all');
if(isset($this->params['requested'])) {
return $users;
}
}
}
?>
Code for the model (user.php)
<?php
class User extends AppModel {
var $name = 'User';
}
?>
I've dwindled it down and discovered that $users is being set to NULL by the $this->User->find('all') statement because that statement is actually not returning any values. I know Cake is finding the table because of a typo I had earlier with the table name ('usres' instead of 'users'). I can't figure out what I'm missing or what I'm doing wrong. Would love some help. :)
The ouput is 'ArrayArrayArray' doen't means itz empty
try
foreach($users as $user) {
debug($user);
}
For me this code is meaningless.
if you want to use requestAction() use it in the proper way. It's used to call (and print) the parts of the application in the view.
Here is how to achieve the working and correct code:
Your Users class:
class Users extends AppController {
function read(){
$this->set('users', $this->User->find('all'));
$this->layout = "ajax";
$this->render();
}
}
Your read.ctp:
<ul><?php
if(isset($users)){
foreach($users as $user) {
echo '<li>'.$user.'</li>';
}
}
?></ul>
Now in your test.ctp you need to call:
<?php echo $this->requestAction(
array(
'controller'=>'users',
'action'=>'read'
)
);?>
And you will see the result in your test action as unsorted list.