Error store data laravel when have image in pages - laravel

I am insert data product with images in dashboard, when I try to order the product I have error 404 not found and the URL showing value database in table like http://127.0.0.1:8000/shops/order/[%7B%22id%22:2,%22category_id%22:1,%22name_product%22:%22asdasd%22,%22harga%22:123123123,%22image%22:%22product-images//HapOzHmJPim1kIfmIeCM21xesCnIbR5Z7lpzcO8M.jpg%22,%22published_at%22:null,%22created_at%22:%222022-05-23T16:25:00.000000Z%22,%22updated_at%22:%222022-05-23T16:25:10.000000Z%22,%22kategori%22:%7B%22id%22:1,%22name_category%22:%22Top%22,%22created_at%22:%222022-05-23T16:22:40.000000Z%22,%22updated_at%22:%222022-05-23T16:22:40.000000Z%22%7D%7D].
But when I am insert data product without images in dashboard, and then I try to order the product is successful.
This is my route
Route::post('/shops/order/{shop:id}', [OrderController::class, 'store']);
This is OrderController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Order;
use App\Models\User;
use App\Models\Shop;
class OrderController extends Controller
{
public function store(Request $request)
{
$createOrder = $request->validate([
'user_id' => 'required',
'name_product_id' => 'required',
'size' => 'required',
'no' => 'required',
'address' => 'required'
]);
Order::create($createOrder);
return redirect('shops/order/{shop:id}')->with('success', 'Order is successfully!');
}
}
This is my views
#extends('layouts.main')
#section('container')
<div class="container">
<div class="row mt-5">
#foreach ($shops as $shop)
<div class="card mb-3">
<div class="row g-0">
<div class="col-md-4">
<img src="{{ asset('storage/' . $shop->image) }}" alt="{{ $shop->name_product }}" class="img-fluid rounded-start" style="max-height: 400px; overflow:hidden">
</div>
<div class="col-md-8">
<div class="card-body">
<h5 class="card-title">{{ $shop->name_product }}</h5>
<p>Jenis {{ $shop->category->name_category }}</p>
<p class="card-text fw-bold">Rp.{{ $shop->price }}</p>
</div>
</div>
</div>
</div>
#endforeach
</div>
<div class="row mt-5">
<div class="col align-self-center">
#if (session()->has('success'))
<div class="alert alert-success text-center" role="alert">
{{ session('success') }}
</div>
#endif
</div>
</div>
<form method="POST" action="/shops/order/{{ $shops }}" class="row g-2" enctype="multipart/form-data">
#csrf
<h3 class="text-center mt-2">Detail Delivery</h3>
<div class="col-md-6">
<div class="mb-3">
<select class="form-select" name="user_id" hidden>
#foreach ($users as $name)
<option value="{{ $name->id }}">{{ $name->id }}</option>
#endforeach
</select>
</div>
<div class="mb-3">
<select class="form-select" name="name_produk_id" hidden>
#foreach ($shops as $shops_id)
<option value="{{ $shops_id->id }}">{{ $shops_id->id }}</option>
#endforeach
</select>
</div>
<div class="mb-3">
<label for="address" class="form-label">Address</label>
<textarea class="form-control" #error('address') is-invalid #enderror id="address" name="address" required>
</textarea>
</div>
</div>
<div class="col-md-4">
<label for="size" class="form-label mt-3">Select size :</label>
<select class="form-select" name="size">
<option selected>S</option>
<option value="M">M</option>
<option value="L">L</option>
<option value="XL">XL</option>
</select>
<div class="mt-3">
<label for="no" class="form-label">Nomor Whatsapp</label>
<input class="form-control" #error('no') is-invalid #enderror id="no" name="no" required autofocus">
</div>
<button type="submit" class="btn btn-primary mt-5">Buy Now</button>
</div>
</form>
</div>
#endsection

Related

Laravel Excel import using Maatwebsite Excel package with additional columns from View

