Trying to get property 'serino' of non-object - php

I don't want to use "id" to edit the product, I want to use the "serial no" column. But I get the following error:
Trying to get property 'serino' of non-object
Product.php
public function edit($serino)
{
$product= DB::select('select * from products where serino=?',[$serino]);
return view("products/edit",compact("product"));
}
index.blade.php
<a href="{{route('products.edit',$product->serino)}}">
<button class="btn btn-info btn-sm"><i class="fas fa-edit"></i> Edit</button>
</a>
edit.blade.php
<form class="form-horizontal" action="{{route('products.update',$product->serino)}}" method="post">
{!! csrf_field() !!}
{!! method_field('put') !!}
<div class="form-group">
<div class="col-sm-12">
<input type="text" class="form-control" value="{{$product->name}}" name="name" placeholder="Product Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="text" class="form-control" value="{{$product->category}}" name="category" placeholder="Product Category">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-12">
<button type="submit" class="btn btn-success">Save</button>
</div>
</div>
</form>
Good Work..

Your query will return you collection/array of result, no just one Product.
You can use sometihing like this:
$product = DB::table('products')->where('serino', $serino)->first();

Related

I can't get data using id

I can't get data with id it returns this: %7Bid%7D.
I want to get the id and edit the data.
Controller:
public function edit($id){
$slider = DB::table('header_sliders')->find($id);
return view('posts.edit',['header'=>$slider]);
}
View:
<div class="container">
<div class="row">
<div class="col-md-12">
<form action="{{url('admin/edit')}}" method="POST" >
{{csrf_field()}
<div class="form-group">
<label for="exampleInputEmail1">Mətn</label>
<input type="text" name="text" class="form-control" aria-describedby="emailHelp" value="{{$header->text}}">
<small id="emailHelp" class="form-text text-muted"></small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Şəkil</label>
<input type="file" name="imgName" class="form-control" value="{{$header->imgName}}">
</div>
<div class="form-check">
</div>
<button type="submit" class="btn btn-primary">Dəyiş</button>
<a href="{{url('admin/edit/{id}')}}" class="edit" data-toggle="modal"><i
class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
Create a Route like this
Route::get('/admin/edit/{id}','UserController#edit');
and pass the id with your URL like this
YourURL..../admin/edit/11
This will solve your problem

Trying to login with custom field "user_id" in laravel

I am trying to login with a custom field called "user_id" but it is not working. I am getting page expired error. Please help.
Login page
<form role="form" action=" {{route('user.login')}}" method="POST">
<div class="form-group mb-3">
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-email-83"></i></span>
</div>
<input class="form-control" placeholder="User Id" name="user_id" type="text">
</div>
</div>
<div class="form-group">
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-lock-circle-open"></i></span>
</div>
<input class="form-control" placeholder="Password" name="password" type="password">
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary my-4">Log in</button>
</div>
</form>
My database fields
id
user_id
user_name
password
type
role
remember_token
created_at
updated_at
by the way my remember_token id null always i don't know why.
changed in Illuminate\Foundation\Auth\AuthenticatesUsers
public function username()
{
return 'user_id';
}
web.php
Route::get('/login', 'UserController#login_form')->name('user.loin_form');
Route::post('/login', 'Auth\LoginController#login')->name('user.login');
what did i missed
Just add the csrf token to your form and it will work.
You can add the #csrf blade directive or manually create your csrf input, like this one:
<input type="hidden" name="_token" value="{{ csrf_token() }}">
Using the blade directive:
<form role="form" action=" {{route('user.login')}}" method="POST">
#csrf
<div class="form-group mb-3">
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-email-83"></i></span>
</div>
<input class="form-control" placeholder="User Id" name="user_id" type="text">
</div>
</div>
<div class="form-group">
<div class="input-group input-group-alternative">
<div class="input-group-prepend">
<span class="input-group-text"><i class="ni ni-lock-circle-open"></i></span>
</div>
<input class="form-control" placeholder="Password" name="password" type="password">
</div>
</div>
<div class="text-center">
<button type="submit" class="btn btn-primary my-4">Log in</button>
</div>
</form>
Hope it helps.

Keep dropdown menu selected laravelcollective/html

I have a dropdown menu like this:
{!! Form::open(['method'=>'get']) !!}
<div class="row">
<div class="col-sm-4 form-group">
{!! Form::select('sort',[''=>'Choose Sort','asc'=>'Ascending','desc'=>'Descending'],null,['class'=>'form-control','onChange'=>'form.submit()']) !!}
</div>
<div class="col-sm-5 form-group">
<div class="input-group">
<input class="form-control" id="search"
value="{{ request('search') }}"
placeholder="Search name" name="search"
type="text" id="search"/>
<div class="input-group-btn">
<button type="submit" class="btn btn-warning">Search</button>
</div>
</div>
</div>
</div>
{!! Form::close() !!}
When I choose a item like "Ascending", my page reloaded and the dropdown menu return into "Choose Sort". I want to keep my selected value on dropdown menu.
How can I do that? Thank you very much!
Its simple just need to pass variable
Change Your route as per your requirement
In Your View
{!! Form::open(['method'=>'get','route' => 'document.sort']) !!}
<div class="row">
<div class="col-sm-4 form-group">
{!! Form::select('sort',[''=>'Choose Sort','asc'=>'Ascending','desc'=>'Descending'],isset($sortvalues) ? $sortvalues : '',['class'=>'form-control','onChange'=>'form.submit()']) !!}
</div>
<div class="col-sm-5 form-group">
<div class="input-group">
<input class="form-control" id="search"
value="{{ request('search') }}"
placeholder="Search name" name="search" value="{{ isset($searchvalues) ? $searchvalues : ''}}"
type="text" id="search"/>
<div class="input-group-btn">
<button type="submit" class="btn btn-warning">Search</button>
</div>
</div>
</div>
</div>
{!! Form::close() !!}
And my route
Route::any('/sort', 'DocDocumentController#sorDocument')->name('document.sort');
And finnaly in Controller
public function sorDocument(Request $request)
{
$docDocuments = DocDocument::latest()->paginate(20,['*'],'documentPage');
$searchvalues = $request->search;
$sortvalues = $request->sort;
$viewShare = array_keys(get_defined_vars());
return view('docdocuments.index', compact($viewShare));
}

error while retrieving data from the database

i was trying to display news with its comments, but one of the headlines is giving me an error,but others are working, i have a table of news and i have a table of news_comments, some of the news are being retrieved perfectly with their comments but this one is giving me an error.
#extends('layouts.app')
#section('content')
<div class="container">
{{$news->title}}<br/>
#foreach($news->news_pictures as $news_picture)
<img src="{{asset($news_picture->pictures)}}" width="200"><br/>
#endforeach
{!! $news->body !!} <br/>
<small>written on{{$news->created_at}} by {{$news->user->name}} </small>
</div>
#foreach($news->news_comments as $news_comment)
#if(!Auth::guest())
<div class="well">
{!! $news_comment->user->name!!}
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#else
<div class="well">
{{$news_comment->commentor}}<br/>
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#endif
#endforeach
#if(!Auth::guest())
<form action="{{action('NewsController#AddComments',[$news->id])}}" method="post">
{{csrf_field()}}
<div class="container">
<textarea type="text" class="form-control" name="comments" placeholder="your comment"></textarea>
<button class="btn btn-primary" >post</button>
</div>
</form>
#else
<form action="{{action('NewsController#AddComments',[$news->id])}}" method="post">
{{csrf_field()}}
<div class="container">
<input type="text" class="form-control" placeholder="your name" name="commentor">
<textarea type="text" class="form-control" name="comments" placeholder="your comment"></textarea>
<button class="btn btn-primary" >post</button>
</div>
</form>
#endif
#endsection
This error occur when the property you want to access that doesn't exist so you should check property isset or not
Here is the example
#if (!is_null($news_comment->user))
<div class="well">
{!! $news_comment->user->name!!}
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#endif
OR
#if (isset($news_comment->user->id) && isset($news_comment->user->name))
<div class="well">
{!! $news_comment->user->name!!}
{{$news_comment->comments}}<br/>
{{$news_comment->created_at}}
</div>
#endif
Try to use: with () on user
{!! $news_comment->user()->name!!}
Instead of:
{!! $news_comment->user->name!!}

Php laravel using post method by controller

am try to send value form from by post method to controller
here is my view,and how can i use post method to send
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="button">
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
here is controller method like
public function postSaveedit($meaning){
}
using route by controller
You should read up on Requests in Laravel: https://laravel.com/docs/5.2/requests#accessing-the-request
You need to pass that to your controller
public function postSaveedit(Request $request) {
$input = $request->input();
$foo = $input['foo'];
$bar = $input['bar'];
$baz = $input['baz'];
}
In Laravel 5.2 you can use request() helper method to solve your problem...
This is how you can do it...
Routes file should look like this (be sure that this route should be of post type)
Route::post('/myurl', 'Controllername#postSaveEdit')->name('postSaveEdit');
Form file should look like this, also please specify the input field names in the form so that you can grab them in the the controller by their specified names (like - title, meaning - see below code)...
<form class="form-horizontal" action="{{ route('postSaveEdit') }}" method="POST" role="form">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" name="title" value='{{ $words->first()->title }}' type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" name="meaning" value="{{ $words->first()->meaning }}" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<button class="btn btn-primary" type="submit">Save Changes</button>
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
and controller should be like this...
public function postSaveEdit() {
// The inputs variable contains all your form's inputs in the form of array...
$inputs = request()->all();
/*
$inputs = array(
'title' => 'title_value',
'meaning' => 'meaning_value'
)
*/
// Wheareas you can also get them by using 'get' method on request method like this
$title = request()->get('title');
$meaning = request()->get('meaning');
}
here u go
Form
you have to add method to your form + names to your inputs
<form class="form-horizontal" role="form" method="POST">
<!-- Add csrf token -->
{!! csrf_field() !!}
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="input1">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="input2">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" type="submit" value="Save Changes"/>
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
controller
Use Word; // at the top of the class
public function postSaveedit(Request $request) {
$word= new Word; // if you are creating a new record
$word= Word::find(1);// if you are updating a record
$word->title = $request->input('input1');
$word->meaning= $request->input('input2');
$word->save();
return view('home.blade.php');
}
Routes file
Route::get('/myurl', 'Controllername#postSaveedit');
:)

Categories