how to insert multiple field data in one column in database in laravel - laravel-5.7

I want to play with this fields,i want to insert description1,description2 data into this description[] how to do that i tried but something is missing.
<div class="form-group row">
<label for="description" class="col-md-4 col-form-label">Description</label>
<div class="col-md-8">
<input type="text" class="form-control" name="description[]" placeholder="Enter description">
</div>
</div>
<div class="form-group row">
<label for="description" class="col-md-4 col-form-label">Description 1 (optional)</label>
<div class="col-md-8">
<input type="text" class="form-control" name="description2" placeholder="Enter description">
</div>
</div>
<div class="form-group row">
<label for="description" class="col-md-4 col-form-label">Description 2 (optional)</label>
<div class="col-md-8">
<input type="text" class="form-control" name="description3" placeholder="Enter description">
</div>
</div>
public function store(Request $request)
{
$description1 = $request->input('description1');
$description2 = $request->input('description2');
$products = new ProductAdd;
$prodduct->name = $request('name');
$prodduct->description[] = [
'description1' => $description1;
'description2' => $description2;
];
$product->save();
}

If you cast the description field to be an array in your model, you can update your controller code to:
$prodduct->description = [
'description1' => $description1;
'description2' => $description2;
];
To cast the field to an array, add this to your ProductAdd Model:
$casts = [
'description' => 'array'
];

Related

how to pass data from bootstrap form to mockemployeerepo in asp .net core razor pages

<div class="form-group row">
<label asp-for="EmployeeEditProperty.Name" class="col-sm-2 col-form-label">
</label>
<div class="col-sm-10">
<input asp-for="EmployeeEditProperty.Name" class="form-control" placeholder="Name">
</div>
</div>
<div class="form-group row">
<label asp-for="EmployeeEditProperty.Email" class="col-sm-2 col-form-label"></label>
<div class="col-sm-10">
<input asp-for="EmployeeEditProperty.Email" class="form-control" placeholder="Email">
</div>
</div>
Data not passing to
Employee employee= _EmployeeList.FirstOrDefault(e => e.ID == UpdatedEmployee.ID);
if(employee !=null)
{
employee.Name = UpdatedEmployee.Name;
employee.Email = UpdatedEmployee.Email;
employee.Department = UpdatedEmployee.Department;
}
The properties in view should match the property name in your post action,
You can check my demo:
<form method="post">
<div class="form-group row">
<input hidden asp-for="EmployeeEditProperty.ID" value="1" />
</div>
<div class="form-group row">
<label asp-for="EmployeeEditProperty.Name" class="col-sm-2 col-form-label">
</label>
<div class="col-sm-10">
<input asp-for="EmployeeEditProperty.Name" class="form-control" placeholder="Name">
</div>
</div>
//....
<input type="submit" value="Submit" />
The property names EmployeeEditProperty, so in post method, it should be EmployeeEditProperty too:
public ******(Employee EmployeeEditProperty)
Result:
Model Binding in razor pages, you can check this article

How to move data from one action to another action in the same controller for forms?

