How do I use a vue.js directive on a form object created using laravel collectives library.
ex: {{Form::open(['route' => 'route.name'])}}
How would I add v-mydirective to the <form> tag?
You can add extra attributes to the array
{{Form::open([
'route' => 'route.name',
'v-if' => 'foo'
])}}
Related
Im trying to create DELETE button in my Laravel CRUD app. and have an error:
(2/2) ErrorException array_key_exists(): The first argument should be either a string or an integer
My view:
{{!!Form::open(['action' => ['CompanyController#update', $company->id], 'method' => 'PUT'])!!}}
{{Form::input('Delete',['class'=>'btn btn-danger'])}}
{{!!Form::close()!!}}
I'm using Laravel Collective documentation and it says I can use:
Form::open(['action' => ['Controller#method', $user]])
But whats wrong with my code?
You must provide the key. Collective won't try to guess the key name, as we are used to on Laravel.
Form::open(['action' => ['Controller#method', $user->id]])
The piece of code above shows how to provide the key: $user->id
I am having an issue while binding from data with my model. When I load the edit page for editing a resource, I can see the input value in the input but it disappears asap since its binded to the vuejs.
Here is my vuejs data
data: {
form: new Form({
title: '',
language: [],
poster: ''
})
},
and my inputs are like this
{!! Form::model($movie, ['class' => 'form-horizontal', '#submit.prevent' => 'form.updateMovie', 'id' => 'update-movie-form', 'files' => true, '#keydown' => 'form.errors.clear($event.target.name)']) !!}
....
{!! Form::text('title', 'movie title', ['class' => 'form-control', 'id' => 'title', 'v-model' => 'form.title']) !!}
....
{!! Form::close() !!}
How I can get around this issue?
I've had exactly the same issue today.
The Vue.js version 1 allows us to provide initial values to the v-model via value attribute, but this functionality was deprecated on the version 2.0.
Migration guide says:
v-model no longer cares about the initial value of an inline value attribute. For predictability, it will instead always treat the Vue instance data as the source of truth.
Credit where credit due: https://www.npmjs.com/package/vue-data-scooper
The author of that plugin (and quote) identifies a change in Vue.js, and wrote the vue-data-scooper plugin as a work-around (to restore the former functionality).
I'm now using the plugin myself (following the instructions on the above link) and can confirm it "resolves" the issue you're seeing.
I don't wish to duplicate the existing instructions found in the plugin's documentation, but I didn't find they aligned precisely with the Laravel installation I was using (v5.4).
Specifically I installed the plugin...
npm install vue-data-scooper
...then patched my app.js (which is extremely minimal in my case)...
require('./bootstrap');
window.Vue = require('vue');
import VueDataScooper from "vue-data-scooper"
Vue.use(VueDataScooper);
...and dropped the data: {...} declaration from my new Vue(...) declarations (allowing the plugin to sort it out using initial data).
I add these packages in my laravel composer.json:
"laravelcollective/html": "~5.0"
and i add these
'Collective\Html\HtmlServiceProvider',
in providers and these
'Form'=> 'Collective\Html\FormFacade','Html=>'Collective\Html\HtmlFacade',
in aliases..
But i cant make change like this in my HTML file {{ html::style('/css/AdminLTE.min.css') }} its show HTML Class Not Found.
i checked in tinker
>>> Form::text('foo')
=> "<input name=\"foo\" type=\"text\">"
but its working..any help??
Html and Form classes were deprecated from Laravel 5. You can access similar functionality using Laravel Collective.
You have a typo error. Try this:
{{ Html::style('/css/AdminLTE.min.css') }}
Check this link, or try this:
Add this in composer.json require section
"illuminate/html": "5.*"
and run composer update.
Open your config/app.php
add under 'providers' array add this
Illuminate\Html\HtmlServiceProvider::class,
add under 'aliases' array add this
'Form' => Illuminate\Html\FormFacade::class,
'Html' => Illuminate\Html\HtmlFacade::class,
and under your blade templates, use like this
{!! Html::image('uploads/zami.jpg') !!}
up until this point I have essentially been using resource routing. One of my routes is for projects. If I create a project and then SHOW it, I see a URL in the form of
myUrl/projects/1
On the show page for a project, I want to be able to add a document. I have set up the relationships so a project can have one document and a document belongs to a project. I then set up the following route to handle the saving of the documents data
Route::post('projects/{id}/docOne', 'DocOneController#store');
So I add an a form in projects/show.blade.php, which opens like so
{!!
Form::model(new App\DocOne, [
'class'=>'form-horizontal',
'route' => ['docOne.store']
])
!!}
I then have my form fields and a save data button. Because of this new form within my projects show page, when I now show a project, it complains that the route for this new form is not defined.
How can I get this route to work within the projects show page?
Thanks
First of all you need to define a route name to your route, if you want to call it by his name.
So your route would be like:
Route::post('projects/{id}/docOne', [ //you need an array to set a route name
'as' => 'docOne.store', //here define the route name
'uses' => 'DocOneController#store' //here the callback
]);
Second you need to change your laravel form to use your route name and set the id
{!! Form::model(new App\DocOne, [
'route' => ['docOne.store', $project], //if you have setted the id variable like $id blade it gonna retturn it automatically only by passing the object, else, you can set $project->id
'method' => 'POST']
) !!}
EDIT:
You can't get an instance of a model on your view.
So the part:
{!! Form::model(new App\DocOne,
gonna fails every time you trye, also, the form:model needs an instance of a class that should have your vars filled with the info that the inputs should have (when you edit it).
You have two solutions:
If it's a new Doc and never before exist on your dataBase
I recomend to change your
Form::model
to:
Form::open
if it's a Doc thath already exist on your DB, like an edit, so in your controller you need to pass your existing Docas $docand remplace the: {!! Form::model(new App\DocOne, to:
{!! Form::model($doc,
and it works.
Form model was created to fill the input values with the data existing in your object instance, like when you edit someting.
So you need to have a correct instance.
Another think it's the MVC scope, a view shouldn't have acces to models, except if are passed by the controller.
Ok that's all.
$router->map('people_companies_add_owner', 'people/add/:is_owner','null', array('controller' => 'companies', 'action' => 'add','is_owner'=>'1'));
$router->map('people_companies_add_client', 'people/add/:is_owner','null', array('controller' => 'companies', 'action' => 'add','is_owner'=>'0'));
which calls controller, showing add template. in controller I m getting parameter passed tht is "is_owner".which can again be accessible by add template but when it is rerouted it calls,
{form action='?route=people_companies_add' method=post}
{include_template name=_profile_form controller=companies module=system}
I want to check the "is_owner" flag and accordingly call owner or client.can I put form action in if loop?? like
if($n==1)
form action=?route=peple_com_owner
else
form action=?route=peple_com_client
It is showing smarty error
This should work:
<form
method="post"
action="?route={if $is_owner}people_com_owner{else}people_com_client{/if}"
>