I am trying to add rating to a user and running in to error Call to a member function Review() on null. in laravel 5.8 - laravel

i am trying to add rating and reviews to the user here is so far what i have done
i have made a separate controller for review
and in the store method of that controller my code is as follow
public function store(Request $request,User $user)
{
//
$up=User::find($user->id);
if($request->review)
{
$up->Review()->attach($request->review);
}
if($request->rating)
{
$up->Review()->attach($request->rating);
}
return redirect()->back();
}
i have created a seprate model for the review as well in which my code is looks like this
class Review extends Model
{
//
protected $fillable=['rating','review'];
Public function User()
{
return $this->belongsToMany(User::class);
}
}
where in my user model i have made this
Public function Review()
{
return $this->belongsToMany(Review::class);
}
where as my migrations looks like this
public function up()
{
Schema::create('review_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->string('rating');
$table->string('review');
$table->timestamps();
});
}
my forms looks like this
<form action="{{route('ReviewsUser',$user->id)}}" method="POST">
#csrf
#method('PUT')
<div class="feedback-yes-no">
<strong>Your Rating</strong>
<div class="leave-rating">
<input type="radio" name="rating" id="rating-1" value="1" checked/>
<label for="rating-1" class="icon-material-outline-star"></label>
<input type="radio" name="rating" id="rating-2" value="2"/>
<label for="rating-2" class="icon-material-outline-star"></label>
<input type="radio" name="rating" id="rating-3" value="3"/>
<label for="rating-3" class="icon-material-outline-star"></label>
<input type="radio" name="rating" id="rating-4" value="4"/>
<label for="rating-4" class="icon-material-outline-star"></label>
<input type="radio" name="rating" id="rating-5" value="5"/>
<label for="rating-5" class="icon-material-outline-star"></label>
</div><div class="clearfix"></div>
</div>
<textarea class="with-border" placeholder="Enter Your Comments" name="review" id="review" cols="7" required></textarea>
<!-- Button -->
<button class="button full-width button-sliding-icon ripple-effect" type="submit" >Your Review's <i class="icon-material-outline-arrow-right-alt"></i></button>
</form>
what i am trying to do is to store reviews that other users have given the user anyone who can help me overcome this error would be highly appreciated .

Call to a member function Review() on null
This error means that your variable $up calling the member function Review() has a null value. Which means it's not finding your user in
$up = User::find($user->id);
Your post route doesn't have a route parameter to pass the user ID to find the user in the first place.
Therefore, changing this
Route::post('Reviews','ReviewsController#store')->name('ReviewsUser');
to this
Route::post('Reviews/{user}','ReviewsController#store')->name('ReviewsUser');
will now capture the User ID in your controller via User $user

Related

foreach error laravel multiple checkbox store

I store a few checkboxes as array in Laravel, but I get an error on Foreach. Can someone help me?
in model
public function setHandServiceAttribute($value)
{
$this->attributes['handservice'] = json_encode($value);
}
public function getHandServiceAttribute($value)
{
return $this->attributes['handservice'] = json_decode($value);
}
in checkbox
<div class="form-check mx-3">
<input class="form-check-input" type="radio" id="1" value="کاشت" name="handservice[]">
<label class="form-check-label" for="1">کاشت</label>
</div>
<div class="form-check mx-3">
<input class="form-check-input" type="radio" id="2" value="کاور" name="handservice[]">
<label class="form-check-label" for="2">کاور</label>
</div>
handservice is column of reservation table in database with value like this after store
["\u062a\u0631"]
and in view
#forelse($reservation->handservice as $hand)
{{ $hand }},
#empty
nothing
#endforlse
error
json_decode(): Argument #1 ($json) must be of type string, array given
I used this article but I get an error when one of the columns handservie is empty handservice in nullable
https://hackthestuff.com/article/how-to-store-multiple-checkbox-value-in-database-in-laravel8
It finally worked
in model
public function getHandServiceAttribute($value) {
return json_decode($value);
}
in view
#if($reservation->handservice != null)
#forelse($reservation->handservice as $hand)
{{$hand}},
#empty
نا مشخص
#endforelse
#else
<p class="text-danger">انتخاب نشده</p>
#endif
thanks all
You shouldn't try to assign something in a getter.
public function getHandServiceAttribute()
{
return json_decode($this->attributes['handservice']);
}

