'pic_name' field doesn't have a default value Laravel - php

so im trying to insert the name of picture into Mysql DB but after i submit the form it gives me a error of 'pic_name' field doesn't have a default value... that means my controller is not storing value of form
here is my blade :
<div>
<form action="/product_store" method="post" enctype="multipart/form-data">
#csrf
<input type="text" name="name_product">
<input type="file" name="file_product">
<button type="submit">submit</button>
</form>
</div>
and controller:
public function store(Request $request)
{
$original_name= $request->file('file_product')->getClientOriginalName();
$path = $request->file('file_product')->store('storage');
Product::create(['name' => $request['name_product'], 'pic_name' => $original_name]);
}
name field is storing perfectly so there is no problem with my web.php

Make pic_name fillable in model.
class Product extends Model
{
protected $fillable = [
'name', 'pic_name'
];
}
Note : sometimes what happened in your table that feild value is not set for default or not null. so while inserting data you don't get that value to insert in that case you need to make as default null.
Table-> feild -> make it null
-> make null if you don't want to store value while insert

Related

Attempt to read property "id" on null - description.blade.php

I have a problem with id
i get error
Attempt to read property "id" on null (View: /resources/views/admin/Attendance/description.blade.php)
view code that show error
<form method="post" action="{{route('time.outd',['id'=>$attend->id])}}" enctype="multipart/form-data" >
controller code:
public function outd(Request $request, $id)
{
$request->validate([
'out'=>'',
'description'=>'required',
]);
$attend=Attendance::where('employee_id',$id)->orderBy('id','desc')->first();
$attend->description=$request->get('description');
$attend->update([
'out' => Carbon::now()
]);
$attend->save();
return redirect()->route('time.index')->with('success', ' success.');
}
I think you're just a little off here. '$attend->id you are setting the id to a null value because you haven't set it. From the controller that is passing the view of this form you need to pass that id to the front. So your collection of $attend is available on the front. Should be something like this in your controller action.
return view('index', compact('attend'));
Then in your form action just post to a non wildcard route...
action="{{route('time.outd')}}"
Then in your form elements put...
<input type="hidden" name="id" value="{{$attend->id}}">
Then in your controller where this is being posted to your id will be...
$request->id
Now you know if it is null on the back-end post that there was no record from the model and that you need to create a new one. If it has an id then you can go on about your day processing.

Delete data from the table after renaming column Laravel 5.8

Recently I've started with Laravel 5.8 and I'm trying to make the delete button which will delete row from the database. But before I create the button, I've Already Change the column id name in the table from id to id_book, the problem is Laravel still get the id-data, not id_book, and raise an error
like this
**Column not found: 1054 Unknown column 'books.id'**
this is button's code:
<form action="/hapus/{{ $book->id_book }}" method="POST">
#method('DELETE')
#csrf
<input type="submit" value="Hapus">
</form>
this is the Controller's Method:
public function hapus($id_book){
$book = Books::find($id_book);
$book->id;
if($book->gambar = 1){
Storage::delete($book->gambar);
$book->delete();
return redirect('/books');
}
else{
$book->delete();
return redirect('/books');
}
the column in tables is:
id_book (previously is id),
name,
image,
category,
description
thank for help, this is my model after fixing this problem:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Books extends Model
{
protected $table = "books";
protected $primaryKey = "id_book";//solution
protected $fillable = [
'title',
'images',
'category',
'description'
];
}

laravel passing data from table to table

so i have a form that input data with <option>. the value is come from one database i provide. ( i can make the value from blade.php but the requirement require it from db). and the form will send the data to the main database. i've already make the view and the <option> work. but the trouble comes when i want to submit the data to the main database.
-the main db = blogs ( target column = 'sistem')
-the option db = sistems ( source column = 'nama')
the goal is the value of 'nama' passed to the 'sistem'
this is the view form
<form action="/" method="post" enctype="multipart/form-data">
// .......
// .......
<select name="sistem[]" id="tag_select">
<option value="0"> Tidak Ada </option>
#foreach ($sistems as $sistem)
<option value="{{$sistem->id}}"> {{$sistem->nama}}</option>
#endforeach
</select>
// .......
// .......
</form>
this is the store controller
public function store(Request $request)
{
// .........
// .........
$request -> sistem = array_unique(array_diff($request->sistem, [0]));
$blog -> sistem = $request -> sistem;
// .........
// .........
$blog -> save();
}
Like above and also are you shure that action "/" points to store method?
The save() method will save the changes that you make to your Eloquent model. Also you shouldn't need to edit the $request object to achieve the required result here.
Additionally you will need to ensure that this property on your model can be mass assigned (either in the $fillable array or excluded from the $guarded array on the $blog model).
$blog->sistem = array_unique(array_diff($request->sistem, [0]));
$blog->save();
I will show how i store things via post method and it works for me, but you must know that I am noob. Sory if what i writting is obvious for you, but cant do anything more :)
public function store(request $request)
{
$this->validatePost();
$post = new Post(request(
['title', 'excerpt','deadline']
));
$post->save();
return redirect(route('admin.posts'));
}

