Hi i have a card that contains fields such as task details and estimated hours
When i try to write checklist name and click on create to store it in the card i am not able to do that
view.blade.php
<div class="checklist" id="checklist">
<form id="form-checklist" class="collapse pb-2" data-action="{{route('checklist.store',[$task->project_id,$task->id])}}">
<div class="card border shadow-none">
<div class="px-3 py-2 row align-items-center">
<div class="col-10">
<input type="text" name="name" required class="form-control" placeholder="{{__('Checklist Name')}}"/>
</div>
</div>
</div>
</form>
#foreach($task->checklist as $checklist)
<div class="card border shadow-none checklist-member">
<div class="px-3 py-2 row align-items-center">
<div class="col">
<div class="form-check form-check-inline">
<input type="checkbox" class="form-check-input" id="check-item-{{ $checklist->id }}" #if($checklist->status) checked #endif data-url="{{route('checklist.update',[$task->project_id,$checklist->id])}}">
<label class="form-check-label h6 text-sm" for="check-item-{{ $checklist->id }}">{{ $checklist->name }}</label>
</div>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
project.task.controller
public function checklistStore(Request $request, $projectID, $taskID)
{
if(\Auth::user()->can('view project task'))
{
$request->validate(
['name' => 'required']
);
$post = [];
$post['name'] = $request->name;
$post['task_id'] = $taskID;
$post['user_type'] = 'User';
$post['created_by'] = \Auth::user()->id;
$post['status'] = 0;
$checkList = TaskChecklist::create($post);
$user = $checkList->user;
}
}
Related
i have created the admin panel where im saving every data, the main thing is the user want to add or edit data from admin panel for his site. its like a blog.
first i have done the slider section where im fetching data from admin panel to client site.
this is the slider section code which is working perfectly.
Add file
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="">ARABIC SECTION</h1>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<hr>
<div class="col-md-12 float-left">
<div class="card">
<div class="card-header">
<h3 class="card-title text-lg float-left col-md-12" >
Manage Home Section
<a class="float-right btn btn-danger" href="{{ URL('/admin/post/list')}}">Back</a>
</h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="tab-content p-0">
<form action="{{ url('/arabicpost/submit')}}" method="POST" enctype="multipart/form-data">
#csrf
<div class="card-body">
<div class="form-group">
<label for="">Title 1</label>
<input type="text" name="title" class="form-control" id="exampleInputTitle" placeholder="Enter Title 1">
#error('title')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="">Description 1</label>
<textarea class="form-control" name="description" id="" cols="30" rows="10" placeholder="Description 1"></textarea>
#error('description')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="exampleInputFile">File input 1</label>
<div class="input-group">
<div class="custom-file">
<input type="file" name="image" class="custom-file-input" id="exampleInputFile">
<label class="custom-file-label" for="exampleInputFile">Choose file</label>
</div>
<div class="input-group-append">
<span class="input-group-text">Upload</span>
</div>
</div>
#error('image')
<span>{{ $message }}</span>
#enderror
</div>
</div>
<div class="card-header">
<h3 class="card-title text-lg float-left col-md-12">
Second Banner
</h3>
</div>
<div class="card-body">
<div class="form-group">
<label for="">Title 2</label>
<input type="text" name="title2" class="form-control" id="exampleInputTitle" placeholder="Enter Title 2">
#error('title2')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="">Description 2</label>
<textarea class="form-control" name="description2" id="" cols="30" rows="10" placeholder="Description 2"></textarea>
#error('description2')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="exampleInputFile">File input 2</label>
<div class="input-group">
<div class="custom-file">
<input type="file" name="image2" class="custom-file-input" id="exampleInputFile">
<label class="custom-file-label" for="exampleInputFile">Choose file</label>
</div>
<div class="input-group-append">
<span class="input-group-text">Upload</span>
</div>
</div>
#error('image2')
<span>{{ $message }}</span>
#enderror
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div><!-- /.card-body -->
</div></div></div></div>
#endsection
List File
#extends('admin.layouts.app')
#section('main-content')
<div class="content-wrapper">
<div class="card" style="margin-top:5%">
<div class="card-header">
<h2 class="text-center">English Home Section</h2>
<div class="col-sm-12" style="text-align: center; color:green; font-size:20px">{{session('msg')}}</div>
<div class="col-sm-12" style="text-align: center; color:red; font-size:20px">{{session('msgForDelete')}}</div>
</div>
<div class="card-header">
<a class="btn btn-success" href="{{ URL('/admin/post/add')}}">Add Post</a>
</div>
<!-- /.card-header -->
<div class="card-body">
<table id="example1" class="table table-bordered table-striped table-responsive">
<thead>
<tr width="100%">
<th width="3%">ID</th>
<th width="10%">Title 1</th>
<th width="23.5%">Description 1</th>
<th width="10%">Title 2</th>
<th width="23.5%">Description 2</th>
<th width="10%">Image 1</th>
<th width="10%">Image 2</th>
<th width="10%">Action</th>
</tr>
</thead>
<tbody>
<?php
// echo '<pre>';
// print_r([$result]);
// die();
?>
#foreach ($result as $list)
<tr>
<td>{{$list->id}}</td>
<td>{{$list->title}}</td>
<td>{{$list->description}}</td>
<td>{{$list->title2}}</td>
<td>{{$list->description2}}</td>
<td><img src="{{ asset('storage/app/public/post/'.$list->image) }}" width="150px"/></td> <td><img src="{{ asset('storage/app/public/post/secondbanner/'.$list->image2) }}" width="150px"/></td>
<td><a class="btn btn-primary" href="{{('/haffiz/admin/post/edit/'.$list->id)}}">Edit</a>
<a class="btn btn-danger" href="{{('/haffiz/admin/post/delete/'.$list->id)}}">Delete</a>
</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th>ID</th>
<th>Title 1</th>
<th>Description 1</th>
<th>Title 2</th>
<th>Description 2</th>
<th>Image 1</th>
<th>Image 2</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</div></div></div> </div>
#endsection
edit file
#extends('admin.layouts.app')
#section('main-content')
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="">ENGLISH SECTION</h1>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<hr>
<div class="col-md-12 float-left">
<div class="card">
<div class="card-header">
<h3 class="card-title text-lg float-left col-md-12" >
Manage Home Section
<a class="float-right btn btn-danger" href="{{ URL('/admin/post/list')}}">Back</a>
</h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="tab-content p-0">
<form action="{{ url('/admin/post/update/'.$result['0']->id)}}" method="POST" enctype="multipart/form-data">
#csrf
<div class="card-body">
<div class="form-group">
<label for="">Title</label>
<input type="text" name="title" class="form-control" id="exampleInputTitle" value="{{$result['0']->title}}" placeholder="Enter Title">
#error('title')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="">Description</label>
<textarea class="form-control" name="description" id="" cols="30" rows="10" placeholder="Description">{{$result['0']->description}} </textarea>
#error('description')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<div class="input-group">
<div class="custom-file">
<input type="file" name="image" class="custom-file-input" id="exampleInputFile">
<label class="custom-file-label" for="exampleInputFile">Choose file</label>
</div>
<div class="input-group-append">
<span class="input-group-text">Upload</span>
</div>
</div>
#error('image')
<span>{{ $message }}</span>
#enderror
</div>
</div>
<!-- /.card-body -->
<div class="card-body">
<div class="form-group">
<label for="">Title 2</label>
<input type="text" name="title2" class="form-control" id="exampleInputTitle" value="{{$result['0']->title2}}" placeholder="Enter Title">
#error('title2')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="">Description 2</label>
<textarea class="form-control" name="description2" id="" cols="30" rows="10" placeholder="Description">{{$result['0']->description2}} </textarea>
#error('description2')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="exampleInputFile">File input 2</label>
<div class="input-group">
<div class="custom-file">
<input type="file" name="image2" class="custom-file-input" id="exampleInputFile">
<label class="custom-file-label" for="exampleInputFile">Choose file</label>
</div>
<div class="input-group-append">
<span class="input-group-text">Upload</span>
</div>
</div>
#error('image2')
<span>{{ $message }}</span>
#enderror
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div><!-- /.card-body -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="">ENGLISH SECTION</h1>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<hr>
<div class="col-md-12 float-left">
<div class="card">
<div class="card-header">
<h3 class="card-title text-lg float-left col-md-12" >
Manage Home Section
<a class="float-right btn btn-danger" href="{{ URL('/admin/post/list')}}">Back</a>
</h3>
</div>
</div>
</div>
</div>
</div>
#endsection
its Controller(Post)
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class Post extends Controller
{
function listing()
{
$data['result'] = DB::table('posts')->orderBy('id','desc')->get();
return view('admin.post.list',$data);
}
function submit(Request $req)
{
//validation
$req->validate([
'title' => 'required',
'description' => 'required',
'title2' => 'required',
'description2' => 'required',
'image' => 'mimes: jpg,jpeg,png',
'image2' => 'mimes: jpg,jpeg,png'
]);
//storing image
$image=$req->file('image');
$ext = $image->extension();
$file=time().'.'.$ext;
$image->storeAs('public/post',$file);
$image2=$req->file('image2');
$ext2 = $image2->extension();
$file2=time().'.'.$ext2;
$image2->storeAs('public/post/secondbanner',$file2);
//array
$data = array(
'title' => $req->input('title'),
'description' => $req->input('description'),
'title2' => $req->input('title2'),
'description2' => $req->input('description2'),
'image' => $file,
'image2' => $file2,
);
//inserting data
DB::table('posts')->insert($data);
$req->session()->flash('msg','Data has been Added');
return redirect('/admin/post/list');
}
function delete(Request $req , $id)
{
DB::table('posts')->where('id',$id)->delete();
$req->session()->flash('msgForDelete','Data has been Deleted');
return redirect('/admin/post/list');
}
function edit(Request $req , $id)
{
$data['result'] = DB::table('posts')->where('id',$id)->get();
return view('admin.post.edit',$data);
}
function update(Request $req , $id)
{
//validation
$req->validate([
'title' => 'required',
'description' => 'required',
'title2' => 'required',
'description2' => 'required',
'image' => 'mimes: jpg,jpeg,png',
'image2' => 'mimes: jpg,jpeg,png'
]);
//array
$data = array(
'title' => $req->input('title'),
'description' => $req->input('description'),
'title2' => $req->input('title2'),
'description2' => $req->input('description2'),
);
if($req->hasfile('image'))
{
$image=$req->file('image');
$ext = $image->extension();
$file=time().'.'.$ext;
$file2=time().'.'.$ext;
$image->storeAs('public/post/',$file,$file2);
$data['image']=$file;
}
if($req->hasfile('image2'))
{
$image2=$req->file('image2');
$ext = $image2->extension();
$file2=time().'.'.$ext;
$image2->storeAs('public/post/secondbanner',$file2);
$data['image2']=$file2;
}
//updating data
DB::table('posts')->where('id',$id)->update($data);
$req->session()->flash('msg','Data has been Updated');
return redirect('/admin/post/list');
}
}
and this is a controller where im sending data to client site.
<?php
namespace App\Http\Controllers\user;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
class EngHafizController extends Controller
{
public function login()
{
return view('user.english.login');
}
public function registration()
{
return view('user.english.registration');
}
public function homefront()
{
return view('user.english.index');
}
public function home()
{
$data['result'] = DB::table('posts')->get();
return view('user.english.index',$data);
}
public function about()
{
$data['aboutresult'] = DB::table('abouts')->get();
return view('user.english.about',$data);
}
public function whyhaffez()
{
return view('user.english.whyhaffez');
}
public function oursheikh()
{
return view('user.english.oursheikh');
}
public function contact()
{
return view('user.english.contact');
}
}
This is all working properly.
lets get to the point. when try to do the same for ABOUT section
it give me the error which is
(Undefined variable: aboutresult (View:C:\xampp\htdocs\haffiz\resources\views\user\english\index.blade.php))
i do the same thing for about section
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class AboutController extends Controller
{
function about_listing()
{
$data['aboutresult'] = DB::table('abouts')->orderBy('id','desc')->get();
return view('admin.post.about.aboutlist',$data);
}
function about_submit(Request $request)
{
//validation
$request->validate([
'title3' => 'required',
'heading3' => 'required',
'description3' => 'required',
'image3' => 'mimes: jpg,jpeg,png'
]);
//storing image
$image3=$request->file('image3');
$ext = $image3->extension();
$file=time().'.'.$ext;
$image3->storeAs('public/post/about_image',$file);
//array
$data = array(
'title3' => $request->input('title3'),
'heading3' => $request->input('heading3'),
'description3' => $request->input('description3'),
'image3' => $file,
);
//inserting data
DB::table('abouts')->insert($data);
$request->session()->flash('msg','Data has been Added');
return redirect('/admin/post/about/aboutlist');
}
function about_delete(Request $request , $id)
{
DB::table('abouts')->where('id',$id)->delete();
$request->session()->flash('msgForDelete','Data has been Deleted');
return redirect('/admin/post/list');
}
function about_edit(Request $request , $id)
{
$data['aboutresult'] = DB::table('abouts')->where('id',$id)->get();
return view('admin.post.about.aboutedit',$data);
}
function about_update(Request $request , $id)
{
//validation
$request->validate([
'title3' => 'required',
'heading3' => 'required',
'description3' => 'required',
'image3' => 'mimes: jpg,jpeg,png'
]);
//array
$data = array(
'title3' => $request->input('title3'),
'heading3' => $request->input('heading3'),
'description3' => $request->input('description3'),
);
if($request->hasfile('image3'))
{
$image3=$request->file('image3');
$ext = $image3->extension();
$file=time().'.'.$ext;
$image3->storeAs('public/post/about_image',$file);
$data['image3']=$file;
}
//updating data
DB::table('abouts')->where('id',$id)->update($data);
$request->session()->flash('msg','Data has been Updated');
return redirect('/admin/post/about/aboutlist');
}
}
aboutLIST
#extends('admin.layouts.app')
#section('main-content')
<div class="content-wrapper">
<div class="card">
<div class="card-header">
<h2 >About Section</h2>
<div class="col-sm-12" style="text-align: center; color:green; font-size:20px">{{session('msg')}}</div>
<div class="col-sm-12" style="text-align: center; color:red; font-size:20px">{{session('msgForDelete')}}</div>
</div>
<div class="card-header">
<a class="btn btn-success" href="{{ URL('/admin/post/about/about')}}">Add Post</a>
</div>
<!-- /.card-header -->
<div class="card-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr width="100%">
<th width="">ID</th>
<th width="10">Title </th>
<th width="40">Heading</th>
<th width="15">Description </th>
<th width="10">Image </th>
<th width="25%">Action</th>
</tr>
</thead>
<tbody>
<?php
// echo '<pre>';
// print_r([$aboutresult]);
// die();
?>
#foreach ($aboutresult as $aboutlist)
<tr>
<td>{{$aboutlist->id}}</td>
<td>{{$aboutlist->title3}}</td>
<td>{{$aboutlist->heading3}}</td>
<td>{{$aboutlist->description3}}</td>
<td><img src="{{ asset('storage/app/public/post/about_image/'.$aboutlist->image3) }}" width="150px" height="100px"/></td>
<td>
<a class="btn btn-primary" href="{{('/haffiz/admin/post/about/aboutedit/'.$aboutlist->id)}}">Edit</a>
<a class="btn btn-danger" href="{{('/haffiz/admin/post/delete'.$aboutlist->id)}}" >Delete</a>
</td>
</tr>
#endforeach
</tbody>
<tfoot>
<tr>
<th width="4">ID</th>
<th width="10">Title </th>
<th width="40">Heading</th>
<th width="15">Description </th>
<th width="10">Image</th>
<th width="25%">Action</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.card-body -->
</div>
</div>
</div>
#endsection
aboutedit
#extends('admin.layouts.app')
#section('main-content')
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1 class="">ENGLISH SECTION</h1>
</div>
</div>
</div><!-- /.container-fluid -->
</section>
<hr>
<div class="col-md-12 float-left">
<div class="card">
<div class="card-header">
<h3 class="card-title text-lg float-left col-md-12" >
Edit About Section
<a class="float-right btn btn-danger" href="{{ URL('/admin/post/about/aboutlist')}}">Back</a>
</h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="tab-content p-0">
<form action="{{ url('/admin/post/about/update/'.$aboutresult['0']->id)}}" method="POST" enctype="multipart/form-data">
#csrf
<div class="card-body">
<div class="form-group">
<label for="">Title</label>
<input type="text" name="title3" class="form-control" id="exampleInputTitle" value="{{$aboutresult['0']->title3}}" placeholder="Enter Title">
#error('title3')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="">Heading</label>
<input class="form-control" name="heading3" id="" placeholder="Heading" value="{{$aboutresult['0']->heading3}}">
#error('heading3')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="">Description </label>
<textarea class="form-control" name="description3" id="" cols="30" rows="10" placeholder="Description ">{{$aboutresult['0']->description3}}</textarea>
#error('description3')
<span>{{ $message }}</span>
#enderror
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<div class="input-group">
<div class="custom-file">
<input type="file" name="image3" class="custom-file-input" id="exampleInputFile">
<label class="custom-file-label" for="exampleInputFile">Choose file</label>
</div>
<div class="input-group-append">
<span class="input-group-text">Upload</span>
</div>
</div>
#error('image3')
<span>{{ $message }}</span>
#enderror
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div><!-- /.card-body -->
</div>
</div>
</div>
</div>
#endsection
this the index file where im fetching
#foreach ($result as $list)
<img src="{{ asset('storage/app/public/post/'.$list->image) }}" class="d-block w-100" alt="...">
<div class="col-12 text-left">
<h1 class="animated slideInDown">{{ $list->title }}</h1>
<svg class="animated slideInDown" width="128" height="9" viewBox="0 0 128 9" fill="none" xmlns="http://www.w3.org/2000/svg">
</svg>
<p class="animated slideInLeft">{{ $list->description }}</p>
Read More
</div>
<div class="carousel-item">
<img src="{{ asset('storage/app/public/post/secondbanner/'.$list->image2) }}" class="d-block w-100" alt="...">
<h1 class="animated slideInDown">{{ $list->title2}}</h1>
<p class="animated slideInLeft">{{ $list->description2 }}</p>
#endforeach
</div>
about section
#foreach($aboutresult as $aboutlist)
<div class="col-xl-7 about-p">
<h5 class="about-welcome">{{$aboutlist->title3}}</h5>
#endforeach
public function home()
{
$data['aboutresult'] = DB::table('abouts')->get();
$data['result'] = DB::table('posts')->get();
return view('user.english.index',$data);
}
Hi, I would like some help on this.
This loop comes from my products table, using pure laravel I load each product in inputs, so if I make any changes and click on an update button it is redirected to a route to update the changed product.
How could I do this same idea using livewire?
How to display the updated data in this loop without having to go to another route.
My blade:
#forelse($categoryProduct->products as $key => $product)
<li>
<div class="mt-3 card">
<div class="card-body">
<div class="row">
<div class="col-1">
<x-inputs.group class="">
<div
x-data="imageViewer('{{ $product->img ? \Storage::url($product->img) : '' }}')">
<x-inputs.partials.label name="img" label="Foto">
</x-inputs.partials.label><br />
<!-- Show the image -->
<template x-if="imageUrl">
<img :src="imageUrl"
class="object-cover border border-gray-200 rounded"
style="width: 100px; height: 100px;" />
</template>
<!-- Show the gray box when image is not available -->
<template x-if="!imageUrl">
<div class="bg-gray-100 border border-gray-200 rounded"
style="width: 140px; height: 140px;"></div>
</template>
<div class="mt-2">
<input wire:model="img" type="file" name="img" id="img"
#change="fileChosen" />
</div>
#error('img')
#include('components.inputs.partials.error')
#enderror
</div>
</x-inputs.group>
</div>
<div class="col-11">
<div class="row">
<div class="col-10">
<input type="text" class="form-control" placeholder="Name"
value="{{ $product->name }}">
</div>
<div class="col-2">
<button
onclick="confirm('Tem certeza que deseja deletar esse produto?') || event.stopImmediatePropagation()"
wire:click.prevent="deleteProduct({{ $product->id }})"
class="btn btn-user btn-danger">
×
</button>
</div>
<div class="mt-3 col-10">
<textarea class="form-control"
placeholder="Description">{{ $product->description }}</textarea>
</div>
<div class="mt-3 col-2">
<input type="number" class="form-control"
placeholder="Price" value="{{ $product->price }}">
</div>
</div>
</div>
</div>
</div>
</li>
#empty
This is the way I'm feeding the loop:
public function render()
{
$this->shop = Shop::with([
'categoryProducts',
'categoryProducts.products',
'districts'
])->where('user_id', Auth::user()->id)->first();
return view('app.menu-controller')
->extends('layouts.app')
->section('content');
}
You can create a Livewire ProductComponent and in blade have the div card once this loop the same elements
https://www.laravel-livewire.com/docs/2.x/making-components
https://www.laravel-livewire.com/docs/2.x/rendering-components
<div>
<div class="mt-3 card">
<div class="card-body">
<div class="row">
<div class="col-1">
<x-inputs.group class="">
<div
x-data="imageViewer('{{ $product->img ? \Storage::url($product->img) : '' }}')">
<x-inputs.partials.label name="img" label="Foto">
</x-inputs.partials.label><br />
//......................
</div>
</div>
Use model binding in nested component for the loop
https://www.laravel-livewire.com/docs/2.x/nesting-components
#forelse($categoryProduct->products as $key => $product)
<li>
#livewire('product-component',['product' => $product], key($user->id))
</li>
and in component
public Product $product;
public function render()
{
return view('livewire.product-component')
->extends('layouts.app')
->section('content');
}
The rest in in Livewire documentation, when you get on there could come here with issues or questions. Greetings
I have a form with image field, inserting all the field with image works fine
but when editing the form->to update a new image I used unlink function to remove the previous image and update with new one. The problem is if I don't upload a new image and submit the form it isn't working.
But I want to keep the current image remain if new image is not uploaded. I have tried many ways even not using unlink function but couldn't reach any solution. please help me, I really need this solution. Thanks in advance
here is my update function in controller
public function Update(Request $request, $id){
$PreviousPic = $request->Prev_pic;
$data = array();
$data['student_name'] = $request->student_name;
$data['matric_no'] = $request->matric_no;
$data['programme_name'] = $request->programme_name;
$data['faculty_name'] = $request->faculty_name;
$data['admission_year'] = $request->admission_year;
$data['contact_no'] = $request->contact_no;
$image = $request->file('pro_pic');
if ($image){
unlink($PreviousPic);
$image_name = date('dmy_H_s_i');
$ext = strtolower($image->getClientOriginalExtension());
$imageFullName = $image_name.'.'.$ext;
$uploadPath = 'media/';
$imageURL = $uploadPath.$imageFullName;
$success = $image->move($uploadPath,$imageFullName);
$data['pro_pic'] = $imageURL;
$Stdata = DB::table('students')->where('id', $id)->update($data);
return redirect()->route('student.index')
->with('success','Updated! The Student Data Updated Successfully');
}
}
here is my edit form image field
<div class="form-group">
<label class="col-md-4 control-label" >Image</label>
<div class="col-md-5 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-picture"></i></span>
<div class="upload-btn-wrapper">
<button class="btn">Upload a New Image</button>
<input type="file" name="pro_pic" />
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Current Image </label>
<div class="col-md-5 inputGroupContainer">
<img src="{{ URL::to($StudentData->pro_pic)}}" height="150px" width="190px">
<input type="hidden" name="Prev_pic" value="{{$StudentData->pro_pic}}">
</div>
</div>
Using this I can upload new image removing the old picture. But If I keep the Upload a New Image field empty and submit the form, the form isn't submitted.
So, I want if I upload new image it works as now and if I don't upload a new image, the current image will remain & submit the form.
# I am using Laravel 7
I have found the answer as expected
public function Update(Request $request, $id){
$PreviousPic = $request->Prev_pic;
$data = array();
$data['student_name'] = $request->student_name;
$data['matric_no'] = $request->matric_no;
$data['programme_name'] = $request->programme_name;
$data['faculty_name'] = $request->faculty_name;
$data['admission_year'] = $request->admission_year;
$data['contact_no'] = $request->contact_no;
$image = $request->file('pro_pic');
if ($image != null){
unlink($PreviousPic);
$image_name = date('dmy_H_s_i');
$ext = strtolower($image->getClientOriginalExtension());
$imageFullName = $image_name.'.'.$ext;
$uploadPath = 'media/';
$imageURL = $uploadPath.$imageFullName;
$success = $image->move($uploadPath,$imageFullName);
$data['pro_pic'] = $imageURL;
$Stdata = DB::table('students')->where('id', $id)->update($data);
return redirect()->route('student.index')
->with('success','Updated! The Student Data Updated Successfully');
}else{
$Stdata = DB::table('students')->where('id', $id)->update($data);
return redirect()->route('student.index')
->with('success','Updated! The Student Data Updated Successfully');
}
}
Just use if else statement to find the solution and it's working great.
You need to pass the exact path of the old image from database.
You can try it with concatenation,
By making two separate columns for image_name and image_path
This is my edit image code
<div id="image" class="row" style="display: none;">
<div class="col-md-12">
<label for="text">Project Image</label>
<div class="form-group">
<input type="file" class="form-control" name="project_image"
value="{{ old('project_image',$currentProject->project_image) }}">
#if ($errors->has('project_image'))
<div class="text-danger">{{ $errors->first('project_image') }}</div>
#endif
</div>
</div>
</div>
This is the div to view the previous and new image
<div class="box box-primary">
<div class="box-body">
<div class="box-header text-center">
<h3 class="box-title">Project Cover Image</h3>
</div>
<div class="row">
<div class="col-md-12 text-center">
<img class="img-responsive" src="{{ URL::asset($currentProject->project_cover_image_path.'/'.$currentProject->project_cover_image_name)}}" alt="Photo">
<br>
<a class="btn btn-primary" data-toggle="modal" data-target="#modal-project-cover">
Change Cover Image
</a>
</div>
</div>
</div>
</div>
This is the modal that actually edits the image
{{-- Modal dialogue to edit Project Cover Image --}}
<div class="modal fade" id="modal-project-cover">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header text-center">
<form method="POST" action="{{url('updateprojectcover', $currentProject->project_id)}}" enctype="multipart/form-data">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
#if (session('error'))
<div class="alert alert-danger">{{ session('error') }}</div>
#endif
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×
</span>
</button>
<div class="row">
<div class="col-md-12">
<h4 for="text">Project Cover Image</h4>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group text-center">
<input type="file" class="form-control" name="project_cover_image">
#if ($errors->has('project_cover_image'))
<div class="text-danger">{{ $errors->first('project_cover_image') }}
</div>
#endif
</div>
</div>
</div>
<div class="modal-footer text-center">
<button type="submit" class="btn btn-primary text-center">Yes</button>
</div>
</form>
</div>
</div>
</div>
</div>
This is my controller to edit image
try
{
$cover_name = $request->file('project_cover_image');
if (isset($cover_name))
{
$project_cover_image_name = $cover_name->getClientOriginalName();
$project_cover_image_name = str_replace(" ", "_", time() . $project_cover_image_name);
$cover_name->move(ImageUrlController::$project_cover_image_path, $project_cover_image_name);
}
Project::where('project_id', $project_id)
->update([
'project_cover_image_path' => ImageUrlController::$project_cover_image_path,
'project_cover_image_name' => $project_cover_image_name,
'updated_at' => Carbon::now('PKT'),
]);
return redirect()->back()->withInput();
}
catch (\Exception $exception)
{
return back()->withError($exception->getMessage())->withInput();
}
I have two table with names courses and episodes in my Database
I want to send a course and episode in database
course_id is foreign key in episode table
i create form with 2 episode and when i'm trying to send it did not work and just send a course not episode
here my controller method :
public function SendToDB(Request $request)
{
$new_Course = new Course($this->Validatepost());
$new_Course->user_id = 1;
$new_Course->image = 'Pics/' . $this->UploadPic(request());
$new_Course->save();
$new_Course->tag()->attach(request('tag'));
$courseID = $new_Course->id;
$episode_number = request()->input('episodes_number');
for ($x = 0; $x < $episode_number; $x++) {
$new_Episode = new Episode($this->validateepisode($episode_number));
$new_Episode->course_id = $courseID;
$new_Episode->save();
return redirect('/Admin/');
}
}
and validate methods
private function Validatepost()
{
return request()->validate([
'title' => 'required',
'body' => 'required',
'price' => 'required'
]);
}
private function validateepisode($id)
{
return request()->validate([
'Episode_Title_' . $id => 'required',
'Episode_Body_' . $id => 'required',
]);
}
and my front
#extends('Admin.layout')
#section('header')
<script src="{{ asset('js/app.js') }}"></script>
<script type="text/javascript" src="/tinymce/tinymce.min.js"></script>
<script>
tinymce.init({
selector: '#mytextarea',
skin: 'oxide-dark',
content_css: 'dark'
});
function next() {
document.getElementById("base-tab-2").className = "nav-link";
document.getElementById('ProgressBar').style.width = "50%"
$('#base-tab-2').trigger('click')
}
function prev() {
document.getElementById('ProgressBar').style.width = "25%"
$('#base-tab-1').trigger('click')
}
function finish() {
document.getElementById("base-tab-3").className = "nav-link";
document.getElementById('ProgressBar').style.width = "75%";
var courseName = document.getElementById('title').value;
var courseprice = document.getElementById('price').value;
var episodeNumer = document.getElementById('episodes_number').value;
document.getElementById('courseName').innerText = courseName;
document.getElementById('coursePrice').innerText = courseprice;
document.getElementById('courseNumber').innerText = episodeNumer;
$('#base-tab-3').trigger('click')
}
function addepisode(number) {
for (var i = 0; i < number; i++) {
//////////////////////////////////////////
var element = document.createElement("input");
var label = document.createElement("Label");
label.innerHTML = "تیتر";
element.setAttribute("type", "text");
element.setAttribute("name", "Episode_Title_" + i);
element.setAttribute("class", "form-control");
label.setAttribute("class", "col-lg-3 form-control-label ");
var div = document.getElementById("test");
div.appendChild(label);
div.appendChild(element);
/////////////////////////////////
var body = document.createElement("textarea");
var bodylabel = document.createElement("Label");
bodylabel.innerHTML = "بدنه";
body.setAttribute("id", "mytextarea");
body.setAttribute("name", "Episode_Body_" + i);
body.setAttribute("class", "form-control");
bodylabel.setAttribute("class", "col-lg-3 form-control-label ");
var div = document.getElementById("test");
div.appendChild(bodylabel);
div.appendChild(body);
}
next();
}
</script>
#endsection
#section('content')
<div class="container-fluid">
<!-- Begin Page Header-->
<div class="row">
<div class="page-header">
<div class="d-flex align-items-center">
<h2 class="page-header-title">افزودن</h2>
</div>
</div>
</div>
<!-- End Page Header -->
<div class="col-xl-12">
<!-- Basic Tabs -->
<div class="widget has-shadow">
<div class="widget-header bordered no-actions d-flex align-items-center">
<h4>افزودن</h4>
</div>
<div class="widget-body sliding-tabs">
<div class="progress progress-lg mb-3">
<div class="progress-bar bg-info" id="ProgressBar" role="progressbar" style="width: 25%"
aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">روند پیشرفت
</div>
</div>
<ul class="nav nav-tabs" id="example-one" role="tablist">
<li class="nav-item">
<a class="nav-link active" id="base-tab-1" data-toggle="tab" href="#tab-1"
onclick="document.getElementById('ProgressBar').style.width = '25%'" role="tab"
aria-controls="tab-1" aria-selected="true">مشخصات دوره</a>
</li>
<li class="nav-item ">
<a class="nav-link disabled" id="base-tab-2" data-toggle="tab" href="#tab-2"
onclick="document.getElementById('ProgressBar').style.width = '50%'" role="tab"
aria-controls="tab-2" aria-selected="false">مشخصات قسمت ها</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" id="base-tab-3" data-toggle="tab" href="#tab-3"
onclick="document.getElementById('ProgressBar').style.width = '75%'" role="tab"
aria-controls="tab-3" aria-selected="false">بازبینی</a>
</li>
</ul>
<form class="form-horizontal" method="post" action="/Admin/Createnew" enctype="multipart/form-data">
#csrf
<div class="tab-content pt-3">
<div class="tab-pane fade show active" id="tab-1" role="tabpanel"
aria-labelledby="base-tab-1">
<div class="widget-body">
<div class="form-group row d-flex align-items-center mb-5 ">
<label
class="col-lg-3 form-control-label #error('title') text-danger #enderror">تیتر</label>
<div class="col-lg-9">
<input type="text" id="title" name="title" value="{{old('title')}}"
class="form-control #error('title') border border-danger #enderror">
</div>
</div>
<div class="form-group row d-flex align-items-center mb-5 ">
<label
class="col-lg-3 form-control-label #error('price') text-danger #enderror">قیمت</label>
<div class="col-lg-9">
<input type="number" id="price" name="price" value="{{old('price')}}"
class="form-control #error('price') border border-danger #enderror">
</div>
</div>
<div class="form-group row d-flex align-items-center mb-5 ">
<label
class="col-lg-3 form-control-label #error('episodes_number') text-danger #enderror">تعداد
قسمت ها</label>
<div class="col-lg-9">
<input type="number" id="episodes_number" name="episodes_number"
value="{{old('episodes_number')}}"
class="form-control #error('episodes_number') border border-danger #enderror">
</div>
</div>
<div class="form-group row d-flex align-items-center mb-5 ">
<label
class="col-lg-3 form-control-label #error('body') text-danger #enderror">بدنه</label>
<div class="col-lg-9">
<textarea class="form-control #error('body') border border-danger #enderror"
name="body" id="mytextarea" rows="15">
{{old('body')}}
</textarea>
</div>
</div>
<div class="form-group row mb-5">
<label class="col-lg-3 form-control-label #error('tag') text-danger #enderror">تگ
ها</label>
<div class="col-lg-9 select mb-3">
<select name="tag[]"
class="custom-select form-control #error('tag') border border-danger #enderror"
multiple>
<option value="یک تگ را انتخاب کنید">یک تگ را انتخاب کنید</option>
#foreach($tags as $tag)
<option value="{{$tag->id}}">{{$tag->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group">
<label for="exampleFormControlFile1">عکس</label>
<input type="file" class="form-control-file" name="Pic"
id="exampleFormControlFile1">
</div>
<div class="form-group">
<button type="button"
onclick="addepisode(document.getElementById('episodes_number').value)"
class="btn btn-gradient-03 mr-1 mb-2">بعدی
</button>
</div>
</div>
</div>
<div class="tab-pane fade" id="tab-2" role="tabpanel" aria-labelledby="base-tab-2">
<div class="form-group row d-flex align-items-center mb-5 " id="test"></div>
<div class="form-group">
<button type="button" onclick="prev()" class="btn btn-gradient-03 mr-1 mb-2">قبلی
</button>
</div>
<div class="form-group">
<button type="button" onclick="finish()" class="btn btn-gradient-03 mr-1 mb-2">بعدی
</button>
</div>
</div>
<div class="tab-pane fade" id="tab-3" role="tabpanel" aria-labelledby="base-tab-3">
<div class="widget has-shadow">
<div id="IconRightCollapseOne" class="card-body collapse show"
data-parent="#accordion-icon-right" style="">
<div class="form-group row mb-5">
<div class="col-sm-3 form-control-label d-flex align-items-center">نام
دوره
</div>
<div id="courseName" class="col-sm-8 form-control-plaintext"></div>
</div>
<div class="form-group row mb-5">
<div class="col-sm-3 form-control-label d-flex align-items-center">قیمت
</div>
<div id="coursePrice" class="col-sm-8 form-control-plaintext"></div>
</div>
<div class="form-group row mb-5">
<div class="col-sm-3 form-control-label d-flex align-items-center">تعداد
قسمت ها
</div>
<div id="courseNumber" class="col-sm-8 form-control-plaintext"></div>
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-gradient-03 mr-1 mb-2">ارسال</button>
</div>
</div>
</div>
</form>
</div>
</div>
<!-- End Basic Tabs -->
</div>
</div>
#endsection
public function SendToDB(Request $request)
{
$new_Course = new Course($this->Validatepost());
$new_Course->user_id = 1;
$new_Course->image = 'Pics/' . $this->UploadPic(request());
$new_Course->save();
$new_Course->tag()->attach(request('tag'));
$courseID = $new_Course->id;
$episode_number = request()->input('episodes_number');
for ($x = 0; $x < $episode_number; $x++) {
$new_Episode = new Episode($this->validateepisode($episode_number));
$new_Episode->course_id = $courseID;
$new_Episode->save();
}
return redirect('/Admin/');
}
return redirect('/Admin/'); command must be end of function. Not end of "for loop"
i found my awnser
$new_Course = new Course($this->Validatepost());
$new_Course->user_id = 1;
$new_Course->image = 'Pics/' . $this->UploadPic(request());
$new_Course->save();
$new_Course->tag()->attach(request('tag'));
$courseID = $new_Course->id;
$episode_number = request()->input('episodes_number');
for ($x = 0; $x < $episode_number; $x++) {
$new_Episode = new Episode();
$new_Episode->title = \request()->input('Episode_Title_'.$x);
$new_Episode->body = \request()->input('Episode_Body_'.$x);
$new_Episode->number = $episode_number;
$new_Episode->course_id = $courseID;
$new_Episode->save();
}
return redirect('/Admin/');
}
validation function return true or false
and i must fill this data with code
i found my awnser
here my new code
public function SendToDB(Request $request)
{
$new_Course = new Course($this->ValidateCourse());
$new_Course->user_id = 1;
$new_Course->image = 'Pics/' . $this->UploadPic(request());
$new_Course->save();
$new_Course->tag()->attach(request('tag'));
$courseID = $new_Course->id;
$episode_number = request()->input('episodes_number');
for ($x = 0; $x < $episode_number; $x++) {
$new_Episode = new Episode();
$new_Episode->title = \request()->input('Episode_Title_'.$x);
$new_Episode->body = \request()->input('Episode_Body_'.$x);
$new_Episode->number = $x+1;
$new_Episode->course_id = $courseID;
$new_Episode->save();
}
return redirect('/Admin/');
}
This is my view file here i use 5 search field like product, group, branch, startdate and end date. All are placed in different table. Both were worked nicely and searched nicely but now i want some additional option that is when i select a particular branch in group field dynamically show values based on branch field value. Now it is showing whole data in db.
NOTE: i want this change appear when dropdown select.
<div class="row">
<div class="col-md-12">
<div class="tab-content">
<form method="get" id="frmSearchGroupPur" action="<?php echo
base_url('admin/group/listgroup'); ?>">
<div class="form-body">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label class="col-md-5 control-label"
style="text-align: left">Product</label>
<div class="col-md-7">
<?php
$product = array();
// if($data['products']){
// foreach ($data['products'] as
$p)
{
// $product[$p['id']] =
$p['name'];
// }
// }
echo form_dropdown('product', $product,
$data['product'], array('class' => 'form-control
select2','id'=>'product'));
?>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="col-md-5 control-label"
style="text-align: left">Branch</label>
<div class="col-md-7">
<?php
$branch = array(NULL=>'Any');
if($data['branches']){
foreach ($data['branches'] as $w){
$branch[$w['id']] = $w['name'];
}
}
echo form_dropdown('branch', $branch,
$data['branch'], array('class' => 'form-control','id'=>'branch_dat'));
?>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="col-md-5 control-label"
style="text-align: left">Group</label>
<div class="col-md-7">
<?php
$group = array(NULL=>'Any');
if($data['groups']){
foreach ($data['groups'] as $g){
$group[$g->id] = $g->name;
}
}
echo form_dropdown('group', $group,
$data['group'], array('class' => 'form-control'));
?>
</div>
</div>
</div>
</div><br/>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label class="col-md-5 control-label"
style="text-align: left">Purchase Date From</label>
<div class='input-group date col-md-7'
id='datetimepicker6'>
<?php echo
form_input('fromdate',$data['fromdate'],array('class' => 'form-
control','id'=>'fromdate')); ?>
<span class="input-group-addon">
<span class="glyphicon glyphicon-
calendar"></span>
</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="col-md-5 control-label"
style="text-align: left">Purchase Date To</label>
<div class='input-group date col-md-7'
id='datetimepicker7'>
<?php echo
form_input('todate',$data['todate'],array('class' => 'form-
control','id'=>'todate')); ?>
<span class="input-group-addon">
<span class="glyphicon glyphicon-
calendar"></span>
</span>
</div>
</div>
</div>
</div><br/>
<div class="row">
<div class="col-md-4">
<div class="form-group pull-center">
<button type="submit" id="btnFilter" class="btn
btn-info">Submit</button>
<a href="<?php echo
base_url('admin/group/listgroup'); ?>" class="btn btn-default">Reset</a>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
and my model file to get group is
function getGroupDetails($groupid=0,$state=''){
$this->db->where('id',$groupid);
$this->db->where('branchid',$state);
$query = $this->db->get('group');
if($query->num_rows()==1){
return $query->row_array();
}else{
return FALSE;
}
and my controller function is
function get_group(){
$state=$this->input->post('brch');
$result = $this->model_group->getGroupDetails($state);
// echo json_decode($result);
}
and my not worked jquery file to change group dynamically is below
$('branch_dat').change(function(){
var branch_da=$('#branch_dat').val();
if(branch_da != "")
{
var post_url="admin/sale/get_group";
$('branch_dat').change(function(){
var branch_da=$('#branch_dat').val();
if(branch_da != "")
{
var post_url="admin/sale/get_group";
$.ajax({
type:"POST",
url:post_url,
data:function(params)
{
return{ brch : params.brch};
}
});
}
});