Laravel explain me how working query - laravel

I have a problem with query on laravel.
Please show me how it work, because I can't understand doc.
For example, I have VideoController.php and I have some data from forms:
$gall = array(
'name' => Input::get('name'),
'user_id' => Auth::id()
);
now I want to add this data to DB, but I don't know how to call to create function in model (and how this function should look).
And please explain me, how I should select data from database and display it on view, where for example user_id = 15;

In your VideoController.php file you should have a method to store the data, which you should post your data to through a form or something.
In a view, this form should look something like this:
{{ Form::open(['route' => 'video.store']) }} <!-- check your routes to see if video.store exists, you'll get an error otherwise -->
... form elements ...
{{ Form::submit('Submit') }}
{{ Form::close() }}
In your store() function you should have code similar to this
$video = new Video(); // if your video model is Video.php
$video->name = Input::get('name');
$video->user_id = Auth::id();
$video->save();
And if you want to display data, i.e. your video index or something, in your VideoController.php file, in the index() function:
$videos = Video::all();
return View::make('video.index', array('videos' => $videos));
then in your view
#foreach($videos as $video)
{{ $video->name }}
#endforeach

Related

Second page of paginate won't display the filtered data

I'm want to use paginate in laravel but didn't work perfectly. When i paginate the filtered data it worked fine, but only for the first page.The problem is when i moved to the second page, it will display the second page of all data not the filtered data. i want it so the second page will display the second page of filtered data
in blade.php I'm using links to call the paginate(for moving page)
<div class="paginate">
{{ $data->links() }}
</div>
Here's the filter controller
function filter(Request $request){
$data = profiles::when($request->has('pNamaLengkap'), function($query) use ($request){
$query->where('pNamaLengkap','like','%'.$request->pNamaLengkap.'%');
});
if($request->pJobDescription != 'All'){
$data = $data->where('pJobDescription','like','%'.$request->pJobDescription.'%');
}
if($request->pUnitKerja != 'All'){
$data = $data->where('pUnitKerja','like','%'.$request->pUnitKerja.'%');
}
if($request->pDirectorate != 'All'){
$data = $data->where('pDirectorate','like','%'.$request->pDirectorate.'%');
}
if($request->pRank != 'All'){
$data = $data->where('pRank','like','%'.$request->pRank.'%');
}
return view('export', [
'data' => $data->paginate(5),
'jobdescription' => jobdes::all(),
'unitkerja' => unitkerja::all(),
'direktorat' => direktorat::all(),
'rank' => rank::all(),
'oldjob' => $request->pJobDescription,
'oldunit' => $request->pUnitKerja,
'olddir' => $request->pDirectorate,
'oldrank' => $request->pRank,
'oldname' => $request->pNamaLengkap
]);
// return redirect('export')->with([
// 'data' => $data,
// 'jobdescription' => jobdes::all(),
// 'unitkerja' => unitkerja::all(),
// 'direktorat' => direktorat::all()
// ]);
}
I'll make an example from a picture
Filtered JobDescription Page 1
Page 2 that reset the filter and display all data
in the URL parameter it change it from
http://127.0.0.1:8000/filterexport?pNamaLengkap=&pJobDescription=Full+Stack+Developer&pUnitKerja=All&pDirectorate=All&pRank=All&export=Filter
into
http://127.0.0.1:8000/filterexport?page=2
for extra note, the paginate is working but just didn't work when you want to view the second page because somehow it reset the input filter
Thank you for reading this page, i really need help in this one
$data->paginate(15)->withQueryString();
use withQueryStrings() method with paginated data.
another solution is replacing
{{ $data->links() }}
to the
{{ $data->appends(Request::except('page'))->links() }}
For lower versions of Laravel, you can use this:
{!! str_replace('/?', '?', $data->appends(Input::except('page'))->render()) !!}

Laravel Cannot Decrypt Encrypted ID On Controller

I cannot decrypt the encrypted value on a controller after clicking on the submit button on my blade file below.
Controller :
public function edit($id)
{
$encrypted_id = encrypt($id);
return view('my.blade.edit', compact('encrypted_id'));
}
public function update(Request $request, $id)
{
$decrypted_id = decrypt($id);
dd($decrypted_id);
}
Blade: (my.blade.edit)
{{ Form::open(['route' => ['route.update', $encrypted_id ], 'method' => 'PATCH']) }}
{{ Form::button('Update', ['type' => 'submit', 'name' => 'update']) }}
{{ Form::close() }}
I am expecting an integer value on my dd(); but I still getting an encrypted string.
Well, as I've already written in the comments, first and simple is to check expected output and exact output.
So far we discovered, that value was sent to view isn't equal to value received in update() method.
id was encrypted twice, but we don't see two encrypt() calls in the code from the question. Probably some other code layer was making that.

