laravel 4 how to show result at the same page and how to solve circle invoke - laravel

I am totally new to laravel, I am now want to use laravel 4.
Suppose I have a page A.php, and it contains a form & a submit button. After I submit the post request to B.php, and in B.php I query data from database.
My question is I want to show my result on B.php , that it is to say the same page of A.php request, how do I write in routes.php.
My code:
master.blade.php
<!DOCTYPE HTML>
<html>
<head>
<metacharset="UTF-8">
<title>course query</title>
</head>
<body>
<div class="container">
#yield('container')
</div>
<h1 class="ret">The result is:
#yield('ret')
<input id="result" type="text"/>
</h1>
</body>
</html>
A.php
#extends('course.master')
#section('container')
<h1>My Courses</h1>
{{Form::open(array('url' => 'csOp'))}}
{{Form::label('course', 'Your course')}}
{{Form::text('course')}}
{{Form::submit('Submit')}}
{{Form::close()}}
#endsection
routes.php
Route::get('course', function(){
return View::make('course.B');
});
Route::post('csOp', function(){
// do something
//$inputCourse = Input::get('course');
//$records = Courses::where('name', '=', $inputCourse)->get();
// how do I return
//return View::make('csOp', $records);
});
As you can see, in A.php I have a form and request to csOp
Form::open(array('url' => 'csOp')
csOp is B.php, and in B.php I query data from db, and now I got the results,
but how can I put result to the page (B.php) itself? That it is to say I want to put the result to
<input id="result" type="text"/>
You know in jquery is easy, how do I use it in laravel 4 ?
And if return to csOp, absolutlly will get an error, it is in a circle. So how can I solve it ?
Thanks very much.

If you want to populate a form based on model contents, i,e. populate form using database data.
So in laravel you can use Form Model Binding. To do so, use the Form::model method. so in your case
Route::post('csOp', function(){
// do something
$inputCourse = Input::get('course');
$records = Courses::where('name', '=', $inputCourse)->get();
// how do I return
return View::make('csOp')->with('records',$records);
});
And your csOp.blade.php
#extends('course.master')
#section('container')
<h1>My Courses</h1>
{{Form::model($records,array('url' => 'csOp'))}}
{{Form::label('course', 'Your course')}}
{{Form::text('course')}}
{{Form::close()}}
#endsection
Now, when you generate a form element, like a text input, the model's value matching the field's name will automatically be set as the field value. So, for example, for a text input named course, the Courses model's course attribute would be set as the value.

Related

How can i send array of data from laravel blade to controller

I have created a form and I used alpine js to add something to the array data, but I don't know how to pass it to the controller as a post method
It is super important that you read the official Laravel documentation, you are not a magician, so you must get this knowledge from somewhere.
You are not sharing a controller code, so I will assume you have this one:
Route::get('/', function () {
return view('greeting', ['name' => 'James']);
});
If you have this on your view, it will render that name:
<html>
<body>
<h1>Hello, {{ $name }}</h1>
</body>
</html>

Using data on dispatched event and trigger div

I have a component that dispatches a browser event with an object
// Livewire Component Method
public function passToDashboard($dataId)
{
$data = Model::find($dataId);
$this->dispatchBrowserEvent('show-data', ['data' => $data]);
}
Now on my dashboard blade view i've got
<div class="some-classes" x-data="{dataDisplay:false}">
<div x-show="dataDisplay">
{{-- This is where i want to use the object --}}
{{ $data->title }}
</div>
</div>
<script>
window.addEventListener('show-data', data => {
console.log(data.detail.title); // outputs title just fine
})
</script>
The question is, how to 'unhide' the dataDisplay and how to show it with the passed data? Thanks!
You can listen for these events directly on the element using #event-name.window="dataDisplay = true"
To get the event data, you use the $event variable and it should be under $event.detail.data.title
Use x-text to get the text onto the element. See my full example below.
So in your case:
<div class="some-classes" x-data="{dataDisplay:false, title: ''}" #event-data.window="dataDisplay = true; title = $event.detail.data.title">
<div x-show="dataDisplay">
<h3 x-text="title"></h3>
</div>
</div>
The documentation for this can be found here: https://laravel-livewire.com/docs/2.x/events#browser
Do notice I changed the event name, because it does apparently not work if you start the event name with "show". When I changed to "event-data" or anything else it started working again.

AJAX update div without creating partial

I have some page:
title = "page"
url = "/page"
layout = "default"
==
function onTest()
{
$this['result'] = 'Test string';
}
==
<!-- AJAX enabled form -->
<form data-request="onTest" data-request-update="mypartial: '#myDiv'"></form>
<!-- Result container -->
<div id="myDiv"></div>
After submitting the form I can get 'result' variable only in partial 'mypartial', where code is: Here is {{ result }}. And this code inserts into #myDiv.
How can I get 'result' variable without creating partial? Why should I create .htm file (partial) for each little AJAX query?
I guess you can easily update your div with id myDiv from server side.
You just need to push markup with proper id #myDiv it will update that Html Element with given ID automatically. [ we do not need to ask for partial or need partial ]
title = "page"
url = "/page"
layout = "default"
==
<?php
function onTest()
{
$result = 'Test string';
return ['#myDiv' => 'New Result: ' . $result];
}
?>
==
<!-- AJAX enabled form -->
<form data-request="onTest">
<button type="submit">Submit Fire Ajax</button>
</form>
<!-- Result container -->
<div id="myDiv">old result</div>
After successful Ajax request markup for myDiv will be
<div id="myDiv">New Result: Test string</div>
Good thing is you do not need to write javascript to update myDiv as part of October Ajax framework it will automatically search #myDiv and update its markup.
When you return an Array for an Ajax api of October Cms
return ['#myDiv' => 'Hello'];
At client side it will automatically search element with id myDiv and if it found it, It will update it with given markup on successful Ajax request, for this example its Hello
If any doubt please comment.

how can i use auth value into v-if condition in vue

i want to use the Auth variable in vue. but its not running
my code in vue is this. This code is possible?
<template lang="html">
<div class="chat-message" v-if="message.user_from == '<?php echo
Auth::user()->id; ?>'">
<div class="chatright">
{{message.user_from}} {{message.msg}}
</div>
</div>
<div class="chatleft" v-else>
{{message.user_from}} {{message.msg}}
</div>
</template>
<script> var user_is = <?php echo Auth::user()->id; ?></script>
then put the variable in condition finally that work
Try using Vuex it fits for your case or you can use the localStorage to persist the userId, otherwise you can't do this :
message.user_from == '<?php echo Auth::user()->id; ?>
Because you are trying to initialize user_from within the template, You should do it within the script tag
You can attach it to the root document of the HTML or you can pass the user as a prop to that Vue component.
Note: If you pass it in vue the HTML document object, you need to understand not to send any sensitive information of course.
In your you master page in the head section, add this:
<script>
window.App = {!! json_encode(
'user' => Auth::user(),
'signedIn'=>Auth::check(),
]) !!};
</script>
And in your component, you can reach it the normal way: App.user.something.
Or you can pass it as a prop if you send a $user object.
<your-compoenent :user="{{$user}}"></your-component>
Hope this hep you.

