Hi. how can i input mutliple data from form into database using laravel 5.3
my code for form is below:
$trans_id = DB::table('sales')->max('trans_id');
$member_id = DB::table('sales')->where('trans_id','=', $trans_id)->value('member_id');
$name = DB::table('members')->where('member_id','=', $member_id)->value('name');
//$trans_id = DB::table('sales')->where('member_id','=', 'member_id')->value('member_id');
//$user = DB::table('users')->where('name', 'John')->first();
//int trans_id=$trans_i;
echo " ".$name;
?><p>
<form method="POST" action="/sales">
{{ csrf_field()}}
<input type="hidden" name='trans_id' value="<?php echo $trans_id;?>">
<!-- trans_id is not going to be inputed from form -->
<div class="form-group">
<label for="content">Product code</label>
<input type="text" name='product_code' class="form_control">
</div>
<div class="form-group">
<label for="content">Quantity </label>
<input type="text" name='quantity' class="form_control">
</div>
<div class="form-group">
<label for="content">Product code</label>
<input type="text" name='product_code' class="form_control">
</div>
<div class="form-group">
<label for="content">Quantity </label>
<input type="text" name='quantity' class="form_control">
</div>
<input type="submit" class="btn btn-succes pull-right" >
</form>
then the Model is called Sales, given below
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sale extends Model
{
protected $table = "transactions"; // If table name is sales then u don't need to write anythink to link to table if the Model name is Sale
protected $fillable = ['trans_id','product_code','quantity'];
}
the contoller is below
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Sale;
use Auth;
class SalesController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
// $trans_id = DB::table('sales')->where('name', 'John')->value('email');
//$trans_id = DB::table('users')->where('member_id', '4')->value('trans_id');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('sales.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
/*$sale = new Sale; // create new sale instance and set attributes of model
$sale->product_code = $request->product_code;
$sale->quantity = $request->quantity;
$sale->save();*/
Sale::create($request->all());
return redirect('/authsales/create');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
im failing to input all value in the 2 rows of data base, its only inputing the last value
Hi Mvere,
Can you please paste your database table for this entity.
Can you also try using the Input class to get the values? Input::get('product_code');
If you want more detailed explaination you can read through this. http://deepdivetuts.com/basic-create-edit-update-delete-functionality-laravel-5-3
The example uses Laravel 5.3 so it may be useful.
Hope that helps
Joel
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community