I am trying to import data into MySQL from an Excel file. My table has 2 foreign keys project_id and site_id when importing I am selecting these 2 fields from dropdowns in my View. Is there a way I can map these 2 fields to my import collection? Mind you, the 2 fields do not exist in the import file (for integrity reasons) but they do exist in the table.
Collection
namespace App\Imports;
use App\Proposal;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
class ProposalsImport implements ToModel, WithHeadingRow
{
public function model(array $row)
{
return new Proposal([
'building' => $row['building'],
'floor' => $row['floor'],
'area' => $row['area'],
'room' => $row['room'],
'luminaire' => $row['luminaire'],
'actual_qty' => $row['actual_qty'],
'installed_qty' => $row['installed_qty'],
]);
}
}
Controller
public function import()
{
Excel::import(new ProposalsImport, 'proposals.xlsx');
}
View
#extends('projectmanagement/proposals.base')
#section('action-content')
<!-- Main content -->
<section class="content">
<div class="container">
<div class="box">
<div class="box-header">
</div>
<!-- /.box-header -->
<div class="box-body" data-widget="box-refresh">
#if (session('status'))
<div style="padding-top: 0px;padding-bottom: 0px;"
class="alert alert_cust alert-success alert-dismissable fade in">{{ session('status') }}×</div>
#endif
</div>
<div class="row">
<div class="col-lg-12">
<form class="form-horizontal" role="form" method="POST" enctype="multipart/form-data" action="{{ route('proposals.store') }}">
{{ csrf_field() }}
<div class="row">
<div class="col-md-12">
<div class="form-group{{ $errors->has('project_id') ? ' has-error' : '' }}">
<label for="project_id" class="col-md-4 control-label">Project Name</label>
<div class="col-md-6">
<select id="project_id" class="form-control select2" style="width: 100%;"
name="project_id">
<option value="0" disabled selected>Select Project</option>
#foreach ($projects as $project)
<option value="{{$project->id}}">{{ $project->project_name }}</option>
#endforeach
</select>
#if ($errors->has('project_id'))
<span class="help-block">
<strong>{{ $errors->first('project_id') }}</strong>
</span>
#endif
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<br/>
<div class="form-group{{ $errors->has('site_id') ? ' has-error' : '' }}">
<label for="site_id" class="col-md-4 control-label">Site Name</label>
<div class="col-md-6">
<select id="site_id" class="form-control select2" style="width: 100%;"
name="site_id">
<option value="0" disabled selected>Select Site</option>
#foreach ($sites as $site)
<option value="{{$site->id}}">{{ $site->site_name }}</option>
#endforeach
</select>
#if ($errors->has('site_id'))
<span class="help-block">
<strong>{{ $errors->first('site_id') }}</strong>
</span>
#endif
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<br/>
<div class="form-group{{ $errors->has('proposal_file') ? ' has-error' : '' }}">
<label for="proposal_file" class="col-md-4 control-label">Proposal File</label>
<div class="col-md-6">
<input id="proposal_file" type="file" class="form-control" name="proposal_file" required autofocus>
#if ($errors->has('proposal_file'))
<span class="help-block">
<strong>{{ $errors->first('proposal_file') }}</strong>
</span>
#endif
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<br/>
<div class="form-group">
<div class="col-md-4">
</div>
<div class="col-md-6">
<button type="submit" class="btn col-sm-3 col-xs-5 btn-primary">Upload Proposal</button>
</div>
</div>
<br/>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
#push('custom_scripts')
<script>
</script>
#endpush
#endsection
Override import controller's construct method, sending all the parameters you need, like this:
class ProposalsImport implements ToModel, WithHeadingRow
{
protected $project_id;
protected $site_id;
public function __construct($project_id, $site_id)
{
$this->project_id = $project_id;
$this->site_id = $site_id;
}
public function model(array $row)
{
return new Proposal([
'building' => $row['building'],
'project_id' => $this->project_id,
'site_id' => $this->site_id
]);
}
}
And then, call it from your Controller like this:
public function import(Request $request)
{
Excel::import(new ProposalsImport($request->project_id, $request->site_id), 'proposals.xlsx');
}

Livewire & Select2