How do I move data from an Iactionresult to another action result? I have been trying to display the data from the form and view it another Iactionresult? I attempt to use Tempdata but it seems like there is an error. Could anyone help me with it?
This action displays an individual product details when I click on an particular Id.
[HttpGet]
public IActionResult Details(int id)
{
string sql = String.Format(#"SELECT * FROM WBProduct
WHERE Id = {0}", id);
List<Product> lstProduct = DBUtl.GetList<Product>(sql);
if (lstProduct.Count == 0)
{
TempData["Message"] = $"Product #{id} not found";
TempData["MsgType"] = "warning";
return RedirectToAction("Index");
}
else
{
Product cdd = lstProduct[0];
return View(cdd);
}
}
I would like to display the the details of the product in this IActionResult
[HttpPost]
public IActionResult Create()
{
return View("Create");
}
View for Details:
#model Product
<div>
<div class="form-group row">
<div class="offset-sm-2"><h2>#Model.ProductName</h2></div>
</div>
<div class="form-group row">
<div class="offset-sm-2 col-sm-5">
<img id="ImgPhoto" src="~/images/product/#Model.ProductImage" style="width:400px;" />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="City">Weight: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductWeight" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Date">Stock :</label>
<div class="col-sm-5">
<input type="text" asp-for="ProductStock" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Cost">Price: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductPrice" asp-format="{0:C}" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Story">Description: </label>
<div class="col-sm-5">
<textarea asp-for="ProductDescription" rows="8" cols="20" class="form-control" readonly></textarea>
</div>
</div>
<div class="form-group row">
<a href="http://localhost:50528/Product/Create" class="btn btn-info" role="button" > Add to Cart </a>
</div>
</div>
Create View:
#model Product
<div class="form-group row">
<div class="offset-sm-2"><h2>#Model.ProductName</h2></div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="City">Weight: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductWeight" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Date">Stock :</label>
<div class="col-sm-5">
<input type="text" asp-for="ProductStock" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Cost">Price: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductPrice" asp-format="{0:C}" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="Story">Description: </label>
<div class="col-sm-5">
<textarea asp-for="ProductDescription" rows="8" cols="20" class="form-control" readonly></textarea>
</div>
</div>
The error message that I got was:
This should be a GET action, not a POST one, then you should extract the info from the TempData and pass it as parameter to the view cshtml.
TempData["Product"] = JsonConvert.SerializeObject(lstProduct[0]);
return RedirectToAction("Create");
Now you can deserialize in the Create action and retrieve your Product
[HttpGet]
public IActionResult Create()
{
// If the caller has prepared a product we can show it.
if(TempData.ContainsKey("Product"))
{
Product p = JsonConvert.DeserializeObject<Product>(TempData["Product"]);
return View(p);
}
else
return View();
}
If you want to move data from one action to another action in the same controller
just call one action from another and put data as an input parameter of another action.
To send message to Index action, at first create a class for the message:
public class ErrorMsg
{
public string Message {get; set;}
public string MessageType {get; set;}
}
Change your action Index to this:
public IActionResult Index(ErrorMsg errorMsg)
{
// if action called from another controller action, details for exapmple,
//errorMsg will contain data from that action
// otherwise errMsg will be an empty default object with empty strings
//Check if error
if(!string.IsNullOrEmpty(errorMsg.Message) ...your error code
else ....your index code here
}
Change your action details code:
public IActionResult Details(int id)
{
string sql = String.Format(#"SELECT * FROM WBProduct
WHERE Id = {0}", id);
List<Product> lstProduct = DBUtl.GetList<Product>(sql);
if (lstProduct.Count == 0)
{
var errMsg = new ErrMessage {
Message = $"Product #{id} not found",
MessageType = "warning"
}
return Index(errMsg);
}
else
{
Product cdd= lstProduct.FirstOrDefault();
//Or you can try again var cdd = lstProduct[0]; if you like it more
return View("Details", cdd);
}
}
Change your create action to this:
public IActionResult Create(Product product)
{
// if action called from another controller action, "product" will contain data //from that action
// otherwise "product" will be posted from the view or it will be an empty model with the default value fields
if(product.Id ==0) ... call add ef code
else ... call update ef code
}
And you have to add <form tag to all your views, othewise if will not post back any data, and add Product.Id hidden field inside of form:
#model Product
#using (Html.BeginForm("Create", "Product", FormMethod.Post)
{
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div>
<input type="hidden" asp-for="#Model.Id" />
<div class="form-group row">
<div class="offset-sm-2"><h2>#Model.ProductName</h2></div>
</div>
<div class="form-group row">
<div class="offset-sm-2 col-sm-5">
<img id="ImgPhoto" src="~/images/product/#Model.ProductImage" style="width:400px;" />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="City">Weight: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductWeight" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="ProductStock">Stock :</label>
<div class="col-sm-5">
<input type="text" asp-for="ProductStock" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="ProductPrice">Price: </label>
<div class="col-sm-5">
<input type="text" asp-for="ProductPrice" asp-format="{0:C}" class="form-control" readonly />
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-2" for="ProductDescription">Description: </label>
<div class="col-sm-5">
<textarea asp-for="ProductDescription" rows="8" cols="20" class="form-control" readonly></textarea>
</div>
</div>
<div class="form-group row">
<button class="btn btn-info btn-link" type="submit"> Add to Cart </button>
</div>
</div>
}

Missing argument 2 in Laravel Controller

I am trying to log a user in and take them to a page that displays content based on a slug.
I get this error :
ErrorException in ApplicantLoginController.php line 21:
Missing argument 2 for App\Http\Controllers\Auth\ApplicantLoginController::Login()
This is my controller :
public function Login(Request $request, $slug){
//Validate the form
$this->validate($request, [
'email' => 'required',
'password' => 'required',
'agree' => 'required'
]);
//Attempt to log the user in
if (Auth::guard('applicant')->attempt(['email' => $request->email,'password' => $request->password] )) {
$house = House::where('slug', '=', $slug)->first();
return view('client.index')->withHouse($house);
}
return redirect()->back()->withInput($request->only('email'));
}
This is the route to the page I am taking them to after login :
Route::get('client/index/{slug}', ['as' => 'client.index', 'uses' => 'ClientRegistrationController#index']);
This are my login routes :
Route::get('client/login', 'Auth\ApplicantLoginController#ShowLoginForm')->name('client.login');
Route::post('/client/login', 'Auth\ApplicantLoginController#Login')->name('client.login.submit');
Any ideas on how I can solve this problem?
This the button that takes the user to the page with the content based on a slug
Book now
If the user is not logged in the page redirects to this login page:
<form class="form-horizontal" role="form" method="POST" action="{{ route('client.login.submit') }}">
<input name="_token" type="hidden" value="{{ csrf_token()}}"/>
<div class="form-group">
<label for="email" class="col-md-3 control-label"></label>
<div class="col-md-6">
<input type="email" class="form-control" placeholder="Your Email" name="email" required="">
</div>
</div>
<div class="form-group">
<label for="password" class="col-md-3 control-label"></label>
<div class="col-md-6">
<input type="password" class="form-control" placeholder="Password" name="password" required="">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<div class="checkbox">
<label>
<input type="checkbox" name="agree" required=""> I have read and accepted the terms and condition
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-3">
<input type="submit" value="Login" class="btn btn-primary">
</input>
<a class="btn btn-link" href="#">
Forgot Your Password?
</a>
</div>
</div>
</form>
You should have $slug declared in the post route:
Route::post('/client/login/{slug}', 'Auth\ApplicantLoginController#Login')->name('client.login.submit');
And change your form to add the parameter:
<form class="form-horizontal" role="form" method="POST" action="{{ route('client.login.submit', ['slug' => $slug]) }}">

how to get data by finding all and finding one at the same time

In my project on the user edit page I would like to show the current role,wich i get by findOne byId,but I would also like to get all available roles so that I can choose, roles are in a separate collection, and I dont have a problem when displaying eather one...or all..
<div class="form-group col-lg-12">
<label>Email</label>
<input type="email" class="form-control" id="email" value="{{emails.[0].address}}">
</div>
<div class="form-group col-lg-12">
<label>Password</label>
<input type="password" name="" class="form-control" id="password" value="{{password}}">
</div>
<div class="form-group col-lg-6">
<label>Name</label>
<input type="text" name="" class="form-control" id="firstname" value="{{profile.name}}">
</div>
<div class="col-xs-12"><strong>Current role:</strong>
<label class="radio-inline">
<input type="radio" name="optradio" id="accountRole" value="{{roleName profile.role}}" checked>{{roleName profile.role}}
</label>
</div>
{{/with}}
<div class="col-xs-12">
<form>
<div class="col-xs-12"><strong>Roles:</strong>
{{#each rolesInformation}}
<label class="radio-inline">
<input type="radio" name="optradio" id="accountRole" value="{{_id}}">{{roleName}}
</label>
{{/each}}</div>`
helpers look like this:
Template.editUser2.helpers({
user: () => {
return Meteor.users.findOne({_id:Session.get('id')});
},
playerInformation: () => {
return Players.find().fetch();
},
roleName: (id)=>{
return Roles.findOne({_id:id}).roleName;
},
rolesInformation: () =>{
return Roles.find().fetch();
},
when i run roleName or rolesInformation separateli it works fine...
whet both included i get "roleName not defined"

Aurelia .withMessage validation css

I have the following code:
JS
.if(() => { return this.HasProvidedEitherSku1OrSku2 === false })
.isNotEmpty().withMessage(‘at least one sku is required')
.hasLengthBetween(0, 50)
.endIf()
.ensure('Sku2', (config) => {config.computedFrom(['Sku1'])})
.if(() => { return this.HasProvidedEitherSku1OrSku2 === false })
.isNotEmpty().withMessage(‘at least one sku is required’)
.hasLengthBetween(0, 50)
.endIf();
HTML
<div class="col-sm-6">
<div class="form-group">
<label class="control-label">SKU</label>
<input disabled.bind="readonly" type="text" class="form-control" value.bind="Sku1">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label class="control-label"> SKU</label>
<input disabled.bind="readonly" type="text" class="form-control" value.bind="Sku2">
</div>
</div>
withMessage seens to react differently when contained within an if statement. rather than adding a 'has-warning' to the form-group. It just adds:
<p class="help-block **aurelia-validation-message**">at least one sku is required</p>
How do I force a 'has-warning'? Or have something close to an if statement. If p class = "" then grab label above and input below.

Resources