Hello everyone recently i started a project on Laravel 7.x but i have some problems.
I made a search with laravel default pagination.
This is my code:
Search form
<form action="/search" method="GET">
<input type="text" name="k" id="k" value="" placeholder="Search...">
<button type="submit">Search</button>
</form>
Controller
public function get_search(Request $request)
{
$keyword = request('k');
$data = DB::table('posts')
->where('title', 'LIKE', '%$keyword%')
->orderBy('id', 'DESC')
->paginate(20);
return view('search', ['data' => $data]);
Route
Route::get('/search',['uses' => 'MainController#get_search', 'as' => 'search']);
View
#foreach($data as $item)
{{ $item->title }}
#endforeach
{{ $data->links() }}
So i'm getting url like /search?key=keyword&page=2
All i want is to make looks like /search/keyword/page/2/
Thanks!
Replace ->paginate(20)
by ->paginate(20, ['*'], 'page', $page)
It's not documented.
I suggest that you modify the routes/web.php file. Use route arguments
Route::get('/search/{keyword}/page/{page}', 'SearchController#index')->name('.search');
And then, on the controller,
public function index($keyword, $page){ //Receive parameters
$data = DB::table('posts')
->where('title', 'LIKE', '%$keyword%')
->orderBy('id', 'DESC')
->paginate(20);
return view('search', ['data' => $data]);
...
}
Ok, i made what did u said above and when getting print_r($data) on search blade i am getting this
Array
(
[current_page] => 1
[data] => Array
(
[0] => stdClass Object
(
[title] => keyword 1
)
[1] => stdClass Object
(
[title] => keyword 2
)
[2] => stdClass Object
(
[title] => keyword 3
)
[3] => stdClass Object
(
[title] => keyword 4
)
[4] => stdClass Object
(
[title] => keyword 5
)
[5] => stdClass Object
(
[title] => keyword 6
)
[6] => stdClass Object
(
[title] => keyword 7
)
[7] => stdClass Object
(
[title] => keyword 8
)
[8] => stdClass Object
(
[title] => keyword 9
)
[9] => stdClass Object
(
[title] => keyword 10
)
)
[first_page_url] => hxxp://xxxx.xxx/search/keyword?page=1
[from] => 1
[last_page] => 5
[last_page_url] => hxxp://xxxx.xxx/search/keyword?page=5
[next_page_url] => hxxp://xxxx.xxx/search/keyword?page=2
[path] => hxxp://xxxx.xxx/search/keyword
[per_page] => 10
[prev_page_url] =>
[to] => 10
[total] => 49
)
Shows the same results while i am replacing the url hxxp://xxxx.xxx/search/keyword/page/2/
Any URL outputs the same result, it's just changed the ?page=x, but the results are the same. basically the hxxp://xxxx.xxx/search/keyword/page/2/ does nothing.
The results are changing only if i change the url to hxxp://xxxx.xxx/search/keyword/page/2/?page=2 which i don't need to..
Related
I have an issue in printing the data from the below json in laravel. Find below the json data and help me with this.
DarthSoup\Cart\Item Object ( [rowId] => 76cc22e6f533b8ef3d08e6eca0f110b4 [id] => 61XJrpB7CgiYSRCX6hR78wCEABhjLI7jNLzPyDrkYA3LyIVisz [name] => linux [quantity] => 1 [price] => 109 [options] => DarthSoup\Cart\CartItemOptions Object ( [items:protected] => Array ( [package] => Super Lite ) ) [subItems] => DarthSoup\Cart\SubItemCollection Object ( [items:protected] => Array ( ) ) [created_at:protected] => Carbon\Carbon Object ( [date] => 2018-06-22 05:41:18.674548 [timezone_type] => 3 [timezone] => UTC ) [updated_at:protected] => Carbon\Carbon Object ( [date] => 2018-06-22 05:41:18.674574 [timezone_type] => 3 [timezone] => UTC ) [associatedModel:protected] => [taxRate:protected] => 19 ) 1
From the above json how to display the "price=>109" and "package=>super Lite"...
I'm getting an error as "Cannot use object of type DarthSoup\Cart\Item as array"
Find below the controller Code:
public function addCart(Request $request)
{
$name = $request->input();
$hosting=$name['hosting'];
$package=$name['package'];
$price=$name['price'];
$response=Cart::add(['id'=>str_random(50) ,'name' => $hosting, 'quantity' => 1, 'price' => $price, 'options' => ['package' => $package]]);
//dd($response);
return Cart::content();
return view('main.cart',Cart::content());
}
Blade file is given below:
#foreach($response->options as $option){
{{$option['name']}}
}
#endforeach
In my output I need to display the value of price,name and package...please help me to solve this error...
First this is not json, it is php object
if your variable name is $item then you can try this
$item->rowId;
$item->id;
$item->name;
$item->price;
foreach($item->options as $option){
echo $option->package;
}
I'm trying to convert to Model binding on a Model built on customer orders. My route:
Route::model('order', 'App\Models\Order');
Route::resource('orders', 'OrderController');
This allows me to pull up an Order to edit through my controller (also grabbing statuses to populate a table and passing the logged in user):
public function index(Order $order)
{
$orders = $order->get();
return view('orders.index', compact('orders'));
}
My orders.index will display $order->id properly but when I try to loop through the Actions, which is connected by a hasMany relationship, nothing shows. Or I try to show $order->user->firstname which belongs to User by user_id.
#foreach( $order->actions as $action )
{{ $action->type->type }}
#endforeach
From my Action model:
public function order()
{
return $this->belongsTo('\App\Models\Order', 'order_id');
}
From my Order model:
public function actions()
{
return $this->hasMany('\App\Models\Action', 'order_id');
}
Here's an excerpt from a dump of the Order:
`Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Models\Order Object
(
[table:protected] => orders
[timestamps] => 1
[dates:protected] => Array
(
[0] => deleted_at
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[attributes:protected] => Array
(
[id] => 1
[created_at] => 2015-03-16 23:42:45
[updated_at] => 2015-03-19 04:37:53
[deleted_at] =>
[user_id] => 16
[status_id] => 5
[address_id] => 5
[datetime_pickup_actual] =>
[datetime_delivery_actual] =>
[datetime_pickup_requested] => 2015-03-20 17:00:00
[datetime_delivery_requested] => 2015-03-21 17:00:00
[hold] => 0
[weight] => 20
)
[original:protected] => Array
(
[id] => 1
[created_at] => 2015-03-16 23:42:45
[updated_at] => 2015-03-19 04:37:53
[deleted_at] =>
[user_id] => 16
[status_id] => 5
[address_id] => 5
[datetime_pickup_actual] =>
[datetime_delivery_actual] =>
[datetime_pickup_requested] => 2015-03-20 17:00:00
[datetime_delivery_requested] => 2015-03-21 17:00:00
[hold] => 0
[weight] => 20
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[forceDeleting:protected] =>
)`
As you compact orders in your controller you should use orders in your Blade template:
#foreach( $orders->actions as $action )
{{ $action->type->type }}
#endforeach
You should be also aware that Order in your index method in controller have nothing in common with Route Model Binding. For index it won't be used at all.
For other methods (show/edit/delete) it won't work because you make a binding with wrong name. It should be:
Route::model('orders', 'App\Models\Order');
and not
Route::model('order', 'App\Models\Order');
Model name (MoneyEntry)
Changed from
Route::resource('moneyentries', 'MoneyEntryController');
to
Route::resource('moneyEntries', 'MoneyEntryController');
Did a trick.
I have a problem with undefined variable. In view/somefolder/somefile.php they echo some variabla (<?php echo $news;?> ) . I tried everything to find where this variable is defined (Find in Path at PHPStorm), but without result. When I put print_r($GLOBALS) to test, I got
[_ci_cached_vars:protected] => Array
(
[unread] => 0
[unans] => 0
[ques_unread] => 0
[is_loged] =>
[level] =>
[avatar] => media/img/avatar.png
[username] =>
[root_cat] => 0
[logErr] =>
[radio_list] => Array
(
)
[entitet] => 1
[current_modul] => infograda
[tree] => <ul></ul>
[category] => stdClass Object
(
[id] => 153
[title] => HRONIKA
[description] =>
[parent_id] => 0
[order] => 52
[module] => infograda
[expanded] => 0
[content_type_id] => 1
)
[module] => infograda
[active] =>
[additionHtml] =>
[news] => Array
(
[0] => stdClass Object
(
[id] => 1574
[content_id] => 1574
[module] => infograda
[order] =>
[title] => Title
[text] => <div class="uvod_holder">
in Codeigniter, a view is loaded from a controller. For example:
Controller:
// this file resides in application\controllers\mycontroller.php
class Mycontroller extends CI_Controller{
function mypage() {
$data = array('news' => 'some news');
$this->load->view('page', $data);// will load views\page.php and pass the $data array
}
}
View
<!--this file resides in application\views\page.php -->
<html>
<head></head>
<body><?= $news; ?></body>
</html>
To make this work, your URL will say http://localhost/index.php/mycontroller/mypage (if you're working locally)
Your variable is being set in the controller that loads that view.
I have no qlue why I can't get the following to work:
DB::table('twitter_hashtags')->paginate(5);
Every single time I get (the second number tends to differs)
Allowed memory size of 134217728 bytes exhausted (tried to allocate 95735352 bytes)
I tried using as in (18776710), but that did not make any difference
DB::connection()->disableQueryLog();
Removing ->paginate(5) does not make any difference.
When I try:
DB::select('SELECT * FROM twitter_hashtags');
It works fine, but then I can't use the build in the pagination option.
Anyone a suggestion?
The table twitter_hashtags has currently 5500 records. An id, tweet_id and the hashtag are saved, so it can't be the problem that the table is too big.
The table's size:
Data 384,0 KB
Index 464,0 KB
Total 848,0 KB
Update
As requested some more information
This is the action
public function getHashtags()
{
DB::connection()->disableQueryLog(); // With or without does not make a difference
$retweets = DB::table('twitter_hashtags')->paginate(10);
// Show the page
return View::make('twitter/retweets', compact('retweets'));
}
As you see, I use the view of retweets, the problem exits also in retweets, or nearly any other table I try to grab the data from this way.
The 'view'
</pre><? print_r($retweets) ?></pre>
The migration I used to create the table
public function up()
{
Schema::create('twitter_hashtags', function($table)
{
// Basic run information
$table->increments('id');
$table->string('status_id')->index();
$table->string('hashtag')->index();
// Misc.
$table->timestamps();
});
}
These are the first 100 or so lines of the response when raised the memory limit to 256M
Illuminate\Database\Query\Builder Object
(
[connection:protected] => Illuminate\Database\MySqlConnection Object
(
[pdo:protected] => PDO Object
(
)
[queryGrammar:protected] => Illuminate\Database\Query\Grammars\MySqlGrammar Object
(
[wrapper:protected] => `%s`
[selectComponents:protected] => Array
(
[0] => aggregate
[1] => columns
[2] => from
[3] => joins
[4] => wheres
[5] => groups
[6] => havings
[7] => orders
[8] => limit
[9] => offset
[10] => unions
)
[tablePrefix:protected] =>
)
[schemaGrammar:protected] =>
[postProcessor:protected] => Illuminate\Database\Query\Processors\Processor Object
(
)
[events:protected] => Illuminate\Events\Dispatcher Object
(
[container:protected] => Illuminate\Foundation\Application Object
(
[booted:protected] => 1
[bootingCallbacks:protected] => Array
(
[0] => Closure Object
(
[parameter] => Array
(
[$app] =>
)
)
[1] => Closure Object
(
[static] => Array
(
[instance] => Illuminate\Log\LogServiceProvider Object
(
[defer:protected] => 1
[app:protected] => Illuminate\Foundation\Application Object
*RECURSION*
)
)
)
[2] => Closure Object
(
[static] => Array
(
[instance] => Illuminate\Mail\MailServiceProvider Object
(
[defer:protected] => 1
[app:protected] => Illuminate\Foundation\Application Object
*RECURSION*
)
)
)
[3] => Closure Object
(
[static] => Array
(
[instance] => Illuminate\Queue\QueueServiceProvider Object
(
[defer:protected] => 1
[app:protected] => Illuminate\Foundation\Application Object
*RECURSION*
)
)
)
[4] => Closure Object
(
[static] => Array
(
[instance] => Illuminate\Translation\TranslationServiceProvider Object
(
[defer:protected] => 1
[app:protected] => Illuminate\Foundation\Application Object
*RECURSION*
)
)
)
Update 2
As requested. This is the response:
Array
(
[total] => 5689
[per_page] => 5
[current_page] => 1
[last_page] => 1138
[from] => 1
[to] => 5
[data] => Array
(
[0] => stdClass Object
(
[id] => 1
[status_id] => 384992474579484672
[hashtag] => Twenterand
[created_at] => 2013-10-01 11:00:02
[updated_at] => 2013-10-01 11:00:02
)
[1] => stdClass Object
(
[id] => 2
[status_id] => 384992323190280192
[hashtag] => Twenterand
[created_at] => 2013-10-01 11:00:03
[updated_at] => 2013-10-01 11:00:03
)
[2] => stdClass Object
(
[id] => 3
[status_id] => 384989174014545921
[hashtag] => PVDA
[created_at] => 2013-10-01 11:00:03
[updated_at] => 2013-10-01 11:00:03
)
[3] => stdClass Object
(
[id] => 4
[status_id] => 384988499188801536
[hashtag] => GR2014
[created_at] => 2013-10-01 11:00:03
[updated_at] => 2013-10-01 11:00:03
)
[4] => stdClass Object
(
[id] => 5
[status_id] => 384986184092356608
[hashtag] => GR2014
[created_at] => 2013-10-01 11:00:03
[updated_at] => 2013-10-01 11:00:03
)
)
)
)
)
Update 3
Here the code I use for the getStatuses
public function getStatuses()
{
// Get all the paginated statuses
$statuses = DB::table('twitter_statuses')
->select('status_id', 'text', 'user_screen_name','datetime','place')
->orderBy('datetime', 'DESC')
->paginate(10);
// Show the page
return View::make('twitter/statuses', compact('statuses'));
}
And the complete view file
#extends('layouts/default')
{{-- Page title --}}
#section('title')
Twitter Statuses ::
#parent
#stop
{{-- Page content --}}
#section('content')
<h1>Twitter Statuses</h1>
<div class="container">
<table class="table">
<tr>
<th>Datum</th>
<th>Gebruiker</th>
<th>Tweet</th>
<th>Locatie</th>
</tr>
<?php foreach ($statuses as $status): ?>
<tr>
<td>{{ $status->datetime; }}</td>
<td><?php echo $status->user_screen_name; ?></td>
<td><?php echo $status->text; ?></td>
<td><?php echo $status->place; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo $statuses->links(); ?>
</div>
#stop
I just had the same issue. Solution is simple: Just add ->get(); at the end of the query:
public function getStatuses()
{
// Get all the paginated statuses
$statuses = DB::table('twitter_statuses')
->select('status_id', 'text', 'user_screen_name','datetime','place')
->orderBy('datetime', 'DESC')
->paginate(10)
->get()
;
// Show the page
return View::make('twitter/statuses', compact('statuses'));
}
how do i pass a multi diminutional array to a view?
controller code
public function index(){
data['navs'] = array(
'name' => array('one', 'two', 'three'),
'link' => array('one', 'two', 'three'));
$this->load->view('adminView', $data);}
view code
<?php if ($navs) {
foreach ($navs as $nav) {
echo('<li>' . $nav->name . '</li>');
}
}?>
First of all you need to build the array, the right way. It should be something like:
$data['navs'] = array( array( 'name' => 'one',
'link' => 'linkone'),
array( 'name' => 'two',
'link' => 'linktwo')
);
$this->load->view('adminView', $data);
Then in your view:
foreach($navs as $n){
echo "<li><a href='{$n['link']}'>{$n['name']}</a></li>";
}
Once in the view, refer to your data as array elelements, not object properties (you are passing the array of arrays, not array of objects). Based on your controller, your view code should look like that:
foreach ($navs as $nav) {
echo('<li>' . $nav['name'] . '</li>');
}
However, that won't output the right result because your $nav['link'] and $nav['name'] are two arrays. You'd need to call any of their elements or change controller accordingly.
how to fetch dynamic array value by using controller of codeigniter in php
Array
(
[id] => 2
[name] => Wellness & Spa
[isSelected] => true
[subModules] => Array
(
[0] => Array
(
[id] => 1
[name] => Spa1
[isSelected] => true
[0] => object:81
)
[1] => Array
(
[id] => 2
[name] => Spa2
[isSelected] => true
[0] => object:82
)
[2] => Array
(
[id] => 3
[name] => Wellness
[isSelected] => true
[0] => object:83
)
)
)