How insert checkbox to database mysql - laravel - laravel

Hey anyone can you tell me the mistake??
this checkbox i show it from my table genre
<div class="row form-group">
<div class="col col-md-3"><label class=" form-control-label">Genre Film</label>
</div>
<div class="col col-md-9">
#foreach($genre as $gr)
<div class="form-check">
<div class="checkbox">
<label for="checkbox1" class="form-check-label ">
<input type="checkbox" name="genre[]" value="{{ $gr->nama_genre }}"> {{ $gr->nama_genre }}
</label>
</div>
</div>
#endforeach
</div>
and then this is the form
enter image description here
controller
public function tambah_movlist(Request $request)
{
$movielist = new Movielist();
$movielist->poster = $poster;
$movielist->judul =$request->judul;
$movielist->tahun =$request->tahun;
$movielist->genre[] = ($request->genre);
$movielist->rating =$request->rating;
$movielist->biaya_produksi =$request->b_produk;
$movielist->pendapatan = $request->pendapatan;
$movielist->sinopsis =$request->sinopsis;
$movielist->save();
return redirect('/adminmovielist');
}

In the HTML, you defined genre value as an array so you can directly get genre value through $request->genre and can save it in the database. Actually no need array_keys().

Please change code:
$movielist->genre[] = ($request->genre);
to
$movielist->genre = $request->genre;
And check the data type of column genre in the database is it right

Related

Laravel 7- Pass database value or a value stored in a variable, to Laravel Blade

I am trying to obtain value from database and pass it to blade. If there is no value in database, a random number is generated and passed from the controller. However, I am unable to fetch the value even if it exists. I am getting 1 for every value available in database
Here are my codes
Controller
public function new_user2(Request $request){
if(!Auth::check()){
$reference = $request->refno;
if((is_null($reference))){
$ref1 = mt_rand(10,1000);
$ref2 = mt_rand();
$reference = $ref1."-".$ref2;
} else{
$tuser = DB::table('temp_users')->where('referenceno',$reference)->first();
if(is_null($tuser)){
return redirect()->back()->with('error',"Invalid Reference No. Please enter a valid one");
}else{
// return view('multipage1',compact('tuser'));
return view('multipage1',compact('tuser'));
}
}
return view('multipage1')->with('ref',$reference);
}else{
Auth::login();
}
}
Blade
<div class="body1">
<div class="container container2">
<div class="row">
<div class="col-md-8 offset-md-2">
<h1 class="text-white text-center display-2">Step 1</h1>
<div class="login-form">
<form id="newuser2_step1" action="{{url('mpage2')}}" method="post" autocomplete="off">
#csrf
<div class="form-group">
<label for="RefNo">Reference Number: </label>
<input type="text" class="form-control" id="refno" name="refno" value="{{$tuser->referenceno or $ref}}" readonly><br>
</div>
<div class="form-group">
<!-- Alert Message -->
<div class="alert alert-danger " role="alert" id="alerts">
<strong id="emailtaken"></strong>
</div>
<label for="exampleInputEmail1">Email address</label>
<input type="text" class="form-control" id="email" name="email" placeholder="Enter email" value="{{$tuser->temp_email or ''}}">
<div class="error text-danger" ></div>
</div>
<button type="submit" class="btn btn2 btn-primary" id="mstep1">Get Started</button>
</form>
</div>
</div>
</div>
</div>
</div>
Database table
Data exists in referenceno and temp_email columns
Output in Browser
According to this article, the Laravel or operator that you used, is discontinued. So, use null coalesce operator instead.
value="{{$tuser->referenceno ?? $ref}}"
Similarly, replace the email value with similar coede
You have change this type of statements {{$tuser->referenceno or $ref}}
It is basically checking true/false that why you are getting 1.
Do something like this {{ isset($tuser->referenceno) ? $tuser->referenceno : $ref}}

Update single field in a Profile Update Form in Laravel 5