Laravel 4 content yielded before layout

I am using a fresh build today of Laravel 4.
I have a dashboardController
class DashboardController extends BaseController {
protected $layout = 'layouts.dashboard';
public function index()
{
$this->layout->content = View::make('dashboard.default');
}
}
I have a simple route
Route::get('/', 'DashboardController#index');
I have a blade layout in views/layouts/dashboard.blade.php
For the sake of saving everyone from all of the actual HTML ill use a mock up.
<html>
<head>
<title></title>
</head>
<body>
#yield('content')
</body>
</html>
I have a default blade file in views/dashboard/ that has the following (edited for simplicity)
#section('content')
<p>This is not rocket science</p>
#stop
For some reason the content gets generated before the layout.
I am using a different approach to set the layouts globally to routes using a custom filter. Put the following filter into the app/filters.php
Route::filter('theme', function($route, $request, $response, $layout='layouts.default')
{
// Redirects have no content and errors should handle their own layout.
if ($response->getStatusCode() > 300) return;
//get original view object
$view = $response->getOriginalContent();
//we will render the view nested to the layout
$content = View::make($layout)->nest('_content',$view->getName(), $view->getData())->render();
$response->setContent($content);
});
and now instead of setting layout property in the controller class, you can group the routes and apply the filter as shown below.
Route::group(array('after' => 'theme:layouts.dashboard'), function()
{
Route::get('/admin', 'DashboardController#getIndex');
Route::get('/admin/dashboard', function(){ return View::make('dashboard.default'); });
});
When creating the views, make sure to use the #section('sectionName') in all the sub views and use #yield('sectionName') in the layout views.
I find it easier to do my layout like this for example. I would create my master blade file like so
<html>
<body>
#yield('content');
</body>
</html
And in the blade files that I want to use the master at the top i would put
#extends('master')
then content like so
#section('content')
// content
#stop
Hope this helps.
When you use controller layouts, i.e. $this->layout->..., then you get access to data as variables, not sections. So to access content in your layout you should use...
<html>
<head>
<title></title>
</head>
<body>
<?php echo $content; ?>
</body>
</html>
And in your partial, you would not use #section or #stop...
<p>This is not rocket science</p>

Resources