GET Data empty in Laravel

Hi I am new to Laravel and so far I made good progress. Right now my head is blocked now and I need some direction or help, please.
I am getting data from radio button but GET Data is empty. I need to fill-in this data (pay) into an exist DB and I am getting "Creating default object from empty value" and I agree with Laravel :) I guess, my lack of knowledge is blocking me here.
Thanks.
This is the GET and POST data
GET Data empty
POST Data
_token = "72nrnI7Y7xuIQJe6LZPLGLzNsAv6ZZbY29zkjcIr"
pay = "CC"`
This is the Model
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
use Auth;
class DAddress extends Model
{
protected $table='dAddress';
protected $fillable = ['payment_method'];
public function createPay()
{
$user = Auth::user();
$order = $user->daddress()->create([
'payment_method' => paymentMethod()
]);
}
}
This is the Controller
public function paymentMethod(Request $request) {
$address->payment_method = $request->pay;
DAddress::createPay();
Cart::destroy();
return redirect('abc');
}
This is where I get the HTML data
<form action="{{url('/paymentMethod')}}" method="post">
<input type="hidden" value="{{csrf_token()}}" name="_token"/>
<div class="form-group">
<div class="col-md-6">
<!-- First name -->
<input type="radio" class="form-control" name="pay"
value="CC"><i class="fa fa-credit-card"></i> Credit Card
<br> <br>
<input type="radio" class="form-control" name="pay"
value="PP"><i class="fa fa-paypal"></i> Paypal
<br> <br>
<input type="radio" class="form-control" name="pay"
value="BT"><i class="fa fa-university"></i> Bank Transfer
</div>
</div>
<input type="submit" class="btn btn-primary" value="Move to Last Page" />
This is the User.php I added below function.
public function daddress()
{
return $this->hasMany(DAddress::class);
}
i do not understand what you are trying to do with this line $address->payment_method = $request->pay;
when you say $address->payment_method, what is the $address object holding and where is payment_method you are trying to access, why not do something like this $payment_method = $request->pay, except is you are setting $payment_method as a global variable
if you are using the laravel create method you could do something like this $payment_method= App\createPay::create(['payment_method' => $request->pay]);
else you could instantiate you model like
$pay =createPay();
$pay->payment_method=$request->pay;
$pay->save();
i would have love you logic to be in the controller, i would have done something like this in my controller
public function createPay(Request $request){
$user = Auth::user();
$id=$user->id;
$payment=createPay::find($id);
$payment->payment_method=$request->pay;
$payment->save();
return redirect('abc');
}
i used laravel eloquent here, that is if you want to update a record, i don't know if this is close to what you want.

How to link to another page when clicking button

login.php (controller)
class login extends CI_Controller {
public function index()
{
$this->load->view('login');
}
public function Click()
{
$action = $this->input->post('register'); // $_POST['start']; also works.
if($action)
{
$this->load->view('register');
}
}
}
login.php(views)
<form action="" class="loginForm" method="POST">
<div class="input-group">
<input type="submit" id="submit" class="form-control" value="Login" name="login">
<input type="submit" id="submit" class="form-control" value="Buat Akun" name="register" >
</div>
</form>
how can I change my view to register.php after clicking register button. The error is I keep back to login page after clicking register.
You can try this :
In Login controller :
public function register()
{
$this->load->view('register');
}
In view:
<form action="" class="loginForm" method="POST">
<div class="input-group">
<input type="submit" id="submit" class="form-control" value="Login" name="login">
<a href="<?php echo base_url('login/register');?>" id="submit" class="form-control" name="register" >Register</a>
</div>
</form>
You need to enable mod_rewrite module in apache
Please follow below mention step to enable mod_rewrite module:
1) Find the “httpd.conf” file under the “conf” folder inside the Apache’s installation folder.
2) Find the following line “#LoadModule rewrite_module modules/mod_rewrite.so” in the “httpd.conf” file.You can do this easily by searching the keyword “mod_rewrite” from find menu.
3) Remove the “#” at the starting of the line, “#” represents that line is commented.
4) Now restart the apache server.
5) You can see now “mod_rewrite” in the Loaded Module section while doing “phpinfo()”.
You can try this :
In Login Controller:
class Login extends CI_Controller {
public function index()
{
if($this->input->post('login') == 'Login'){
$this->load->view('login');
}else{
$this->register();
}
}
public function register(){
$this->load->view('register');
}
}
In View :

Laravel - Upload to Existing Model Record

Maybe it's because I'm tired, but I can't seem to get a simple upload working for one of my models.
On the show details page of my customers (who are NOT users) model, I have a simple form where a user can upload the logo of the customer.
The form:
<form enctype="multipart/form-data" action="/customers/i/{{$customer->url_string}}" method="POST">
<input type="file" name="logoUpload">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<input type="submit" class="pull-right btn btn-sm btn-primary" value="Upload">
</form>
The Controller:
public function logoUpload(Request $request){
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
$customer->car_logo = $path;
$customer->save();
return back();
}
}
I know the issue is that I haven't defined $customer in the controller since the file does actually store itself in the correct folder after I click submit, but it does not hit the database at all.
Update
The current customer details url:
http://localhost:8000/customers/i/dsdado9a98w78721
The web definition for the post route:
Route::post('/customers/i/{customer}', 'CustomerController#logoUpload');
You have to create a hidden field in your form that contains the customer id and then use it in your controller to update it with the new file path, here is an example:
View
<form enctype="multipart/form-data" action="/customers/i/{{$customer->url_string}}" method="POST">
<input type="file" name="logoUpload">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<!-- customer_id field -->
<input type="hidden" name="customer_id" value="{{$customer->id}}">
<input type="submit" class="pull-right btn btn-sm btn-primary" value="Upload">
</form>
Controller
public function logoUpload(Request $request){
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
// get customer
$customer = Customer::find($request->customer_id);
$customer->car_logo = $path;
$customer->save();
return back();
}
}
You have some options, here is a simple one, with only adjusting that controller method:
public function logoUpload(Request $request, $customer)
{
$customer = Customer::where('url_string', $customer)->firstOrFail();
if($request->hasFile('logoUpload')){
$path = Storage::putFile('public/customer/uploads', new File(request('logoUpload')));
$customer->car_logo = $path;
$customer->save();
return back();
}
...
}
We have added a parameter to the method signature to accept the route parameter. We then find the model via url_string matching that parameter.
You also could setup route model binding as well to do the resolving of that model based on the route parameter for you.

Codeigniter: simple form function

I'm stuck writing a simple form...I feel dumb.
Here's my controller:
function welcome_message(){
//Update welcome message
$id = $this->session->userdata('id');
$profile['welcome_message'] = $this->input->post('welcome_message');
$this->db->update('be_user_profiles',$profile, array('user_id' => $id));
}
And the html:
<?php print form_open('home/welcome_message')?>
<input type="checkbox" value="0" checked="false">Don't show me this again</input>
<p>
<input class="button submit" type="submit" class="close-box" value="Close" />
</p>
<?php print form_close()?>
Edit
I simply need it to submit to a private function and return to the home page (page submitted from).
You have to name your input...?
<input type="checkbox" value="0" name="welcome_message" checked="false">
Don't show me this again
</input>
EDIT: You can't submit to a private function via the URL (in a default CI installation, anyway).
Do it like this:
function welcome_message()
{
if(isset($_POST['welcome_message']))
{
$this->_my_private_function();
}
}
private function _my_private_function()
{
// do your thing with the $_POST data
redirect('your/uri', 'location');
}
But you really don't have to redirect if your returning to the same page (controller method) that called the private function in the first place. Would be a waste.

Resources