I am using Livewire and Slect2 in my project
and I am code to select 2 javascript also but select 2 value is not passed to the database here is my code
My Blade File
<div class="col-12 col-lg-12 col-sm-12">
<div wire:ignore>
<div class="form-group">
<label for="exampleInputRounded0">Section Name</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-globe-asia text-primary"></i></span>
</div>
<select class="form-control rounded-0 " wire:model="cr_classes_id" id="cr_classes_id" style="width: 100%;" tabindex="-1" aria-hidden="true">
#foreach ($classes as $class)
<option value="{{$class->id}}">{{$class->classes_name}}</option>
#endforeach
</select>
</div>
#error('cr_classes_sec_id') <span class="text-danger error">{{ $message }}</span>#enderror
</div>
</div>
</div>
Here is My JS
#push('js')
<script type="text/javascript">
$(document).ready(function () {
$('#cr_classes_id').select2();
$('#cr_classes_id').on('change', function (e) {
var data = $('#cr_classes_id').select2("val");
#this.set('cr_classes_id', data);
});
});
</script>
#endpush
Consol Error
Uncaught TypeError: Cannot read property '$wire' of undefined
at Livewire.value (index.js:31)
at HTMLSelectElement.<anonymous> (ClassRooms:844)
at HTMLSelectElement.dispatch (jquery.min.js:2)
at HTMLSelectElement.v.handle (jquery.min.js:2)
at Object.trigger (jquery.min.js:2)
at HTMLSelectElement.<anonymous> (jquery.min.js:2)
at Function.each (jquery.min.js:2)
at k.fn.init.each (jquery.min.js:2)
at k.fn.init.trigger (jquery.min.js:2)
at n.select (select2.min.js:2)
without wire:ignore Working find and data send to the database
Try this
<div class="col-12 col-lg-12 col-sm-12">
<div wire:ignore.self>
<div class="form-group">
<label for="exampleInputRounded0">Section Name</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-globe-asia text-primary"></i></span>
</div>
<select class="form-control rounded-0 " wire:change="$emit('classChanged', $event.target.value)" style="width: 100%;" tabindex="-1" aria-hidden="true">
#foreach ($classes as $class)
<option value="{{$class->id}}" {{ $cr_classes_id ? 'selected' : ''}} >{{$class->classes_name}}</option>
#endforeach
</select>
</div>
#error('cr_classes_sec_id') <span class="text-danger error">{{ $message }}</span>#enderror
</div>
</div>
</div>
and in component
public $cr_classes_id = '';
public $listeners = [
'classChanged'
];
......
public function classChanged($value)
{
$this->cr_classes_id = $value;
}
try to add ":key="cr_classes_id_key" in your blade file
<div class="col-12 col-lg-12 col-sm-12">
<div wire:ignore :key="cr_classes_id_key">
<div class="form-group">
<label for="exampleInputRounded0">Section Name</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fas fa-globe-asia text-primary"></i></span>
</div>
<select class="form-control rounded-0 " wire:model="cr_classes_id" id="cr_classes_id" style="width: 100%;" tabindex="-1" aria-hidden="true">
#foreach ($classes as $class)
<option value="{{$class->id}}">{{$class->classes_name}}</option>
#endforeach
</select>
</div>
#error('cr_classes_sec_id') <span class="text-danger error">{{ $message }}</span>#enderror
</div>
</div>

how to display pdf in embed tag after click button?

I have a form with a filter and a submit button. I would like to display a pdf in an embed tag on the same page with the filter fields used when generating the pdf with Dompdf. Thanks in advance.
here is the form :
#section('content')
<!-- Your html goes here -->
<div class='panel panel-default'>
<div class='panel-heading'>Customer Statement</div>
<div class='panel-body'>
<form method='get' action="{{ route('getStatement') }}" id="statementForm">
#csrf
<div class="row">
<div class='col-sm-6 form-group'>
<div class="form-group col-6">
<label for="start_date"><strong>Start Date</strong></label>
<input type="date" required name="start_date" value="{{ date('Y-m-d') }}"
class="form-control" id="start_date">
</div>
</div>
<div class='col-sm-6 form-group'>
<div class="form-group col-6">
<label for="end_date"><strong>End Date</strong></label>
<input type="date" required name="end_date" value="{{ date('Y-m-d') }}"
class="form-control" id="end_date">
</div>
</div>
</div>
<div class='col-sm-12 form-group'>
<label> Customers :</label>
<select style="font-size: 12px; width:100%" id="customer_id" name="customer_id"
class="select2 mb-2 select2-multiple">
<option value=''> Choose </option>
#if ($customers)
#foreach ($customers as $item)
#php $selected = ''; #endphp
#if (isset($customerdirect) == $item->id)
#php $selected = 'selected'; #endphp
#endif
<option data-name="{{ $item->name }}" value='{{ $item->id }}' {{ $selected }}>
{{ $item->name }} </option>
#endforeach
#endif
</select>
</div>
</div>
<div class='panel-footer'>
<input type='submit' class='btn btn-success' value='Generate'/>
</div>
</form>
</div>
#endsection
the code in controller
public function getStatement(Request $request)
{
$pdf = PDF::loadView('customerslocations::customerstatementpdf');
return $pdf->stream();
}

