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

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

Related

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

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

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

Property [id] does not exist on this collection in Form

I designed form to save data in Laravel
and this problem shows me the following error
Property [id] does not exist on this collection instance.
This is my Code
Controller
public function create ()
{
$post = Post::all();
return view ("post.create",compact('post'));
}
view create.blade.php
<form action="{{url()->action ('PostController#store', ['id'=>$post->id])}}" method="POST">
<input type="text" name="title" placeholder="Write Title">
<input type="text" name="body" placeholder="Write Body">
<input type="submit" value="Submit" name="submit">
</form>
I know the problem is here, but I don't know what I can do
'PostController#store', ['id'=>$post->id])}}" method="POST"
This will return the collection
$post = Post::all();
Instead, pass one object to the view by using
$post = Post::first();
or if u want to check particular post or any other condition you can use where clause to it..
eg: $post = Post::where('id',$user_id)->first(); // It will return single row...It will solve your problem
The problem is that you are sending a collection of Post models to your view. The $post variable contains a collection of Post models, which means that the collection will not have an id because it is a collection, not a single Post.
This:
$post = Post::all();
returns a collection.
Because this is a create method, you may wish to new up a Post model:
$post = new Post();
and add a few things to it and save it before sending to the view to get an id, OR, probably more useful: you can open the form without form-model binding since you don't actually have a Post model created yet -- just remove the ['id'=>$post->id] part since there is no post to have an id at the time the form is created.
You can make it look cleaner for the route.
web.php
Route::post('/myform/store', 'PostController#store')->name('post.store');
In your view, you can use the route name you just created.
<form action="{{ route('post.store', ['id' => $post->id]) }}" method="post">
But since you don't have column of 'id' in your Post then it returns the error you getting. Try creating a column 'id' first to resolve your problem.
I agree with #Watercayman on using model-binding instead, it's quick and makes the code more readable (and understandable too). Since Laravel quickly matches 'id' with unique id in the database. Take a look here for route parameters (how you can pass your data through URLs) and how to access your parameter here.
Using model binding will return you a collection.
public function store(Post $post)
Your $post variable is a collection so if you want to access your 'id' column you will do $post->id.

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