I am trying to update a single field in my user profile form section in the Laravel app.
I can save fields correctly in DB but input values and placeholders are taking wrong values. In every hit Save, the values doesn' change, and they are taken from the last listed user profile details. In my case this is user#3. The problem is when I log in with the user's #1 credentials, value and placeholder are taken from user #3. When I log in with user #2, again from user #3. Only values of user#3 are correct and I can manipulate it with no issues for both fields.
When i update the profile fields with user#1 it saves the entered one filed, but because the 2nd filed inherits the user#3 input details it saves it in field 2 of user#1 which makes a wrong entry. I can't leave null in those fields by default. My mass assignment is guarded.
How can save/update just a single field in the blade template without affecting the other fields in the form?
My routes:
Route::get( '/profile', 'userController\\profileEdit#profileEdit')->name('profileEdit');
Route::post('/profile', 'userController\\profileEdit#update')->name('update');
My controller:
namespace App\Http\Controllers\userController;
use App\Model\Hause_users;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class profileEdit extends Controller
{
function profileEdit (Request $request){
$user = Hause_users::all();
$name = $request->session()->get('name');
$request->session()->keep([request('username', 'email')]);
return view('frontview.layouts.profile',['user'=>$user])->with('username' , $name );
}
function update (Request $request){
$user = Hause_users::where('username', $request->session()->get('name'))->first();
$user->fill(['email' => request('Email')]) ;
$user->save();
$user->phone;
//dd($user->phone->phone);
if ($user->phone === null) {
$user->phone->phone->create(['phone' => request('tel')]);
}
else{
$user->phone->update(['phone' => request('tel')]);
}
return back()->withInput();
}
Blade file: `
#extends('frontview.layouts.userView')
#extends('frontview.layouts.default')
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#section('title')
#endsection
#section('content')
#foreach($user as $v )
#endforeach
<h2 class="form-group col-md-6">Здравей, {{$username }} </h2>
<form class = "pb2" method="POST" name = 'profile' action='profile' >
{{ csrf_field()}}
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">Поща</label>
<input type="email" class="form-control" name = "Email" id="inputEmail4"
value="{{$v['Email']}}"
placeholder="{{$v->Email}}">
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Промени Парола</label>
<input type="password" class="form-control" id="inputPassword4" placeholder="Парола">
</div>
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" name = "Adress" id="inputAddress" placeholder="Снежанка 2">
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputAddress">Телефон</label>
<input class="form-control" type="text" name = 'tel' value="{{$v->phone['phone']}}"
placeholder="{{$v->phone['phone']}}"
id="example-tel-input" >
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputCity">Град</label><input type="text" class="form-control" id="inputCity">
<label for="inputCity">Квартал</label><input type="text" class="form-control" id="inputCity">
</div>
{{--<div class="col-md-6" >--}}
{{--<label for="image">Качи снимка</label>--}}
{{--<input type="file" name = "image">--}}
{{--<div>{{$errors->first('image') }}</div>--}}
{{--</div>--}}
</div>
{{--<div ><img src="https://mdbootstrap.com/img/Photos/Others/placeholder-avatar.jpg"--}}
{{--class="rounded-circle z-depth-1-half avatar-pic" alt="example placeholder avatar">--}}
{{--</div>--}}
{{--<div class="d-flex justify-content-center">--}}
{{--<div class="btn btn-mdb-color btn-rounded float-left">--}}
{{--<span>Add photo</span>--}}
{{--<input type="file">--}}
{{--</div>--}}
{{--</div>--}}
{{--</div>--}}
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="gridCheck">
<label class="form-check-label" for="gridCheck">
Запомни ме!
</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Запази</button>
</form>
#endsection
#section('name')
{{ $username }}
#endsection
Output summary:
On img#1 are the correct entry details . This is other section not the Profile edit one. Currently loged user is U#1 but as you can see on image 2, values and placeholder of both fields are for the U#3. When i hit the blue button U#1 saves the untouched filed input of U#3. Same is when i log in with U#2.
Actually the answer here is quite simple. What i am doing wrong is that i am not passing the value of the currently logged user to the view correctly. On my profileEdit method i was using $user = Hause_users::all(); and then looping trough all id's into the view and then fetching every field. But because the view doesn know which user passes the data, the foreach always returns the last user id from the array with its input, no matter which user is currently logged in. Then the data was overridden with wrong inputs.
The solution is also simple.
Instead of $user = Hause_users::all();
i have used
$user = Hause_users::where('username', $request->session()->get('name'))->first();
and then into view i was objecting the $user variable without any loops like this:
<form class = "pb2" method="POST" name = 'profile' action='profile' >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{{--<input type="hidden" name="_method" value="PATCH">--}}
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">Поща</label>
<input type="Email" class="form-control" name = "Email" id="inputEmail4"
value="{{$user->Email}}"
placeholder="{{$user->Email}}">
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Промени Парола</label>
<input type="password" class="form-control" id="inputPassword4" placeholder="Парола">
</div>
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" name = "Adress" id="inputAddress" placeholder="Снежанка 2">
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputAddress">Телефон</label>
<input class="form-control" type="text" name = 'tel' value="{{$user->phone['phone']}}"
placeholder="{{$user->phone['phone']}}"
id="example-tel-input" >
Basically this is a detailed explanation to all that not using the built in Auth system of Laravel

Dropdown menu from any table in database - Laravel

I have a form, which has some text field.
Let me explain this field:
So_number from table sales_order
so_date from table sales_order
customer_name from table customers
and then I've got linked all of model and controller and views
My controller is linked of my 3 models.
and I try to call with {{}} this, as same as like variable as I made,
I call the details with this
<td>{{$so->so_number}}</td>
this is my web.php of details
Route::get('/so/{so_number}/details', 'So_Controller#details');
this is my So_Controller#details:
public function details($so_number)
{
$data_so = \App\Sales_Order::all();
$data_so_detail = \App\Sales_Order_Details::all();
$data_customer = \App\Customer_Model::all();
return view('salesorder.details', ['data_so_detail' => $data_so_detail, 'data_so' => $data_so, 'data_customer' => $data_customer]);
}
This is my views of my details.index.php
<div class="modal-body">
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<input type="text" name="so_number" value="{{$data_so->so_number}}" class="form-text" required>
<span class="bar"></span>
<label for="so_number">Sales Order Number</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<input type="text" name="so_date" value="{{$data_so->so_date}}" class="form-text" required>
<span class="bar"></span>
<label for="so_date">Sales Order Date.</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<label for="customer_name">Customer Name</label>
<select name="customer_name" id="customer_name">
#foreach ($data_customer as $customerName)
<option value="{{$customerName->customer_name}}"></option>
#endforeach
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<label for="customer_id"></label>
<select name="customer_id" id="customer_id">
#foreach ($data_customer as $customerId)
<option value="{{$customerId->customer_id}}"></option>
#endforeach
</select>
</div>
</div>
And the error is
ErrorException (E_ERROR)
Property [so_number] does not exist on this collection instance. (View: /Applications/XAMPP/xamppfiles/htdocs/belajar_laravel/resources/views/salesorder/details.blade.php)
Previous exceptions
Property [so_number] does not exist on this collection instance. (0)
My expected output is list of items in dropdown.
Please help me.
In your details.blade.php, you have written
{{$data_so->so_number}}
whereas in controller, you have written
$data_so = \App\Sales_Order::all();
all() returns a collection which is like a 2 dimensional array. Either you have to iterate through $data_so to access 'so_number' or in controller you have to write
$data_so = \App\Sales_Order::first(); //this is an example, first() or find() will return an instance of the model and you can directly access it like $data_so->so_number.
In another case, check your method details($so_number).
In case you want to retrieve from \App\Sales_Order a record for $so_number, you may use find().
You may read https://laravel.com/docs/5.8/eloquent-relationships to connect multiple models.

cannot show data from model object

I want to change the data absent but an error appears 'Trying to get the property' id 'of non-object' I do not understand why even though I have done it before and it's fine. and my relationship table is in apache MySql so my model is blank.
I've tried to match my other edit method but nothing is wrong
this my edit()
public function edit($id)
{
$absensi = absensi::find($id);
$dataSiswa = DB::table('absensis')
->join('siswas', 'siswas.id', '=', 'absensis.idSiswa')
->select('absensis.*', 'siswas.nama')
->where([
['siswas.id', $absensi->idSiswa],
])->first();
return view('absensi.edit', ['absensi'=> $dataSiswa]);
}
and then this is my blade
<div class="card-header">
<h1>Edit Data Absensi </h1>
</div>
<div class="card-body">
<form action="/absensi/{{$absensi->id}}" method="POST" name="form1" >
{{ csrf_field() }}
<div class="form-group">
<label for="usr">Nama :</label>
<input type="text" value="{{$dataSiswa->nama}}">
</div>
<div class="form-group">
<label for="usr">Semester :</label>
<select name="semester" class="form-control" id="sel1">
<option value="genap"#if ($dataSiswa->semester=="ganjil") selected #endif>ganjil</option>
<option value="ganjil"#if ($dataSiswa->semester=="genap") selected #endif>genap</option>
</select>
</div>
<div class="form-group">
<input type="hidden" class="form-control" name="kelas" value="{{$dataSiswa->kelas}}">
</div>
<div class="form-group">
<label for="comment">izin:</label>
<input type="number" class="form-control" name="izin" value="{{$dataSiswa->izin}}">
</div>
<div class="form-group">
<label for="comment">Sakit :</label>
<input type="number" class="form-control" name="sakit" value="{{$dataSiswa->izin}}">
</div>
<div class="form-group">
<label for="comment">Tanpa Keterangan :</label>
<input type="number" class="form-control" name="tanpaKeterangan" value="{{$dataSiswa->izin}}">
</div>
<button type="submit" class="btn btn-primary" id="Submit" name="Submit">simpan perubahan</button>
</form>
</div>
</div>
</div>
change
$absensi = absensi::find($id);
to
$absensi = absensi::findOrFail($id);
i think the problem is that the model cannot find the record with that id
and if thats not the case then
$dataSiswa = DB::table('absensis')
->join('siswas', 'siswas.id', '=', 'absensis.idSiswa')
->select('absensis.*', 'siswas.nama')
->where([
['siswas.id', $absensi->idSiswa],
])->first();
to
$dataSiswa = DB::table('absensis')
->join('siswas', 'siswas.id', '=', 'absensis.idSiswa')
->select('absensis.*', 'siswas.nama')
->where([
['siswas.id', $absensi->idSiswa],
])->first();
if(!$dataSiswa) {
// throw new notfound exception here or return back
}
return view('absensi.edit', ['absensi'=> $dataSiswa]);
In other words you are trying to access a property of a null object because the queries you made are returning a null object, and on your blade you are tying to access a null object property
You should probably look into defining your relationships with eloquent instead as per the docs.
Once that's done you can use Eloquent to query your table like so:
$abensis = abensis::findOrFail($id);
$dataSiswa = $abensis->siswa;
return view('absensi.edit', ['absensi'=> $dataSiswa]);

How to display selected value and image in edit page to perform update operation using laravel

Hello,
I am new learner in Laravel and my first post in Stackoverflow so applogy to me for my mistake and English language.
I can create a project shoppingcart & perform CRUD operation where create product information such as product name, brand, description, price & image. I can done create & read operation easily. Below my product list that can be show in browser
enter image description here
When I click edit button browser can show like that below image to perform edit operation
enter image description here
where brand name & image can't shown edit product form
My productController code below like that
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\brand;
use App\Product;
use Illuminate\Support\Facades\DB;
class productController extends Controller
{
public function index(){
$product = DB::table('products')
->join('brands','products.brandID','=','brands.brandID')
-> select('products.*','brands.brandName')
->get();
return view('admin.product.productList',compact('product'));
}
public function create()
{
$product = brand::pluck('brandName','brandID');
return view('admin.product.createProduct',compact('product'));
}
public function store(Request $request)
{
// image upload
$product = $request->except('image');
$image = $request->image;
if ($image) {
$imageName = $image->getClientOriginalName();
$image->move('images', $imageName);
$product['image'] = $imageName;
}
Product::create($product);
return view('admin.product.productList', compact('product'));
}
public function show($id)
{
$product = Product::find($id);
return view('admin.product.editProduct',compact('product'));
}
public function edit($id)
{
}
public function delete($id){
$product= Product::find($id);
$data=$product->delete();
return view('admin.product.productList',compact('data'));
}
}
my editProduct.blade file code below like that
#extends('layouts.master')
#section('content')
<div class="container" align="center">
<div class="col-md-6 col-md-offset-3">
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Edit Product
</div>
<div class="panel-body">
<form action="edit" class="form-horizontal" method="POST>
{{csrf_field()}}
<div class="form-group">
<label for="name" class="col-md-4 control-label">Product Name :</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" value="{!! $product->productName !!}" name="name" required autofocus>
</div>
</div>
<div class="form-group">
<label for="name" class="col-md-4 control-label">Brand :</label>
<div class="col-md-6">
<select name=" " class="form-control" >
<option>
</option>
</select>
</div>
</div>
<div class="form-group">
<label for="description" class="col-md-4 control-label">Description:</label>
<div class="col-md-6">
<input id="description" class="form-control" name="description" value="{!! $product->description !!}" required>
</div>
</div>
<div class="form-group">
<label for="price" class="col-md-4 control-label">Price:</label>
<div class="col-md-6">
<input id="price" value="{!! $product->price !!}" class="form-control" name="price" required>
</div>
</div>
<div class="form-group">
<label for="image" class="col-md-4 control-label">Image:</label>
<div class="col-md-6">
<input id="image" type="file" value="{!! $product->image !!}" class="btn-btn-default" name="image" value="Upload" required>
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
ADD
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
#endsection
Here, brand name can be selected by HTML select attribute and value get another model Brand & the data can be retrieve from Brand model using join query which is included productController index method.
So how can I show brand name & image to edit in the editProduct blade file. Pls help me
There are couple of ways, here's 1 way.
In your controller.
public function edit($id)
{
$product = Product::find($id);
$data = [
'product_info' => $product
];
return view('your.edit.view.here', $data);
}
And inside your view, inside the FORM Tag, do the looping.
#foreach($product_info as $info)
// data info here
<input id="description" value="{{ $product->description }}" required>
#endforeach
EDIT::
Use Laravel Encryption, you need it especially web apps like shopping cart.
URL:: http://localhost:8000/product/show/encrypted_id_here
public function show($id) {
$id = decrypt($id);
}

Resources