display fetch data in dropdown menu with "where" value selected in laravel

I am trying to display the currencies in the dropdown menu but I need the data with a value of default=1 to be selected. upon searching, i found a sample and tried to applied it to my controller, here's what I came up,
$currencies = \DB::table('currencies')->where('default', 1)->lists('acronym');
it doesn't work. the error message said
Call to undefined method Illuminate\Database\Query\Builder::lists()
also I read a comment that the list() is already obsolete in laravel.
how can I achieve this?
here's from my create function in controller
public function create()
{
$currencies = \DB::table('currencies')->where('default', 1)->lists('acronym');
return view ('orders.create')->with('currencies', $currencies);
}
here's from create blade
{{ Form::select('currency_id', $currencies, Input::old('currency_id'),null, ['class' => 'form-control input-lg','required']) }}
thank you so much in advance!
Try using ->pluck(),
$currencies = \DB::table('currencies')->pluck('currency_name','id');
// In blade
{{ Form::select('currency_id', $currencies, null, ['class' => 'form-control input-lg','required']) }}
Read more about pluck here.

Laravel5.2 delete doesn't work

I developed website with CRUD on products table .this is the structure of the table.
Create and update works fine But delete not work.
This is the form in blade to delete product
{{ Form::open(array('url' => 'admin/products/' . $product->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete ', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
And this the destroy function in controller
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
// Product::destroy($id);
return redirect('admin/products')->with('message', 'Successfully deleted the product!');
}
And This is my routes
Route::group(['middleware' =>'App\Http\Middleware\AdminMiddleware'], function () {
//resource
Route::resource('admin/products','AdminFront');
});
When I click delete button it enter the destroy function and dd($id) correct
But when write
$product = Product::find($id);
$product->delete();
Or
Product::destroy($id);
I get this error
The localhost page isn’t working
localhost is currently unable to handle this request.
This error tired me . I developed delete fun with resource API in another table and work fine.I don't know are the problem in the db or where. please any one help me ,
What does your routes.php look like?
You may need to include the resource route in routes.php.
Route::resource('admin/products/', 'TheNameOfYourController');
But make sure the route is protected either in the controller or routes.php.
Here is somewhat the same setup you have:
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/routes.php LINE 119
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/Controllers/UsersManagementController.php LINES 369-376
https://github.com/jeremykenedy/laravel-material-design/blob/master/resources/views/admin/edit-user.blade.php LINES 243-246
Cheers!

Laravel - How to return last value from user input into a view

I'm building a backend for my website in Laravel. I need only few text boxes to populate a template. So far I've managed to do that, but every time I pass a value to a template I see also all previous values, not only the last one.
So for example if I'm editing slider title, I see also all previous slider titles - I want to see only last one.
My controler:
public function store()
{
$input = Input::all();
homepage::create([
'slider_title' => $input['slider_title'],
]);
return Redirect::to('/');
}
Part of view responsible for displaying slider title:
#foreach ($homepage as $home)
{{$home->slider_title}}
#endforeach
View for user input in admin:
{{ Form::open(['url' => '/admin']) }}
<div>
{{ Form::label('slider_title', 'Slider title:') }}
{{ Form::text('slider_title') }}
</div>
<div>
{{ Form::submit('Change a slider title') }}
</div>
{{ Form::close() }}
Route for admin:
Route::post('/admin', 'AdminController#store');
Route for main page:
Route::get('/', 'PageController#home');
PageController home method:
public function home()
{
$homepage = Homepage::all();
return View::make('home')->with('homepage',$homepage);
}
I tried changing PageControl with:
$homepage = Homepage::orderBy('id', 'desc')->first();
but I'm getting this error:
ErrorException: Trying to get property of non-object on my view page
public function home()
{
$homepage = Homepage::all();
return View::make('home')->with('homepage',$homepage);
}
You are seeing all slides because you are selecting all of them:
$homepage = Homepage::all();
This should give you the last one:
$homepage = Homepage::orderBy('id', 'desc')->first();
or
$homepage = Homepage::orderBy('created_at', 'desc')->first();
Remember that now you are getting only one result and you cannot use foreach anymore:
foreach ($homepage as $home)
You can refer to it directly:
$homepage->slider_title
If you still want to return an array to use foreach, this is an option:
$homepage = array( Homepage::orderBy('created_at', 'desc')->first() );

Resources