Add [title] to fillable property to allow mass assignment on [App\Profile]

I am trying to create edit profile, but when I click on edit profile button I'm getting below error:
Illuminate \ Database \ Eloquent \ MassAssignmentException Add [title]
to fillable property to allow mass assignment on [App\Profile]
show.blade.php :
<#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-4">
<img src="https://scontent-cdt1-1.cdninstagram.com/vp/dcca3b442819fc8b9b63f09b2ebde320/5DA9E3CB/t51.2885-19/s150x150/40101184_290824334847414_1758201800999043072_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com" class="rounded-circle">
</div>
<div class="col-8">
<div class="d-flex align-items-baseline">
<div class="h4 mr-3 pt-2">{{ $user->username }}</div>
<button class="btn btn-primary">S'abonner</button>
</div>
<div class="d-flex">
<div class="mr-3">{{ $user->posts->count() }} article(s) en vente
</div>
Modifier Profile
<div class="mt-3">
<div class="font-weight-bold">
{{ $user->profile->title }}
</div>
<div class="font-weight-bold">
{{ $user->profile->description }}
</div>
</div>
</div>
</div>
<div class="row mt-5">
#foreach ($user->posts as $post)
<div class="col-4">
<img src="{{ asset('storage') . '/' . $post->image }}" class="w-100">
</div>
#endforeach
</div>
</div>
#endsection
ProfileController :
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function show(User $user)
{
return view('profile.show', compact('user'));
}
public function edit(User $user)
{
return view('profile.edit', compact('user'));
}
public function update(User $user)
{
$data = request()->validate([
'title' => 'required',
'description' => 'required'
]);
$user->profile->update($data);
return redirect()->route('profile.show', ['user' => $user]);
}
}
edit.blade.php :
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Modifier profile</div>
<div class="card-body">
<form method="POST" action="{{ route('profile.update', ['user' => $user]) }}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="form-group">
<label for="title">Titre</label>
<div class="col-md-6">
<input id="title" type="text" class="form-control #error('title') is-invalid #enderror" name="title" value="{{ old('title') ?? $user->profile->title }}" autocomplete="title" autofocus>
#error('title')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group">
<label for="description">Description</label>
<div class="col-md-6">
<textarea id="description" type="text" class="form-control #error('description') is-invalid #enderror" name="description" autocomplete="description" autofocus>{{ old('description') ?? $user->profile->description }}</textarea>
#error('description')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group">
<div class="custom-file">
<input type="file" name="image" class="custom-file-input #error('image') is-invalid #enderror" id="validatedCustomFile" >
<label class="custom-file-label" for="validatedCustomFile">Choisir une image</label>
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Modifier profile
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
protected $guarder = [];
public function user()
{
return $this->belongsTo('App\User');
}
}
What am I doing wrong here and how can I get rid of this error?
You have a spelling error, instead of $guarder add this in your model:
protected $guarded = [];
I won't advise using empty guarded but use $fillable instead.
In the same controller model, free access to database fields with
protected $guarded = [];
or
protected $fillable = [];

insert input fied array in laravel 5.2

