request has file is not working - laravel - laravel

public function store(Request $request) {
if($request->hasFile('image')) {
return "this is if";
} else {
return 'this is else';
}
}
This is my function. The print is "this is else". Why it's not going inside if?
public function store(Request $request) {
dd($request);
}
It is giving us:
public function store(Request $request) {
dd($request->all());
}
It is giving us:
So, I'm taking an 'image', but the hasFile function does not see it. How to fix it?
Extra: This is my form section:
<form method="post" action="{{ route('articles.store') }} " enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="">Title</label>
<input type="text" name="title" class="form-control" required>
</div>
<div class="form-group">
<label for="">Category</label>
<select name="category" required>
#foreach($categories as $category)
<option value="{{$category->id}}">{{ $category->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Article Image</label>
<input type="file" name="image" class="form-control" required>
</div>
<div class="form-group">
<label for="">Article Content</label>
<textarea name="contentArticle" id="summernote" rows="12"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success btn-block">Create an Article</button>
</div>
</form>

Have you try to do this ?
public function store(Request $request)
{
dd($request->all());
{
What result do you get ?

You have your enctype in your frontend code, there is not any problem just run the below commands:
composer dump-autoload
php artisan optimize:clear
If again that does not work then use the below code for checking if you have an image or not:
if ($request->file('image')!=null){...}
In place of:
if($request->hasFile('image')){...}

Related

Using Select in an Edit Form Laravel

Im trying to implement a Edit function to update permissions from an Laravel User, (I already installed Spatie), it parcially works, it save the changes but I cant see the permissions of the role on the Select Form. Any help please
<--CONTROLLER-->
public function edit($id)
{
$role = Role::findById($id);
$permissions = Permission::all();
return view('admin_roles_edit', compact('role','permissions', 'permissions1'));
}
public function update(Request $request, $id)
{
$request->validate([
'name'=>'required|max:30',
]);
$role = Role::findById($id);
$role->update($request->all());
$role->permissions()->sync($request->permissions);
return redirect()->route('admin.roles.index')->with('message', 'El rol se ha actualizado correctamente');
}
<--HTML-->
<form action="{{route('admin.roles.update', $role->id)}}" method="post"
class="form">
#csrf
<div class="form-group has-feedback">
<label class="control-label col-lg-3">Nombre<span
class="text-danger">*</span></label>
<input type="text" class="form-control" name="name"
value="{{$role->name}}" required="required">
#error('name')
<label class="validation-error-label" for="basic">{{$message}}</label>
#enderror
</div>
<div class="form-group has-feedback">
<label class="control-label col-lg-3">Permisos<span
class="text-danger">*</span></label>
<div class="multi-select-full">
<select class="multiselect" multiple="multiple" name="roles">
#foreach($permissions as $permission)
<option value="{{$permission->id}} #if($permission->id == $role->permission) selected #endif">{{$permission->name}}</option>
#endforeach
</select>
</div>
</div>
<div class="form-group pull-right">
<a href="{{route('admin.roles.index')}}" type="button" class="btn btn-default"><i
class="icon-cross2 position-left"></i>Cancelar
</a>
<button type="submit" class="submit-btn btn btn-success"><i
class="icon-add position-left"></i>Editar
</button>
</div>
</form>
<--RESULT-->
in your controller add this line:
public function edit($id)
{
$role = Role::findById($id);
$permissions = Permission::all();
$rolePermissions = $role->permissions()->pluck('name','id')->toArray();
return view('admin_roles_edit', compact('role','permissions', 'rolePermissions'));
}
update your blade:
<option value="{{$permission->id}}" {{in_array($permission->id, array_keys($rolePermissions)) ? 'selected' : '')}}>{{$permission->name}}</option>
Fixed finally
RoleController.php
public function edit($id)
{
$role = Role::findById($id);
$permissions = Permission::all();
return view('admin_roles_edit', compact('role','permissions'));
}
RoleView.blade.php
<select class="multiselect" multiple="multiple" name="roles">
#foreach($permissions as $permission)
<option value="{{$permission->id}}"
#if($role->hasPermissionTo($permission->id))
selected="selected"
#endif/>{{$permission->name}}</option>
#endforeach
</select>
Fixed Result

data isn't saving into db using eloquent relation

hi m trying to save data into db using Eloquent ORM one-to-one but it is not saving and not showing any error to fix it (m unable to find any solution about it because it is not showing any error),
public function store(Request $request)
{
$request->validate([
'owner_id' => 'required',
'phone_name' => 'required|unique:phones'
]);
$phone = new Phone;
$phone->owner_id = $request->owner_id;
$phone->phone_name = $request->phone_name;
$phone->save();
return redirect()->route('phone.index')->with('flash_messages_success', 'Phone has been added successfully');
}
protected $fillable = [
'pphone_name', 'owner_id'
];
public function owner()
{
return $this->belongsTo('App\Owner');
}
<form method="POST" action="{{ route('phone.store') }}">
#csrf
<div class="form-group">
<label for="">Phone Title</label>
<input type="text" name="phone_name" class="form-control" id="" placeholder="Phone Name">
</div>
<div class="form-group">
<label>Select Owner</label>
<select class="form-control" name="category_id">
<option selected="">Under Owner</option>
#foreach($owners as $owner)
<option value="{{ $owner->id }}">{{ $owner->owner_name }}</option>
#endforeach
</select>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="submit">
</form>
The issue in select name you named category_id so the view should be:
<select class="form-control" name="owner_id">

Laravel : no errors and no update on database happens after saving the edit

I have two tables, user and technicien, with a one to one relation. After editing technicien information through edit form and saving, no update happens on database and no errors as well.
Here is my code:
controllers
public function edit($id)
{
$technicien=technicien::find($id);
$user = $technicien->user;
return view('technicien.edit',['technicien'=>$technicien])->with('user',$user);
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update($request->get('user'));
$user->nom = $request->update('nom');
return redirect('/technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $technicien->technicien ) }}" method="update">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control" name="user[nom]" value="{{$user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control" name="user[prenom]" value="{{$user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control" name="user[email]" value="{{$user->email}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="moyenne_avis" class="form-control" value ="{{$technicien->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="actif" class="form-control" value ="{{$technicien->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-control btn btn-primary">
</div>
</div>
</form>
</div>
</div>
#endsection
route.php
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::patch('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
You just need to pass parameters to update function.
Read docs
public function update(Request $request, $id)
{
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update([
'nom' => $request->nom,
'premon' => $request->premon,
'email' => $request->email
]);
return redirect('/technicien');
}
Also from the docs
You should define which model attributes you want
to make mass assignable. You may do this using the $fillable property
on the model.

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);
}

laravel edit and update doesn't work

I have a CRUD with simple date. But I can't get the update method to work.
This is my controller
public function show(Customer $customer)
{
return view('customer.show',compact('customer'));
}
public function edit(Customer $customer)
{
return view('customer.edit', compact('customer'));
}
public function update(CustomerRequest $request, Customer
$customer,$id)
{
$customer = Customer::find($id)->update($request->all());
return redirect()->route('customer.index',compact('customer'));
}
and that is my view
<form method="POST" action="{{route('customer.update',$customer->id ) }}">
{{--{{dd($customer)}}--}}
{{method_field('PUT')}}
{{ csrf_field() }}
<div class="form-group">
<label for="firstName">Voornaam</label>
<input type="text" class="form-control" name="firstName" value="{{$customer->firstName}}">
</div>
<div class="form-group">
<label for="lastName">Achternaam</label>
<input type="text" class="form-control" name="lastName" value="{{$customer->lastName}}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" value="{{$customer->email}}">
</div>
<div class="form-group">
<label for="phone">Telefoonnummer</label>
<input type="text" class="form-control" name="phone" value="{{$customer->phone}}">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
I can go to the edit page. But, after changing the data nothing change it look like that I miss save somewhere but I don't know, now I get that that error too few arguments to function New Controller ::update () 2 passed and exactly 3 expected.
any help will be appreciated.
Change:
public function update(CustomerRequest $request, Customer
$customer,$id)
To:
public function update(CustomerRequest $request, $id)
Or:
public function update(CustomerRequest $request, Customer
$customer)
With the last one you can remove Customer::find($id) and just use $customer
EDIT
If you look at the update route: route('customer.update',$customer->id ) you see it only takes 1 argument. The controller expects 2 because one is the request and the other is the ID.

Resources