I seems cant to save my Uploaded file to database LARAVEL?

hello im trying to save my filename in database laravel , but it gives me
im doing upload image with laravel. The image is uploaded yet the field from my rent database not. This is what happens.
Creating default object from empty value
im using Storage:link
Controller
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
public function insertproof(Request $request)
$id = $request['id'];
if ($request->hasFile('rent_proof')){
$filepath = $request->file('rent_proof')->store('app/folder_proof');
}
$result = rent::find($id);
$result->rent_proof=$filepath;
$result->save();
}
and here is my view and this view is from mymodal
<form method="POST" action="{{route('visitor_rent.insertproof')}}"
enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<input type="file" class="form-control" name="rent_proof" id="proof">
</div>
<button type="submit" class="btn btn-success">Save</button>
</form>
This is my model
public class Rent extends Model
{
protected $primaryKey = 'id';
protected $fillable = ['rent_proof'];
}
edit :
hello this one that gives me the error
$result = rent::find($id);
$result->rent_proof=$filepath; //<--this line is giving me the error
$result->save();
thanks for the care
is there anything that i miss?
Version laravel 5.5
PHP 7.2
Creating default object from empty value this error is because id which you have passed doesn't exists in your database.
if you are creating a new record with id and image path
then you should use
$result = new Rent;
$result->rent_proof=$filepath; //<--this line is giving me the error
$result->save();
If you are updating existed one then pass proper id which exists in database

Laravel 5.1 Delete a row from database

I am trying to delete a category by clicking on a button
Blade:
<td class="center"><span class="glyphicon glyphicon-trash"></span></td>
Route:
Route::get('/deletecat/{name}','CategoryController#delete');
Controller:
public function delete($name)
{
category::find($name)->delete();
return Redirect::route('managecategory');
}
but I am getting an error while clicking on a button that
Call to a member function delete() on a non-object
Any help appreciated.
The ::find($id) method expects $id to be a number, the primary key of the row you want to find.
If you want to delete a row by name, you should use the following code:
category::where('name', $name)->delete();
The error is obviously because of the returned NON-OBJECT.
::find($name) returns an empty object or null
This is because the search was empty.
Make sure name is the primary key in order to use find.
Otherwise:
use ::where('name', $name)->delete();
or:
::where('name','=', $name)->delete();
*I pointed = because if you ever need to search different than = use >= <> and so on...
Secondly, it is not very important to use destroy. It is recommended but not a must-have!
In your view:
it has to be a form to submit a POST request like below:
<form action="/deletecat/{{$data->id}}" method="post">
#csrf
#method('DELETE')
<input type="submit" value="delete " >
</form>
Because this is POST Request has to use csrf and delete action.
In your route, it has be with delete() method,followwing with the controller.
Route::delete('/deletecat/{name}', 'CategoryController#destory');
In the controller:
public function destory($name)
{
category::find($name)->delete();
return Redirect::route('managecategory');
}
This is how laravel name format role:

Categories