I want to insert to DB with array input. but have an error. Please help me
this is my code:
in view
Số công văn
<div class="form-group">
<label for="exampleInputpwd1">Người phê duyệt</label>
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-thumbs-o-up"></i></div>
<select class="form-control" name="txtleader[]">
<?php
$list_ld = DB::table('leaders')->select('id','Name')->get();
?>
#foreach($list_ld as $item_ld)
<option value="{!! $item_ld->id !!}">{!! $item_ld->Name !!}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Lí do xin vé</label>
<div class="input-group">
<div class="input-group-addon"><i class="icon-question"></i></i></div>
<select class="form-control" name="txtlido[]">
<?php
$list_rs = DB::table('reasons')->select('id','Name')->get();
?>
#foreach($list_rs as $item_rs)
<option value="{!! $item_rs->id !!}">{!! $item_rs->Name !!}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="exampleInputpwd2">Công ty</label>
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-bank"></i></div>
<select class="form-control" name="txtCom[]">
<?php
$list_c = DB::table('companies')->select('id','NameCOM')->get();
?>
#foreach($list_c as $item_c)
<option value="{!! $item_c->id !!}">{!! $item_c->NameCOM !!}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="exampleInputpwd2">Phòng</label>
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-building-o"></i></div>
<select class="form-control" name="txtRoom[]">
<?php
$list_r = DB::table('rooms')->select('id','NameRO')->get();
?>
#foreach($list_r as $item_r)
<option value="{!! $item_r->id !!}">{!! $item_r->NameRO !!}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="exampleInputpwd2">Giao cho</label>
<div class="input-group">
<div class="input-group-addon"><i class="ti-share"></i></div>
<input type="text" class="form-control" id="exampleInputuname" placeholder="Chi nhánh/Đại diện nhận nhiệm vụ" name="txtReceiver[]">
</div>
</div>
<div class="form-group">
<div class="checkbox checkbox-success"></div>
</div>
<div class="text-right"></div>
</div>
<div class="col-md-6">
<div class="form-group nguoixin">
<label for="exampleInputuname">Người xin</label>
<div class="input-group">
<div class="input-group-addon"><i class="icon-user"></i></div>
<input type="text" class="form-control" id="exampleInputuname" placeholder="Người xin" name="txtnguoixin[]">
<input type="text" class="form-control" id="exampleInputuname" placeholder="Người xin" name="txtnguoixin[]">
<div class="input-group-addon"><button class="add-more-nx"><i class="fa fa-plus"></i></button></div>
</div>
</div>
<div class="form-group">
<label for="exampleInputuname">Mối quan hệ</label>
<div class="input-group">
<div class="input-group-addon"><i class="ti-exchange-vertical"></i></div>
<select class="form-control" name="txtRelation[]">
<?php
$list_rl = DB::table('relationship')->select('id','Name')->get();
?>
#foreach($list_rl as $item_rl)
<option value="{!! $item_rl->id !!}">{!! $item_rl->Name !!}</option>
#endforeach
</select>
<div class="input-group-addon"><button><i class="fa fa-plus"></i></button></div>
</div>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Người đi</label>
<div class="input-group">
<div class="input-group-addon"><i class="icon-people"></i></div>
<textarea class="form-control" rows="5" name="txtGuest[]"></textarea>
<div class="input-group-addon"><button><i class="fa fa-plus"></i></button></div>
</div>
</div>
<div class="form-group">
<label for="exampleInputpwd1">chặng bay</label>
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-fighter-jet"></i></div>
<select class="form-control" name="txtFlies[]">
<?php
$list_cb = DB::table('return')->select('id','CodeFN')->get();
?>
#foreach($list_cb as $item_cb)
<option value="{!! $item_cb->id !!}">{!! $item_cb->CodeFN !!}</option>
#endforeach
</select>
<div class="input-group-addon"><button><i class="fa fa-plus"></i></button></div>
</div>
</div>
<div class="form-group">
<label for="exampleInputpwd2">mức giảm</label>
<div class="input-group">
<div class="input-group-addon"><i class="ti-package"></i></div>
<select class="form-control" name="txtDiscount[]">
<?php
$list_dc = DB::table('discounts')->select('id','CodeDist')->get();
?>
#foreach($list_dc as $item_dc)
<option value="{!! $item_dc->id !!}">{!! $item_dc->CodeDist !!}</option>
#endforeach
</select>
<div class="input-group-addon"><button><i class="fa fa-plus"></i></button></div>
</div>
</div>
<div class="form-group">
<div class="checkbox checkbox-success"> </div>
</div>
<div class="text-right">
<button type="submit" class="btn btn-success waves-effect waves-light m-r-10">Thêm mới</button>
</div>
</form>
my controller
$input = Input::all();
$count = count($input['txtnguoixin']);
$ticketin = array();
for($i=1; $i<$count; $i++){
if($input['txtnguoixin'][$i]){
array_push($ticketin, array(
'NoCV' => $input['txtSCV'][$i],
'nguoixin' => $input['txtnguoixin'][$i],
'Com_id' => $input['txtCom'][$i],
'Room_id' => $input['txtRoom'][$i],
'Reason_id' => $input['txtlido'][$i],
'Nguoidi' => $input['txtGuest'][$i],
'Relation_id' => $input['txtRelation'][$i],
'return_id' => $input['txtFlies'][$i],
'discount_id' => $input['txtDiscount'][$i],
'leader_id' => $input['txtleader'][$i],
'nguoinhap' => 'muoi',
'assignto' => $input['txtReceiver'][$i],
'tinhtrang' => 1
));
}
}
echo "<pre>";
print_r($ticketin);
echo "</pre>";
But it has erro undefined offset 1.

Resources