Inputting multiple row entries by a factor in laravel - laravel

I am attempting to recursively input a quantity of rows into a mysql database. The rows will all have the same product id but an incremental name given by product_id_incremental_number. My form is as follows:
<div class="row">
<div class="col-lg-6">
<h1>Add a new entry to the database</h1>
<p class="lead">Add your entry below:</p>
<hr>
{!! Form::open(array('method'=>'POST', 'route'=>array('inventory.store'))) !!}
<div class="form-group">
{!! Form::label('production_id', 'Production ID', ['class' => 'control-label']) !!}
{!! Form::text('production_id', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('quantity', 'Quantity of chips:', ['class' => 'control-label']) !!}
{!! Form::number('quantity', null, ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Create New Batch Entry', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
</div>
</div>
#stop
My controller is as follows:
public function store()
{
$data = Input::all();
$batch = $data->replicate($data['quantity']);
$batch->save();
}
return Redirect::route('inventory.index')->with('flash_notice', "Batch Created");
}
I know this isn't the full function to add incremental names to each of the items that would be in the batch, I haven't reached that point yet. My problem is that I cannot replicate an array to then insert each replicate of the batch into the database. Any help on this would be greatly appreciated.
TL;DR: It's a similar problem to ordering more than one pizza on an online cart, you just click a counter that increases the number of pizza that are added to the cart database.
Many thanks,
MF

Related

Laravel 5.2, carry over article ID to next page and insert into database field

I am trying to use Laravel 5.2 to add questions to an article. Currently I can edit the article, using the edit function in the ArticleController. I want to have a button on the Article edit view, that allows the user to add a question. So when this is clicked, I need it to take the user to /articles/questions/create, but it needs to bring the ID for the article it has just come from with it, so it can be automatically stored in the article_id field in the db table. How would I do this, currently I can store the article ID by manually typing the in a text box on the creation form. Obviously, the aim is for the user to not have to know the ID of the article, and for it to be added automatically.
My Create function so far:
public function create(){
$questions = Question::all();
$users = User::all();
$articles = DB::table('article')->where('id', 'article_id')->get();
return view('articles/questions/create', ['question' => $questions], ['users' => $users], ['article' => $articles]);
}
The form so far:
<h1>Create & Add Question</h1>
{!! Form::open(array('action' => 'QuestionController#store', 'id' => 'createquestion')) !!}
{{ Form::hidden(csrf_token()) }}
<div class="row col-sm-12 col-lg-12">
{!! Form::label('article_id', 'Article ID:') !!}
{!! Form::text('article_id', null, ['class' => 'large-8 columns']) !!}
</div>
<div class="row col-sm-12 col-lg-12">
{!! Form::label('title', 'Question:') !!}
{!! Form::textarea('title', null, ['class' => 'large-8 columns']) !!}
</div>
<div class="row col-sm-12 col-lg-12">
{!! Form::label('require', 'Required?') !!}
{{ Form::radio('require', 1) }} Yes <br>
{{ Form::radio('require', 0) }} No
</div>
<br>
<div class="row large-4 columns">
{!! Form::submit('Add Question', ['class' => 'button']) !!}
</div>
{!! Form::close() !!}
The form does successfully post the the Database, I just need the ID to automatically insert so that the article can reference it to show the question on the article page.
First of all edit the below lines
$articles = DB::table('article')->where('id', 'article_id')->get();
return view('articles/questions/create', ['question' => $questions], ['users' => $users], ['article' => $articles]);
to
$articles = DB::table('article')->where('id', 'article_id')->first();
return view('articles/questions/create', ['question' => $questions, 'users' => $users, 'article' => $articles]);
Now, take a hidden field in the form you posted above
{!! Form::hidden('article_id', $article->id) !!}
and finally access the article id in your store method.
public function store(Request $request){
$article_id = $request->article_id
}

laravel how to use star rating plugin on a form text input

My project is on storing reviews on products. I am using Laravel framework here. The review has a field for rating the product. I am using the jquery 'starrr' plugin for the rating.
Form :
{!! Form::open(['url'=>'/admin/reviews', 'role' => 'form', 'class' => 'form-horizontal']) !!}
<div class="form-group">
{!! Form::label('title', "Title:", ['class'=>'control-label col-md-2']) !!}
<div class="col-md-8">
{!! Form::text('title', null, ['class'=>'form-control'])!!}
</div>
</div>
<div class="form-group">
{!! Form::label('product_id', "Product:", ['class'=>'control-label col-md-2']) !!}
<div class="col-md-8">
{!! Form::select('product_id', $products, null, ['class'=>'form-control product_list'])!!}
</div>
</div>
<div class="form-group">
{!! Form::label('rating', "Rating:", ['class'=>'control-label col-md-2']) !!}
<div class="starrr"></div>
{!! Form::hidden('rating', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
<div class="col-md-3 col-md-offset-4">
{!! Form::button('<i class = "fa fa-plus-circle"></i> Add New Review', ['type' => 'submit', 'class'=>'btn btn-primary btn-lg']) !!}
</div>
</div>
{!! Form::close() !!}
controller store method:
public function store(Request $request){
$rules = [
'product_id' => 'required',
'title' => 'required',
'content' => 'required',
];
$this->validate($request, $rules);
create($request->all());
return redirect('admin/reviews')->withSuccess('Review is created');
}
My question is how to get the rating value from the star rating, into the request object to store on the database?
Thanks for your help in advance
$('.starrr').starrr({
change: function(e, value){
$('input[name="value"]').val(value)
}
})
This will set the input to the value of the starrr plugin whenever it is changed. Then the input value will be sent to the server

Form model binding laravel 5.1 for multiple models

I want Form model binding for multiple objects in laracollective's Form package?
Something as following?
Form::model([$user,$vendors], array('route' => array('user.update', $user->id)))
Where can I request this feature?
I assume you're using Laravel-Collective, Unfortunately you cant do something like that. instead you can try something like this :
UPDATE
you can query all your model in your controller and combine them like this :
$user = User::where('id',$user_id)->get();
$vendor = Vendor::where('user_id',$user_id)->get();
//merge two model
$user = $user->merge($vendor);
// return $user;
return view('admin.users.edit', compact('user'))
->withTitle('Edit user');
and in your form call them like this :
{!! Form::model($user[1], ['route' => ['admin.users.update', $user],'method'=>'PUT']) !!}
#include('admin.users._formEdit')
<div>
{!! Form::submit('Save user', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
_formEdit.blade.php
<div class="form-group">
{!! Form::label('first_name', 'First Name : ') !!}
{!! Form::text('user[first_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('last_name', 'Last Name : ') !!}
{!! Form::text('user[last_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('email', 'Email : ') !!}
{!! Form::email('user[email]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('vendor_name', 'vendor_name') !!}
{!! Form::text('vendor_name', null,['class' => 'form-control']) !!}
</div>
OR ANOTHER SOLUTION
create relation between model of your User and Vendor (one-to-one or one-to-many) example
User :
public function vendor(){
return $this->hasOne('App\Vendor','user_id');
}
Vendor:
public function user(){
return $this->belongsTo('App\User','user_id);
}
Build your response query like this :
$user = Vendor::with('user')->find($user_id);
and then in your view template :
{!! Form::model($user, ...) !!}
Vendor: {!! Form::text('vendor_name') !!}
User: {{ Form::text('user[username]') }}
{!! Form::close() !!}

Form model binding not working in laravel 5.2 using laravelcollective

Hello I'm new to laravel 5.2 and going through some lessons.
For some reason form model binding is not working for me.
{!! Form::model($post, ['method'=>'PATCH', 'action'=> ['PostController#update', $post->id]]) !!}
I received data in $post because I'm using a workaround like this:
{!! Form::text('title', "$post->title" ,['class'=> 'form-control']) !!}
And that is showing my data.
Controller:
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
class PostController extends Controller{
public function update(Request $request, $id){
$post =Post::findOrfail($id);
$post->update($request->all());
return redirect('/posts');
}
}
create.blade.php view:
#section('content')
<h1>Create Post</h1>
{!! Form::open(['method'=>'POST', 'action'=>'PostController#store']) !!}
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
</div>
<!-- Form Input -->
<div class="form-group">
{!! Form::submit('Create Post', ['class'=> 'btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#endsection
You shouldn't be quoting the value for null in your input:
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
should be
{!! Form::text('title', null, ['class'=> 'form-control']) !!}
Ok, try adding the body of the form into a partial called posts/partials/form.blade.phpand include it between the form open / model and form close tags.
Example:
posts/partials/form.blade.php
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
</div>
<!-- Form Input -->
<div class="form-group">
{!! Form::submit($formButtonText, ['class'=> 'btn-primary form-control']) !!}
</div>
posts/create.blade.php
{!! Form::open(['method'=>'POST', 'action'=>'PostController#store']) !!}
#include('posts.partials.form', [
'formSubmitButtonText' => 'Create Post'
])
{!! Form::close() !!}
posts/edit.blade.php
{!! Form::model($post, ['method'=>'PATCH', 'action'=> ['PostController#update', $post->id]]) !!}
#include('posts.partials.form', [
'formSubmitButtonText' => 'Update Post'
])
{!! Form::close() !!}

Yield a form on a different page using laravel

I'm making a twitter look-a-like app.
I have a form to post a message on 1 blade (createMessage.blade.php) which is as followed:
#section('message')
{!! Form::open(['url' => 'message/postmessage']) !!}
<div class="form-group col-lg-6 col-lg-offset-3">
{!! Form::label('body') !!}
{!! Form::textarea('body', null,['class' => 'form-control']) !!}
</div>
<div class="form-group col-lg-6 col-lg-offset-3">
{!! Form::submit('Post message', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#endsection`
Now I want to get my form on different pages (such as my timeline) but if I try #yield('message') it doesn't work.
timeline:
#extends('layouts.app')
#section('content')
#yield('message')
//some code for my timeline
#endsection
I can't find out why it doens't work.
Thanks in advance!
You have to use
#include('createMessage.blade.php') in the timeline file and remove the #section declaration in createMessage.blade.php

Resources